目前工作中在使用Spring Boot2,陆续会集成众多开发框架,因此整理成博客,记录下来,方便以后使用。
管理工具使用Maven,采用模块化开发,各模块尽量能够独立运行,主要用于记录学习过程,因此可能会有很多冗余代码。
项目结构图
项目pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 从spring-boot-starter-parent继承版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- 定义当前Maven项目隶属的实际项目 -->
<!-- 首先,Maven项目和实际项目不一定是一对一的关系,一个项目往往会被分为很多模块 -->
<!-- 其次,groupId不应该对应项目隶属的组织或公司,一个组织可能会有很多实际项目 -->
<groupId>com.wu.parker</groupId>
<!-- 定义实际项目中的一个Maven项目,推荐做法是使用实际项目名称作为artifactId的前缀 -->
<!-- 本项目将包含多个模块,因此作为总项目 -->
<artifactId>parker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>parker-jpa</module>
</modules>
<!-- 定义Maven项目的打包方式,默认为jar -->
<!-- 对于聚合模块来说,必须是pom,否则无法构建 -->
<packaging>pom</packaging>
<!-- 给项目提供一个更易阅读的名字 -->
<name>${project.artifactId}</name>
<description>spring boot demo</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<postgresql.version>42.2.5</postgresql.version>
</properties>
<!-- 继承的依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 依赖范围,默认为compile,此处为test,只对测试有效 -->
<scope>test</scope>
</dependency>
</dependencies>
<!-- dependencyManagement既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性 -->
<!-- dependencyManagement下的依赖声明不会引入实际的依赖,不过它能约束dependencies下的依赖使用 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>parker</finalName>
</build>
</project>
parker-jpa模块的pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parker</artifactId>
<groupId>com.wu.parker</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>parker-jpa</artifactId>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
application.yml
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/test
username: postgres
password: postgres
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.PostgreSQLDialect
temp:
use_jdbc_metadata_defaults: false
show-sql: true
启动类
package com.wu.parker.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author: wusq
* @date: 2018/11/22
*/
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
User实体类
package com.wu.parker.jpa.po;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
* @author: wusq
* @date: 2018/11/22
*/
@Entity
@Table(name="user_test")
public class User {
@Id
@GenericGenerator(name = "uuid", strategy = "uuid")
@GeneratedValue(generator = "uuid")
@Column(length = 32)
private String id;
@Column(length = 32, unique = true, nullable = false)
private String username;
@Column(length = 32, nullable = false)
private String password;
// 请自行补上get/set方法
}
持久层接口
持久层接口只需继承JpaRepository即可,好像还有其他接口可以继承,可以按需修改。
package com.wu.parker.jpa.dao;
import com.wu.parker.jpa.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author: wusq
* @date: 2018/11/22
*/
public interface UserRepository extends JpaRepository<User, String> {
}
测试类
package com.wu.parker.jpa.test;
import com.wu.parker.jpa.dao.UserRepository;
import com.wu.parker.jpa.po.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author: wusq
* @date: 2018/11/22
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void contextLoads() {
}
@Test
public void testSave() throws Exception {
User user = new User();
user.setUsername("admin");
user.setPassword("admin");
userRepository.save(user);
}
}
运行测试类,会自动在数据库建表,并保存一条数据。
源代码
https://github.com/wu-boy/parker.git
参考资料
1、《Maven实战》,徐晓斌著。
2、springboot(五):spring data jpa的使用