温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中怎么集成JPA

发布时间:2021-06-15 11:13:08 来源:亿速云 阅读:157 作者:Leah 栏目:web开发

今天就跟大家聊聊有关SpringBoot中怎么集成JPA,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.搭建SpringBoot项目

SpringBoot中怎么集成JPA

SpringBoot中怎么集成JPA

SpringBoot中怎么集成JPA

2.新建配置文件 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、项目结构图

SpringBoot中怎么集成JPA

看完上述内容,你们对SpringBoot中怎么集成JPA有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI