今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助!
搭建SpringBoot项目
新建配置文件 application.yml
server:
port: 8090
spring:
#通用的数据源配置
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
#将默认的存储引擎切换为 InnoDB
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
#配置在日志中打印出执行的 SQL 语句信息。
show-sql: true
hibernate:
#配置指明在程序启动的时候要删除并且创建实体类对应的表
# validate 加载 Hibernate 时,验证创建数据库表结构
# create 每次加载 Hibernate ,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
# create-drop 加载 Hibernate 时创建,退出是删除表结构(退出是指退出sessionFactory)
# update 加载 Hibernate 自动更新数据库结构
# none 不启用
ddl-auto: none
3、新建用户实体类 UserInfoDAO.java
package my.springboot.jpa.entity;
import javax.persistence.*;
import java.util.Date;
/**
* 用户表实体
* **/
@Entity
@Table(name = "userinfo")
public class UserInfoDAO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
private String userName;
@Column
private Integer age;
@Column(length = 500)
private String address;
@Column(name = "create_date")
private Date createDate;
@Column(name = "create_user")
private String createUser;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
}
4、仓库接口类 UserIfoRepository
package my.springboot.jpa.dao;
import my.springboot.jpa.entity.UserInfoDAO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* 仓库接口类 UserIfoRepository
**/
@Repository
public interface UserIfoRepository extends
JpaRepository<UserInfoDAO, Integer> {
}
5、新建测试用户类 UserInfoTest.java
package my.springboot.jpa;
import my.springboot.jpa.dao.UserIfoRepository;
import my.springboot.jpa.entity.UserInfoDAO;
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;
import java.util.Date;
import java.util.List;
import java.util.Optional;
/**
* 测试UserInfo用法
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserInfoTest {
@Autowired
UserIfoRepository userIfoRepository;
@Test
public void test() {
//插入用户测试
UserInfoDAO dao = new UserInfoDAO();
dao.setUserName("小明");
dao.setAge(32);
dao.setCreateDate(new Date());
dao.setCreateUser("管理员");
dao.setAddress("苏州");
userIfoRepository.save(dao);
UserInfoDAO dao2 = new UserInfoDAO();
dao2.setUserName("小张");
dao2.setAge(35);
dao2.setCreateDate(new Date());
dao2.setCreateUser("管理员");
dao2.setAddress("南京");
userIfoRepository.save(dao2);
// 查询多条记录 打印
List<UserInfoDAO> list = userIfoRepository.findAll();
for (UserInfoDAO item : list) {
System.out.println("姓名:" + item.getUserName()
+ " 年龄:" + item.getAge()); }
// 查询单个记录
Optional<UserInfoDAO> mo = userIfoRepository.findById(2);
System.out.println(mo.get().getUserName());
//更新操作
mo.get().setUserName("小明123");
userIfoRepository.save(mo.get());
System.out.println(mo.get().getUserName());
//删除记录
userIfoRepository.delete(mo.get());
}
}
6、配置生成数据表
package my.springboot.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan(basePackages = {"my.springboot.jpa.entity"})
@EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"})
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
@EntityScan(basePackages = {"my.springboot.jpa.entity"})
@EnableJpaRepositories(basePackages = {"my.springboot.jpa.dao"})
7、项目结构图
IT技术分享社区
个人博客网站:https://programmerblog.xyz
文章推荐程序员效率:画流程图常用的工具程序员效率:整理常用的在线笔记软件远程办公:常用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识