SpringBoot整合Mybatis-Plus
标签:
SpringBoot
这篇文章介绍一个SpringBoot整合Mybatis-Plus,提供一个小的Demo供大家参考。
已经很久没有写文章了,最近家里有点事刚刚处理完,顺便也趁机休息了一段时间。刚回到公司看了一下码云,发现本期码云封面人员就是Mybatis-Plus团队苞米豆的负责人,如下图。
image
忽然想到,正好之前别人跟我说过怎么不出一个SpringBoot整合Mybatis-Plus的,已经很久的事了,正好想起来,这次就弄一个整合的Demo。
言归正传,新建一个项目。pom文件中加入Mybatis依赖,完整pom如下:
<?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> <groupId>com.dalaoyang</groupId> <artifactId>springboot_mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_mybatisplus</name> <description>springboot_mybatisplus</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
配置文件配置数据库配置和对应Mybatis-Plus实体信息,配置如下:
##端口号server.port=8888##数据库urlspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false##数据库用户名spring.datasource.username=root##数据库密码spring.datasource.password=root##数据库驱动spring.datasource.driver-class-name=com.mysql.jdbc.Driver##日志级别logging.level.com.dalaoyang.dao.UserMapper=debug##mybatis-plus mapper xml 文件地址mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml##mybatis-plus type-aliases 文件地址mybatis-plus.type-aliases-package=com.dalaoyang.entity
实体类User代码如下:
package com.dalaoyang.entity;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.entity * @email yangyang@dalaoyang.cn * @date 2018/7/20 */public class User { private int id; private String user_name; private String user_password; public User() { } public User(String user_name, String user_password) { this.user_name = user_name; this.user_password = user_password; } public User(int id, String user_name, String user_password) { this.id = id; this.user_name = user_name; this.user_password = user_password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } }
下面要说的都是需要注意的地方,新增一个MybatisPlus配置类,其中没有做过多的设置,只是设置了一下方言,代码如下:
package com.dalaoyang.config;import com.baomidou.mybatisplus.plugins.PaginationInterceptor;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.config * @email yangyang@dalaoyang.cn * @date 2018/7/20 */@Configurationpublic class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor(){ PaginationInterceptor page = new PaginationInterceptor(); //设置方言类型 page.setDialectType("mysql"); return page; } }
UserMapper继承了MybatisPlus的BaseMapper,这里面列举一个普通的查询方法getUserList,完整代码如下:
package com.dalaoyang.dao;import com.baomidou.mybatisplus.mapper.BaseMapper;import com.dalaoyang.entity.User;import org.apache.ibatis.annotations.Mapper;import java.util.List;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.dao * @email yangyang@dalaoyang.cn * @date 2018/7/20 */@Mapperpublic interface UserMapper extends BaseMapper<User> { List<User> getUserList(); }
新建一个UserMapper.xml,里面写getUserList对应sql,代码如下:
<?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.dalaoyang.dao.UserMapper"> <resultMap id="user" type="com.dalaoyang.entity.User"/> <parameterMap id="user" type="com.dalaoyang.entity.User"/> <select id="getUserList" resultMap="user"> SELECT * FROM USER </select></mapper>
最后和往常一样,新建一个Controller进行测试,完整代码如下:
package com.dalaoyang.controller;import com.baomidou.mybatisplus.mapper.EntityWrapper;import com.baomidou.mybatisplus.plugins.Page;import com.dalaoyang.dao.UserMapper;import com.dalaoyang.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/7/20 */@RestControllerpublic class UserController { @Autowired private UserMapper userDao; //http://localhost:8888/getUserList @GetMapping("getUserList") public List<User> getUserList(){ return userDao.getUserList(); } //http://localhost:8888/getUserListByName?userName=xiaoli //条件查询 @GetMapping("getUserListByName") public List<User> getUserListByName(String userName) { Map map = new HashMap(); map.put("user_name", userName); return userDao.selectByMap(map); } //http://localhost:8888/saveUser?userName=xiaoli&userPassword=111 //保存用户 @GetMapping("saveUser") public String saveUser(String userName,String userPassword) { User user = new User(userName,userPassword); Integer index = userDao.insert(user); if(index>0){ return "新增用户成功。"; }else{ return "新增用户失败。"; } } //http://localhost:8888/updateUser?id=5&userName=xiaoli&userPassword=111 //修改用户 @GetMapping("updateUser") public String updateUser(Integer id,String userName,String userPassword) { User user = new User(id,userName,userPassword); Integer index = userDao.updateById(user); if(index>0){ return "修改用户成功,影响行数"+index+"行。"; }else{ return "修改用户失败,影响行数"+index+"行。"; } } //http://localhost:8888/getUserById?userId=1 //根据Id查询User @GetMapping("getUserById") public User getUserById(Integer userId) { return userDao.selectById(userId); } //http://localhost:8888/getUserListByPage?pageNumber=1&pageSize=2 //条件分页查询 @GetMapping("getUserListByPage") public List<User> getUserListByPage(Integer pageNumber,Integer pageSize) { Page<User> page =new Page<>(pageNumber,pageSize); EntityWrapper<User> entityWrapper = new EntityWrapper<>(); entityWrapper.eq("user_name", "xiaoli"); return userDao.selectPage(page,entityWrapper); } }
作者:dalaoyang
链接:https://www.jianshu.com/p/8bc02304618b
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦