这篇文章介绍一个SpringBoot整合Mybatis-Plus,提供一个小的Demo供大家参考。
已经很久没有写文章了,最近家里有点事刚刚处理完,顺便也趁机休息了一段时间。刚回到公司看了一下码云,发现本期码云封面人员就是Mybatis-Plus团队苞米豆的负责人,如下图。
image
忽然想到,正好之前别人跟我说过怎么不出一个SpringBoot整合Mybatis-Plus的,已经很久的事了,正好想起来,这次就弄一个整合的Demo。
言归正传,新建一个项目。pom文件中加入Mybatis依赖,完整pom如下:
4.0.0 com.dalaoyang springboot_mybatisplus0.0.1-SNAPSHOT jar springboot_mybatisplus springboot_mybatisplus org.springframework.boot spring-boot-starter-parent1.5.9.RELEASE UTF-8 UTF-8 1.8 com.baomidou mybatisplus-spring-boot-starter1.0.5 com.baomidou mybatis-plus2.3 org.mybatis.spring.boot mybatis-spring-boot-starter1.3.1 org.springframework.boot spring-boot-devtoolsruntime mysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-maven-plugin
配置文件配置数据库配置和对应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*:mapperpublic 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;@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;@Mapperpublic interface UserMapper extends baseMapper{ List getUserList(); }
新建一个UserMapper.xml,里面写getUserList对应sql,代码如下:
SELECT * FROM USER
最后和往常一样,新建一个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;@RestControllerpublic class UserController { @Autowired
private UserMapper userDao; //http://localhost:8888/getUserList
@GetMapping("getUserList") public List getUserList(){ return userDao.getUserList();
} //http://localhost:8888/getUserListByName?userName=xiaoli
//条件查询
@GetMapping("getUserListByName") public List 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 getUserListByPage(Integer pageNumber,Integer pageSize)
{
Page page =new Page<>(pageNumber,pageSize);
EntityWrapper entityWrapper = new EntityWrapper<>();
entityWrapper.eq("user_name", "xiaoli"); return userDao.selectPage(page,entityWrapper);
}
}
作者:dalaoyang
链接:https://www.jianshu.com/p/8bc02304618b



