springboot与mybatis-plus整合
项目准备
1. 新建项目day63-springboot-03-mybatisplus
2. 点war包
3. 依赖
4. 将base项目下载到自己项目中,然后打成jar包放在自己的maven仓库中
<dependency>
<groupId>com.wpjgroupId>
<artifactId>baseartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
5. 导入mybatis-plus的依赖
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>2.3version>
dependency>
6 设置mysql依赖版本
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
<version>5.1.44version>
dependency>
7. 创建数据库day63-springboot-mybatisplus
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`pwd` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` int(1) DEFAULT NULL COMMENT '0 男 1 女',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'admin', 'admin', '22', '0');
INSERT INTO `user` VALUES ('2', 'wpj', 'wpj', '22', '1');
8. 定义实体类User
package com.wpj.bean;
import com.baomidou.mybatisplus.annotations.*;
import com.baomidou.mybatisplus.enums.*;
import lombok.*;
@Data
@TableName("user")
public class User {
@TableId(type= IdType.AUTO)
private Integer id;
private String name;
private String pwd;
private Integer age;
private Integer sex;
}
9. 定义Mapper
package com.wpj.mapper;
import com.baomidou.mybatisplus.mapper.*;
import com.wpj.bean.*;
public interface IUserMapper extends BaseMapper<User> {
}
10. 定义service以及实现类
package com.wpj.service;
import com.wpj.baseservice.*;
import com.wpj.bean.*;
public interface IUserService extends IBaseService<User> {
}
package com.wpj.service.impl;
import com.baomidou.mybatisplus.mapper.*;
import com.wpj.baseservice.impl.*;
import com.wpj.bean.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
@Autowired
private IUserMapper iUserMapper;
@Override
protected BaseMapper<User> getBaseMapper() {
return null;
}
}
11. 写application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/day63-springboot-mybatisplus
username: root
password: 123456
logging:
level:
com.wpj.mapper: debug
mybatis-plus:
type-aliases-package: com.wpj.bean
12. 配置包扫描
package com.wpj.day63springboot03mybatisplus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.wpj")
@MapperScan(basePackages = "com.wpj.mapper")
public class Day63Springboot03MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(Day63Springboot03MybatisplusApplication.class, args);
}
}
13. 测试环境
1 模拟登陆功能
1.1 mapper定义login方法
public interface IUserMapper extends BaseMapper<User> {
User login(@Param("name") String username, @Param("pwd") String password);
}
1.2 service定义login方法,impl重写该方法
public interface IUserService extends IBaseService<User> {
User login(String username, String password);
}
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
@Autowired
private IUserMapper iUserMapper;
@Override
protected BaseMapper<User> getBaseMapper() {
return iUserMapper ;
}
@Override
public User login(String username, String password) {
return iUserMapper.login(username,password);
}
}
1.3 新建mapper对应映射文件
<mapper namespace="com.wpj.mapper.IUserMapper">
<select id="login" resultType="user">
select * from user where name = #{name} and pwd = #{pwd}
select>
mapper>
1.4 扫描mapper文件
mybatis-plus:
type-aliases-package: com.wpj.bean
mapper-locations: classpath:mapper/*.xml
1.5 测试功能
2. 设置分页sql
2.1 定义配置类给spring容器添加分页插件
package com.wpj.config;
import com.baomidou.mybatisplus.plugins.*;
import org.springframework.context.annotation.*;
@Configuration
public class PageConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
2.2 测试功能