搭建集成Mybatis的Web应用程序

​ 上期说到搭建一个SpringBoot程序,该程序创建的若干符合Restful风格的接口,同时返回一些字符串,但在实际情况中,返回的字符串是有查询数据库操作的,所以这期内容是搭建集成Mybatis的Web应用程序。

​ 因为yml文件的好处是有天然的树状结构,配置和使用比较方便,所以我个人更推荐使用yml后缀的配置文件。

application.properties 切换到 application.yml步骤:

​ 修改后缀

​ 重新修改程序

​ 如下图依次点击Maven的clean、install即可

创建数据库、建表

使用Navicat创建mysql数据库

创建以下字段

最后输入表名

向user表插入一些数据

添加MySQL驱动包和Mybatis集成依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>

在根目录的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
package com.example.demo.entity;

/**
* @Author: panghai
* @Date: 2022/05/10/22:42
* @Description: 用户实体类
*/
public class User {
private long userId;
private String userName;
private String password;
private boolean sex;

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public boolean isSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", sex=" + sex +
'}';
}
}

创建用户api接口

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
package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* @Author: panghai
* @Date: 2022/05/10/22:50
* @Description: 用户api接口
*/
@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private IUserService userService;

@GetMapping("/getOneUser")
public User getOnUser(@RequestParam("id") long id){
return userService.getOneUser(id);
}
}

创建用户业务类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.example.demo.service;

import com.example.demo.entity.User;

/**
* @Author: panghai
* @Date: 2022/05/10/22:46
* @Description: 用户业务类
*/

public interface IUserService {

public User getOneUser(long id);
}

创建用户业务实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.demo.service.imp;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @Author: panghai
* @Date: 2022/05/10/22:46
* @Description: 用户业务实现类
*/
@Service
public class UserServiceImpl implements IUserService {

@Autowired
private UserMapper userMapper;

@Override
public User getOneUser(long id) {
return userMapper.selectUserById(id);
}
}

创建用户Mapper接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;

/**
* @Author: panghai
* @Date: 2022/05/10/22:45
* @Description: 用户Mapper接口
*/
@Mapper
public interface UserMapper {

public User selectUserById(long id);
}

创建用户Mapper

1
2
3
4
5
6
7
8
9
10
<?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.example.demo.mapper.UserMapper">

<select id="selectUserById" parameterType="Long" resultType="User">
select user_id,username,password from user where user_id=#{user_id};
</select>

</mapper>

测试

启动调试成功后,浏览器访问http://localhost:9090/user/getOneUser?id=1

最后根据需求在创建其他类型的实体类以及业务类即可。

常见问题

  1. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database ‘user’

    原因:数据库名称有误

  2. Field userService in com.example.demo.controller.UserController required a bean of type ‘com.example.demo.service.IUserService’ that could not be found.

    原因:业务实现类没有添加注解@Service

  3. Field userMapper in com.example.demo.service.imp.UserServiceImpl required a bean of type ‘com.example.demo.mapper.UserMapper’ that could not be found.

    原因:业务Mapper类没有添加注解@Mapper

参考文章

MyBatis Mapper(映射器)

第一个MyBatis程序

往期文章

Linux、Shell介绍

开发环境配置

搭建Web应用程序

搭建集成Mybatis的Web应用程序

坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道