SpringBoot整合Mybatis
2019-11-24 本文已影响0人
ip小怪兽
- JDK 1.8
- Maven 3.6.0
- Spring Boot 2.2.1
- IDEA 2019.1.3
- pageHelper 物理分页插件
- Swagger2
- 构建与前端交互的通用RESTful API 【JsonResponse】
步骤:
-
创建一个Spring Boot工程
image.png -
配置工程结构及JDK版本
image.png -
选择起步依赖
image.png -
查看pom.xml
<?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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-jdbc</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.1.1</version>
</dependency>
<!--MyBatis分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--阿里巴巴Druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 配置application.properties
#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#Druid连接池配置
# 初始化大小
spring.datasource.druid.initial-size=3
#最小连接数
spring.datasource.druid.min-idle=3
#最大连接数
spring.datasource.druid.max-active=10
#连接超时时间
spring.datasource.druid.max-wait=60000
#Mybatis配置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#日志打印(控制台显示SQL语句)
logging.level.com.example.demo.apply.mapper=debug
- 创建数据库表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`username` varchar(50) DEFAULT NULL COMMENT '用户名',
`password` varchar(50) DEFAULT NULL COMMENT '密码',
`truename` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `user`(`username`,`password`,`truename`) VALUES('lishimin','123456','李世民'),('yuefei','123123','岳飞'),('lixunhuan','123456','李寻欢');
- 创建实体类User.Java
package com.example.demo.apply.entity;
import lombok.Data;
/**
* ClassName: com.example.demo.apply.entity.User <br/>
* Description: User实体<br/>
*/
@Data
public class User {
/**
* id
*/
private Long id;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 别名
*/
private String truename;
}
注意:这里使用了Lombok来简化代码
- 创建UserMapper.java接口
package com.example.demo.apply.mapper;
import com.example.demo.apply.entity.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* ClassName: com.example.demo.apply.mapper.UserMapper <br/>
* Description: <br/>
*/
@Mapper
public interface UserMapper {
/**
* 获取所有的User
*
* @return 返回所有的User
*/
List<User> listUsers();
}
- 在src/main/resource包下创建mapper包,并将创建的Mapper文件存放在该包下
<?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.apply.mapper.UserMapper">
<select id="listUsers" resultType="com.example.demo.apply.entity.User">
select * from user
</select>
</mapper>
注意:该UserMapper.xml文件对应UserMapper.java接口
- 创建service层IUserService.java接口
package com.example.demo.apply.service;
import com.example.demo.apply.entity.User;
import com.github.pagehelper.PageInfo;
import java.util.List;
/**
* ClassName: com.example.demo.apply.service.UserService <br/>
* Description: <br/>
*/
public interface IUserService {
/**
* 查询所有User,不带分页
*
* @return 返回Users
*/
List<User> listUsers();
/**
* 查询所有User,带分页
*
* @param pageNum 当前页数
* @param pageSize 每页数量
* @return 返回Users
*/
PageInfo<User> listUsersWithPage(int pageNum, int pageSize);
}
- service层的实现类UserServiceImpl.java
package com.example.demo.apply.service.impl;
import com.example.demo.apply.entity.User;
import com.example.demo.apply.mapper.UserMapper;
import com.example.demo.apply.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* ClassName: com.example.demo.apply.service.impl.UserServiceImpl <br/>
* Description: <br/>
*/
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> listUsers() {
return userMapper.listUsers();
}
@Override
public PageInfo<User> listUsersWithPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.listUsers();
PageInfo<User> pageInfo = PageInfo.of(users);
return pageInfo;
}
}
- 创建web层UserController.java
package com.example.demo.apply.web;
import com.example.demo.apply.entity.User;
import com.example.demo.apply.mapper.UserMapper;
import com.example.demo.apply.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* ClassName: com.example.demo.apply.web.UserController <br/>
* Description: <br/>
*
* @author lhp <br/>
* @date 2019/11/24 14:29 <br/>
*/
@RestController
@RequestMapping("/userController")
public class UserController {
@Autowired
private IUserService userService;
/**
* 查询所有User信息,不带分页
*
* @return 返回所有Users
*/
@GetMapping("/findAll")
public List<User> findAll() {
return userService.listUsers();
}
/**
* 查询所有User信息,带分页
*
* @param pageNum 当前页数
* @param pageSize 每页数量
* @return 所有Users
*/
@PostMapping("/findAllWithPage")
public PageInfo<User> findAllWithPage(@RequestParam int pageNum,
@RequestParam int pageSize) {
return userService.listUsersWithPage(pageNum, pageSize);
}
}
-
用postman测试接口地址
不带分页查询
带分页查询
14.总结
Spring Boot项目可以很容易地整合Mybatis框架,只需要在pom中引入相关的起步依赖,并经过简单的配置就可以快速使用了。