spring-boot-data-jpa
程序员文章站
2022-06-14 15:23:57
...
学习途径:官方文档和API
jpa(Java Persistence API):wiki解释
前言:
不管是JPA还是JDBC都是基于orm模型的连接数据库驱动,数据库按照特征可以分为 embedded datasource和proudction datasource
- embedded:derby、h2等
- proudction: mysql postgresql等
spring-boot-data-jpa自动产生一个repository bean.
具体实现
1、pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--如果是proudction DB, 需注释-->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.1.0</version>
<scope>test</scope>
</dependency>
<!--如果是embeded DB, 需注释-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>compile</scope>
</dependency>
注:以上的依赖包可以看出: derby是一个完整功能的jar ,而mysql只是一个链接jar。
2、dao 层写repository接口
package xxxx.membermanagersystem.dao;
import xxxx.membermanagersystem.model.Tmember;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Tmember,Long> {}
3、model层写实体
package xxxx.membermanagersystem.model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Tmember implements Serializable {
@Id
@GeneratedValue
private int id;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private String phone;
@Column(nullable=false)
private String ethaddress;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEthaddress() {
return ethaddress;
}
public void setEthaddress(String ethaddress) {
this.ethaddress = ethaddress;
}
}
4、修改application.properties
spring.datasource.url=jdbc:mysql://192.168.xxx.xxx:xxx/xxx(库名)
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
#spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.hibernate.ddl-auto 和 spring.jpa.properties.hibernate.hbm2ddl.auto是等效的。
5、创建test 类
package xxxx.membermanagersystem.dao;
import xxxx.membermanagersystem.Application;
import xxxx.membermanagersystem.model.Tmember;
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.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)
public class TMemberTest {
@Autowired
private MemberRepository repository;
@Test
public void test01Member(){
Tmember member=new Tmember();
member.setName("xxxxxx")
member.setPhone("xxxxxx");
member.setEthaddress("xxxxx");
Tmember result=(Tmember) repository.save(member);
assertNotNull("id couldn't null",result.getId());
}
}
推荐阅读