Spring Boot 整合mybatis

<Excerpt in index | 首页摘要>

<The rest of contents | 余下全文>

创建项目 pom.xml中添加依赖

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.caicai</groupId>
<artifactId>12-spring-boot-springmvc-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>12-spring-boot-springmvc-mybatis</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

创建application.yml

1
2
3
4
5
6
7
8
9
10
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/ssm
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.caicai.springbootspringmvcmybatis.pojo
mapper-locations: classpath:mapper/*.xml

创建Users类

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
package com.caicai.springbootspringmvcmybatis.pojo;

public class Users {

private Integer id;
private String name;
private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

创建Controller

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
package com.caicai.springbootspringmvcmybatis.controller;

import com.caicai.springbootspringmvcmybatis.pojo.Users;
import com.caicai.springbootspringmvcmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author liu
* @title: Controller
* @projectName 12-spring-boot-springmvc-mybatis
* @description: TODO
* @date 2019/3/3113:01
*/
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
@RequestMapping("/addUser")
@ResponseBody
public String addUser(Users user){
userService.addUser(user);
return "success";
}
}

创建 UserMapper接口和UsersMapper.xml(创建路径在resources/mapper下)

1
2
3
4
5
6
7
8
9
10
package com.caicai.springbootspringmvcmybatis.mapper;


import com.caicai.springbootspringmvcmybatis.pojo.Users;

public interface UsersMapper {

void insertUser(Users users);
}

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caicai.springbootspringmvcmybatis.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values(#{name},#{age})
</insert>
</mapper>

创建Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.caicai.springbootspringmvcmybatis.service;

import com.caicai.springbootspringmvcmybatis.pojo.Users;

/**
* @author liu
* @title: UserService
* @projectName 12-spring-boot-springmvc-mybatis
* @description: TODO
* @date 2019/3/3112:16
*/
public interface UserService {
void addUser(Users users);
}

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
package com.caicai.springbootspringmvcmybatis.service;

import com.caicai.springbootspringmvcmybatis.mapper.UsersMapper;
import com.caicai.springbootspringmvcmybatis.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author liu
* @title: UserServiceImpl
* @projectName 12-spring-boot-springmvc-mybatis
* @description: TODO
* @date 2019/3/3112:25
*/
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
}

创建page.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
用户姓名<input type="text" name="name"><br>
用户年龄<input type="text" name="age"><br>
<input type="submit" th:value="确定"><br>

</form>

</body>
</html>

创建启动类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.caicai.springbootspringmvcmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.caicai.springbootspringmvcmybatis.mapper")
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}