SpringBoot集成JPA用法笔记

               

今天给大家整理SpringBoot集成JPA用法。希望对大家能有所帮助!

  1. 搭建SpringBoot项目

               

               

               

  1. 新建配置文件 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及串口基础知识硬件:断路器、接触器、继电器基础知识

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 659
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 659
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT技术分享社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值