SpringBoot整合Spring Data JPA

1、添加相关依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.offcn</groupId>
 8     <artifactId>springbootmybatis</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.0.2.RELEASE</version>
15     </parent>
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21         <!-- springBoot JPA的起步依赖 -->
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-data-jpa</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>mysql</groupId>
29             <artifactId>mysql-connector-java</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-freemarker</artifactId>
34         </dependency>
35 
36         <dependency>
37             <groupId>org.mybatis.spring.boot</groupId>
38             <artifactId>mybatis-spring-boot-starter</artifactId>
39             <version>1.1.1</version>
40         </dependency>
41 
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46         </dependency>
47 
48         <dependency>
49             <groupId>org.springframework.boot</groupId>
50             <artifactId>spring-boot-starter-data-redis</artifactId>
51         </dependency>
52         <dependency>
53             <groupId>org.springframework.boot</groupId>
54             <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
55         </dependency>
56         <dependency>
57             <groupId>org.projectlombok</groupId>
58             <artifactId>lombok</artifactId>
59             <version>1.18.10</version>
60             <scope>provided</scope>
61         </dependency>
62     </dependencies>
63     <build>
64         <resources>
65             <resource>
66                 <directory>src/main/java</directory>
67                 <includes>
68                     <include>**/*.properties</include>
69                     <include>**/*.xml</include>
70                 </includes>
71                 <filtering>false</filtering>
72             </resource>
73             <resource>
74                 <directory>src/main/resources</directory>
75                 <includes>
76                     <include>**/*.*</include>
77                 </includes>
78                 <filtering>false</filtering>
79             </resource>
80         </resources>
81         <plugins>
82             <plugin>
83                 <groupId>org.springframework.boot</groupId>
84                 <artifactId>spring-boot-maven-plugin</artifactId>
85             </plugin>
86         </plugins>
87     </build>
88 
89 </project>

 注意:导入的依赖中包含了整合Mybatis的依赖,多余的代码未删除,因为这是整合mybatis后写的随笔。

2、在数据库中创建user

 

3application.yml文件配置

 1 #DB Configation
 2 spring:
 3   datasource:
 4     driverClassName: com.mysql.jdbc.Driver
 5     url: jdbc:mysql://127.0.0.1:3306/cat
 6     username: root
 7     password: root
 8     jpa:
 9       database: Mysql
10       show-sql: true
11       generate: true
12 
13 page:
14   rows: 1

4User实体类

package com.offcn.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user")
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer uid;
   private String uname;
   private String pwd;
   private String gender;
   private String phone;
}

5、UserController控制层

 1 package com.offcn.controller;
 2 
 3 import com.offcn.entity.User;
 4 import com.offcn.mapper.UserMapper;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Value;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.ui.Model;
 9 import org.springframework.web.bind.annotation.GetMapping;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 @Controller
18 @RequestMapping("/user")
19 public class UserController {
20 
21    @Autowired
22    private UserMapper userMapper;
23 
24    @Value("${page.rows}")
25    private String rows;
26 
27    @GetMapping("/get")
28    @ResponseBody
29    public List<User> showUser(){
30       return userMapper.findAll();
31    }
32 
33    @GetMapping("/show")
34    public String showUserList(Model model){
35       List<User> userList = userMapper.findAll();
36       model.addAttribute("userList",userList);
37       return "user";
38    }
39 
40    @GetMapping("rows")
41    @ResponseBody
42    public Map getRows(){
43       Map<String,Object> map = new HashMap<>();
44       map.put("rows",rows);
45       return map;
46    }
47 
48 }

6、UserMapper

1 package com.offcn.mapper;
2 
3 import com.offcn.entity.User;
4 import org.springframework.data.jpa.repository.JpaRepository;
5 
6 public interface UserMapper extends JpaRepository<User,Integer> {
7 }

7、主启动类

 1 package com.offcn;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 /**
 8  * @author yy
 9  * @date 2019/11/5 9:56
10  */
11 @SpringBootApplication
12 public class HelloApplication {
13    public static void main(String[] args) {
14       SpringApplication.run(HelloApplication.class,args);
15    }
16 }

8、user.ftl静态文件

 1 <html>
 2     <head>
 3         <title>user</title>
 4     </head>
 5     <body>
 6         <table border="1px">
 7             <thead>
 8                 <tr>
 9                     <th>序号</th>
10                     <th>姓名</th>
11                     <th>密码</th>
12                     <th>性别</th>
13                     <th>电话</th>
14                 </tr>
15             </thead>
16             <tbody>
17                 <#list userList as user>
18                     <tr>
19                         <td>${user.uid}</td>
20                         <td>${user.uname}</td>
21                         <td>${user.pwd}</td>
22                         <td>${user.gender}</td>
23                         <td>${user.phone}</td>
24                     </tr>
25                 </#list>
26             </tbody>
27         </table>
28     </body>
29 </html>

5、启动项目,测试

原文地址:https://www.cnblogs.com/lifefamily/p/11801433.html