Springboot整合mybaits

1.加依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <parent>
 7         <groupId>org.springframework.boot</groupId>
 8         <artifactId>spring-boot-starter-parent</artifactId>
 9         <version>2.2.0.RELEASE</version>
10         <relativePath/> <!-- lookup parent from repository -->
11     </parent>
12 
13     <groupId>com.offcn</groupId>
14     <artifactId>springbootmybatisdemo</artifactId>
15     <version>0.0.1-SNAPSHOT</version>
16     <name>springbootmybatisdemo</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <properties>
20         <java.version>1.8</java.version>
21     </properties>
22 
23     <dependencies>
24 
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29 
30         <!--springBoot data jpa的依赖-->
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-data-jpa</artifactId>
34         </dependency>
35 
36         <!--SpringBoot 整合 mybatis  :1.添加依赖-->
37         <!--mybatis依赖-->
38         <dependency>
39             <groupId>org.mybatis.spring.boot</groupId>
40             <artifactId>mybatis-spring-boot-starter</artifactId>
41             <version>1.1.1</version>
42         </dependency>
43         <!--mysql依赖-->
44         <dependency>
45             <groupId>mysql</groupId>
46             <artifactId>mysql-connector-java</artifactId>
47         </dependency>
48         <dependency>
49             <groupId>org.springframework.boot</groupId>
50             <artifactId>spring-boot-starter-freemarker</artifactId>
51         </dependency>
52 
53         <dependency>
54             <groupId>org.projectlombok</groupId>
55             <artifactId>lombok</artifactId>
56         </dependency>
57 
58         <dependency>
59             <groupId>org.springframework.boot</groupId>
60             <artifactId>spring-boot-starter-test</artifactId>
61             <scope>test</scope>
62            <!-- <exclusions>
63                 <exclusion>
64                     <groupId>org.junit.vintage</groupId>
65                     <artifactId>junit-vintage-engine</artifactId>
66                 </exclusion>
67             </exclusions>-->
68         </dependency>
69     </dependencies>
70 
71     <build>
72 
73         <resources>
74             <resource>
75                 <directory>src/main/java</directory>
76                 <includes>
77                     <include>**/*.properties</include>
78                     <include>**/*.xml</include>
79                 </includes>
80                 <filtering>false</filtering>
81             </resource>
82             <resource>
83                 <directory>src/main/resources</directory>
84                 <includes>
85                     <include>**/*.*</include>
86                 </includes>
87                 <filtering>false</filtering>
88             </resource>
89         </resources>
90 
91         <plugins>
92             <plugin>
93                 <groupId>org.springframework.boot</groupId>
94                 <artifactId>spring-boot-maven-plugin</artifactId>
95             </plugin>
96         </plugins>
97     </build>
98 
99 </project>
View Code

2、配置文件:

 1 #DB Configuration
 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 3 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
 4 spring.datasource.username=root
 5 spring.datasource.password=root
 6 # JPAConfiguration
 7 spring.jpa.database=mysql
 8 spring.jpa.show-sql=true
 9 spring.jpa.generate-ddl=true
10 #添加文件转发后缀:*重要
11 spring.freemarker.suffix=.ftl
View Code

3.实体类:

 1 package com.offcn.springbootmybatisdemo.pojo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 import javax.persistence.*;
 8 import java.io.Serializable;
 9 
10 
11 /*@Data
12 @AllArgsConstructor
13 @NoArgsConstructor*/
14 @Entity
15 @Table(name = "user")
16 public class User{
17     @Id
18     @GeneratedValue(strategy = GenerationType.IDENTITY) // 主键  自增长
19     private Integer uid;
20     private String uname;
21     private String password;
22 
23     public User(Integer uid, String uname, String password) {
24         this.uid = uid;
25         this.uname = uname;
26         this.password = password;
27     }
28 
29     public User() {
30     }
31 
32     public Integer getUid() {
33         return uid;
34     }
35 
36     public void setUid(Integer uid) {
37         this.uid = uid;
38     }
39 
40     public String getUname() {
41         return uname;
42     }
43 
44     public void setUname(String uname) {
45         this.uname = uname;
46     }
47 
48     public String getPassword() {
49         return password;
50     }
51 
52     public void setPassword(String password) {
53         this.password = password;
54     }
55 
56     @Override
57     public String toString() {
58         return "User{" +
59                 "uid=" + uid +
60                 ", uname='" + uname + '\'' +
61                 ", password='" + password + '\'' +
62                 '}';
63     }
64 }
View Code

4.mapper:

1 import java.util.List;
2 
3 public interface UserMapper {
4     List<User> getUserList();
5 }
View Code

5.映射文件

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="com.offcn.springbootmybatisdemo.mapper.UserMapper">
6     <select id="getUserList" resultType="com.offcn.springbootmybatisdemo.pojo.User">
7         select * from user
8     </select>
9 </mapper>
View Code

6.Controller·:

 1 @Controller
 2 public class FtlController {
 3     @Autowired
 4     private UserMapper userMapper;
 5 
 6     @RequestMapping("/showUserList")
 7     public String getUserList(Model model){
 8         List<User> userList = userMapper.getUserList();
 9         for (User user : userList) {
10             System.out.println(user);
11         }
12         model.addAttribute("userList",userList);
13         return "user";
14     }
15 }
View Code

7.user.ftl

 1 <html>
 2     <head>
 3         <title>Springboot 模板测试</title>
 4     </head>
 5     <body>
 6         <table border="1" align="center" cellpadding="10" cellspacing="1">
 7             <thead>
 8                 <tr>
 9                     <th>id</th>
10                     <th>用户名</th>
11                     <th>密码</th>
12                 </tr>
13             </thead>
14             <tbody>
15             <#list userList as user>
16                 <tr>
17                     <td>${user.uid}</td>
18                     <td>${user.uname}</td>
19                     <td>${user.password}</td>
20                 </tr>
21             </#list>
22             </tbody>
23         </table>
24     </body>
25 </html>
View Code

猜你喜欢

转载自www.cnblogs.com/xxblogs/p/11802199.html