SpringBoot除了使用Mybatis操作数据库外,还可以使用JPA来操作数据库,好处是不需要在手动去数据库建表了。

首先配置依赖,POM.XML

<dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jpa</artifactId>
         </dependency>
 ​

配置数据源和一些配置

#mysql
 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/edumanager?characterEncoding=utf8&useSSL=false
 spring.datasource.username=edumanager
 spring.datasource.password=edumanager
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 ​
 # Hikari 数据源专用配置
 spring.datasource.hikari.maximum-pool-size=20
 spring.datasource.hikari.minimum-idle=5
 # JPA 相关配置
 spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
 spring.jpa.show-sql=true
 spring.jpa.hibernate.ddl-auto=update

创建实体类User.java,使用了lombok插件可以不用写下面的get/set方法,启动运行一下,数据库中会自动创建MAIN_USER

package cn.vwmwv.hgnuman.entity;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 ​
 import javax.persistence.*;
 ​
 @Data
 @AllArgsConstructor // 自动有参构造
 @NoArgsConstructor // 自动无参构造
 ​
 @Entity  // 标注实体
 @Table(name = "MAIN_USER")  //表示对应于数据库中的MAIN_USER,将会自动创建
 public class User {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)  // 自增主键
     private Integer id; // 用户id
 ​
     @Column(length = 100)
     private String username;  // 用户名
 ​
     @Column(length = 100)
     private String pwd;  // 用户密码
 ​
     @Column(length = 100)
     private Integer user_jurisdiction;  // 用户权限 1 管理员,2维修人员 3,学生
 ​
 // get/set方法,使用Lombok插件可省略
     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 String getPwd() {
         return pwd;
     }
 ​
     public void setPwd(String pwd) {
         this.pwd = pwd;
     }
 ​
     public Integer getUser_jurisdiction() {
         return user_jurisdiction;
     }
 ​
     public void setUser_jurisdiction(Integer user_jurisdiction) {
         this.user_jurisdiction = user_jurisdiction;
     }
 }

写一个UserRepository接口继承JpaRepository接口,可以实现大部分的CRUD方法。

package cn.vwmwv.hgnuman.repository;
 ​
 import cn.vwmwv.hgnuman.entity.User;
 import org.springframework.data.jpa.repository.JpaRepository;
 ​
 public interface UserRepository extends JpaRepository<User,Integer> {
 }

写一个controller,用于实现CRUD

package cn.vwmwv.hgnuman.controller;
 ​
 import cn.vwmwv.hgnuman.dao.UserDao;
 import cn.vwmwv.hgnuman.entity.User;
 import cn.vwmwv.hgnuman.repository.UserRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 ​
 @RestController
 public class UserController {
     @Autowired
     UserRepository userRepository;
 ​
     @GetMapping("/user/{id}") //http://127.0.0.1:8080/user/1
     public User GetUser(@PathVariable("id") Integer id){
         return userRepository.findById(id).orElse(null); //根据id查找数据,
     }
 ​
     @GetMapping("/user") // http://127.0.0.1:8080/user?username=zhangs&pwd=1235&user_jurisdiction=2
     public User insertUser(User user){
         return userRepository.save(user); // 直接返回保存的数据
     }
 }

本站墙裂推荐,便宜好用的云服务器,点这儿购买
LIZHI_STORE
如果觉得我的文章对你有用,请随意赞赏