今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助!
- 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
- 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;
- }
- }
- 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
{ - }
- 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
list = userIfoRepository.findAll(); - for (UserInfoDAO item : list) {
- System.out.println("姓名:" + item.getUserName()
- + " 年龄:" + item.getAge()); }
- // 查询单个记录
- Optional
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());
- }
- }
- 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"})
本文转载自微信公众号「IT技术分享社区」,可以通过以下二维码关注。转载本文请联系IT技术分享社区公众号。
当前文章:SpringBoot集成JPA用法笔记
标题来源:http://www.shufengxianlan.com/qtweb/news22/440472.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联