(1)pom.xml 依赖配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
(2)application.properties 配置
#数据源配置(默认)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#xml配置文件
mybatis.mapper-locations=classpath:mappers/*.xml
(3)启动类添加 @MapperScan 注解,指定basePackages,扫描mybatis Mapper接口类。
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
接下来就是业务的东西了。。。举个粟子:
(4)UserModel.java
public class UserModel {
private Integer userId;
private String userName;
private String password;
private String phone;
//省略getter和setter
}
(5)UserDao.java, (3)中所添加的 @MapperScan 就是扫描这些 mapper 类
package com.example.demo.dao;
import java.util.List;
import com.example.demo.model.UserModel;
public interface UserDao {
int insert(UserModel record);
List<UserModel> selectUsers();
}
(6)UserMapper.xml
<?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.dao.UserDao">
<resultMap id="BaseResultMap" type="com.example.demo.model.UserModel">
<id column="userId" property="userId" jdbcType="INTEGER" />
<result column="userName" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
</resultMap>
<sql id="BASE_TABLE">
t_user
</sql>
<sql id="BASE_COLUMN">
userId, userName, password, phone
</sql>
<insert id="insert" parameterType="com.example.demo.model.UserModel">
INSERT INTO
<include refid="BASE_TABLE" />
<trim prefix="(" suffix=")" suffixOverrides=",">
userName, password,
<if test="phone != null">
phone,
</if>
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
#{userName, jdbcType=VARCHAR}, #{password, jdbcType=VARCHAR},
<if test="phone != null">
#{phone, jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="selectUsers" resultMap="BaseResultMap">
SELECT
<include refid="BASE_COLUMN" />
FROM
<include refid="BASE_TABLE" />
</select>
</mapper>
(7)UserService.java
package com.example.demo.service;
import com.example.demo.model.UserModel;
import com.github.pagehelper.PageInfo;
public interface UserService {
int addUser(UserModel user);
PageInfo<UserModel> findAllUser(int pageNum, int pageSize);
}
(8)UserServiceImpl.java
package com.example.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dao.UserDao;
import com.example.demo.model.UserModel;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int addUser(UserModel user) {
return userDao.insert(user);
}
/**
* pageNum 开始页数
* pageSize 每页显示的数据条数
*/
@Override
public PageInfo<UserModel> findAllUser(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<UserModel> userModel = userDao.selectUsers();
PageInfo<UserModel> result = new PageInfo<UserModel>(userModel);
return result;
}
}
(9)UserController.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.model.UserModel;
import com.example.demo.service.UserService;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@PostMapping("/add")
public int addUser(UserModel user) {
return userService.addUser(user);
}
@ResponseBody
@GetMapping("/all")
public Object findAllUser(
@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
@RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
return userService.findAllUser(pageNum, pageSize);
}
}