SpringBoot(十六)-----Springboot整合JPA

今天用Springboot配合JPA写一个增删改查的小例子

程序结构

 

首先,加入JPA依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>myspringboot002</groupId>
  <artifactId>myspringboot002</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  </dependencies>
</project>

写配置文件application.properties

1
2
3
4
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Controller层

StudentController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.zk.Controller;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zk.Dao.StudentDao;
import com.zk.Service.StudentService;
import com.zk.entity.Student;
 
@RestController
public class StudentController {
    @Autowired
    private StudentService userService;
     
    @Autowired
    private StudentDao userDao;
     
    @RequestMapping("/createUser")
    private String createUser(String Sname,String Sno,Integer Sgrade) {
        userService.createUser(Sname,Sno,Sgrade);
        return "success";
    }
     
    @RequestMapping("/updateUser")
    public String updateStudent(Integer Sid) {
        List<Student> Studentlist=userDao.findAll();
        for(Student stu:Studentlist) {
            System.out.println(stu.toString());
        }
        return "success";
    }
     
    @RequestMapping("/getUser")
    public Student getStudent(Integer Sid) {
        return userDao.findOne(Sid);
    }
     
    @RequestMapping("/deleteUser")
    public String deleteStudent(Integer Sid) {
        userDao.delete(Sid);
        return "success";
    }
}

Dao层

StudentDao.java

1
2
3
4
5
6
7
8
9
10
package com.zk.Dao;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
import com.zk.entity.Student;
 
public interface StudentDao extends JpaRepository<Student,Integer>{
 
 
}

 Student.java student实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.zk.entity;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
 
@Entity(name="Student")
public class Student {
    @Id
    private Integer Sid;
    @Column
    private String Sname;
    @Column
    private String Sno;
    @Column
    private Integer Sgrade;
     
    public Integer getSid() {
        return Sid;
    }
    public void setSid(Integer sid) {
        Sid = sid;
    }
    public String getSname() {
        return Sname;
    }
    public void setSname(String sname) {
        Sname = sname;
    }
    public String getSno() {
        return Sno;
    }
    public void setSno(String sno) {
        Sno = sno;
    }
    public Integer getSgrade() {
        return Sgrade;
    }
    public void setSgrade(Integer sgrade) {
        Sgrade = sgrade;
    }
    @Override
    public String toString() {
        return "Student [Sid=" + Sid + ", Sname=" + Sname + ", Sno=" + Sno + ", Sgrade=" + Sgrade + "]";
    }
     
     
}

  Service层 StudentService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.zk.Service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
 
import com.zk.Dao.StudentDao;
 
@Service
public class StudentService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
     
    @Autowired
    private StudentDao userDao;
     
    public void createUser(String sname, String sno, Integer sgrade) {
        System.out.println("createUsers");
        jdbcTemplate.update("insert into student(Sname,Sno,Sgrade) value(?,?,?)",sname,sno,sgrade);
        //jdbcTemplate.execute("Select * from Student");
        System.out.println("创建用户成功");
    }
     
    public void createJPAUser(String name,Integer no,Integer grade) {
        System.out.println("createUsers");
        //jdbcTemplate.execute("Select * from Student");
        System.out.println("创建用户成功");
    }
}

  启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.zk.myspringboot007;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 
@ComponentScan(basePackages={"com.zk.Controller","com.zk.Service","com.zk.Dao"})
@EnableJpaRepositories("com.zk.Dao")
@EnableAutoConfiguration
@EntityScan("com.zk.entity")
public class SpringBootApplicationSixth {
    public static void main(String[]args){
        SpringApplication.run(SpringBootApplicationSixth.class, args);
    }
    public static void run(String...arg0) {
        System.out.println("Hello world from Command Line Runner");
    }
}

测试结果如下:

增加用户

 

 

删除用户

 

 

 

获取用户

更新有点问题,还没理解具体什么原因。。。。。。

 

posted @   leagueandlegends  阅读(149)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示