通过前面多篇文件对SpringDataJPA的介绍,相信大家应该已经对SpringDataJPA很熟悉了,使用起来还是蛮方便的,只是在整合的时候需要添加大量的配置文件,本文来给大家介绍下SpringBoot整合SpringDataJPA的过程
其他JPA接口的实现参考此链接:
https://dpb-bobokaoya-sm.blog.csdn.net/column/info/38759
SpringBoot整合SpringDataJPA
1.构建SpringBoot项目
通过IDEA创建一个SpringBoot项目。添加如下依赖。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springBoot的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 测试工具的启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> </dependencies>
2.添加相关的配置
在application.properties中添加如下配置
# jdbc 的相关信息 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 # 配置连接池信息 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 配置jpa的相关参数 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
这比我们在Spring中整合SpringDataJPA的配置文件要简单很多!
3.创建pojo
/** * @program: spring-boot-data-jpa-demo * @description: pojo * @author: 波波烤鸭 * @create: 2019-05-20 11:15 */ @Entity @Table(name="t_users") public class Users { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id") private Integer id; @Column(name="name") private String name; @Column(name="age") private Integer age; @Column(name="address") private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]"; } }
4.创建dao接口
创建Dao的接口,实现Repository接口
/** * @program: spring-boot-data-jpa-demo * @description: Dao接口 * @author: 波波烤鸭 * @create: 2019-05-20 11:17 */ import com.dpb.spring_boot_data_jpa_demo.pojo.Users; import org.springframework.data.jpa.repository.JpaRepository; /** * 参数一 T :当前需要映射的实体 * 参数二 ID :当前映射的实体中的OID的类型 * */ public interface UsersRepository extends JpaRepository<Users,Integer> { }
5.创建启动器
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDataJpaDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDataJpaDemoApplication.class, args); } }
6.单元测试
import com.dpb.spring_boot_data_jpa_demo.dao.UsersRepository; import com.dpb.spring_boot_data_jpa_demo.pojo.Users; 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; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootDataJpaDemoApplication.class) public class SpringBootDataJpaDemoApplicationTests { @Autowired private UsersRepository usersRepository; @Test public void testSave(){ Users users = new Users(); users.setAddress("深圳固戍"); users.setAge(20); users.setName("张三"); this.usersRepository.save(users); } }