SpringBoot整合mybatis-plus入门

pom.xml中加入如下依赖

  
            com.baomidou
            mybatisplus-spring-boot-starter
            1.0.5
        
        
            com.baomidou
            mybatis-plus
            2.3
        
复制代码

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 {
     List getUserList();

}
复制代码

UserMapper.xml:接口对应的配置

"1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"cn.niit.dao.UserMapper">
    "user" type="cn.niit.entity.User"/>
    "user" type="cn.niit.entity.User"/>

    

复制代码

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 getUserList() {
        return userMapper.getUserList();
    }

    //条件查询
    @GetMapping(value = "getUserListByName")
    public List 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 getUserListByPage(Integer pageNum, Integer pageSize) {
        Page page = new Page<>(pageNum, pageSize);
        EntityWrapper 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

个人网站

你可能感兴趣的:(SpringBoot整合mybatis-plus入门)