pom.xml中加入如下依赖
<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>
application.properties
##端口号
server.port=8888
##数据库url
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=1234
##数据库驱动
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=cn.niit.entity
UserMapper.java接口加上@Mapper这个注解,@Component不加时自动注入时变量报红但似乎并不影响运行(难道是我idea的问题?)
@Mapper
@Component
public interface UserMapper extends BaseMapper<User> {
List<User> getUserList();
}
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="cn.niit.dao.UserMapper">
<resultMap id="user" type="cn.niit.entity.User"/>
<parameterMap id="user" type="cn.niit.entity.User"/>
<select id="getUserList" resultMap="user">
SELECT * FROM USER
</select>
</mapper>
MybatisplusConfig.java配置类加上@Configuration这个注解
@Configuration
public class MybatisplusConfig {
// 分页拦截器
@Bean
public PaginationInterceptor pageInterceptor()
{
PaginationInterceptor paginationInterceptor=new PaginationInterceptor();
paginationInterceptor.setDialectType("mysql");
return paginationInterceptor;
}
}
UserController.java
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping(value = "getUserList")
public List<User> getUserList() {
return userMapper.getUserList();
}
//条件查询
@GetMapping(value = "getUserListByName")
public List<User> getUserListByName(String userName) {
Map map = new HashMap();
map.put("user_name", userName);
return userMapper.selectByMap(map);
}
//根据Id查询User
@GetMapping("getUserById")
public User getUserById(Integer userId) {
return userMapper.selectById(userId);
}
//http://localhost:8888/getUserListByPage?pageNumber=1&pageSize=2
//条件分页查询
@GetMapping(value = "getUserListByPage")
public List<User> getUserListByPage(Integer pageNum, Integer pageSize) {
Page<User> page = new Page<>(pageNum, pageSize);
EntityWrapper<User> entityWrapper = new EntityWrapper<>();
entityWrapper.eq("user_name", "xiaoli");
return userMapper.selectPage(page, entityWrapper);
}
//保存用户
@GetMapping(value = "saveUser")
public String saveUser(String userName, String userPassword) {
User user = new User(userName, userPassword);
Integer insert = userMapper.insert(user);
if (insert > 0) {
return "添加成功";
} else {
return "添加失败";
}
}
//修改用户
@GetMapping(value = "updateUser")
public String updateUser(Integer id,String userName,String userPassword)
{
User user = new User(id, userName, userPassword);
Integer index = userMapper.updateById(user);
if (index>0)
{
return "修改成功";
}else {
return "修改失败";
}
}
}
接口继承BaseMapper
扫描接口
实体类变量要跟表的字段一致,没单独配置一一映射关系
加入的依赖是mybatisplus-spring-boot-starter