创建Springboot项目
引入依赖
把mybatis的依赖换成mybatis-plus依赖,下面是mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
把mybatis的依赖替换成下面的
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
配置文件(application.properties)
spring.datasource.url=jdbc:mysql://127.0.0.1/test_library?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#端口
server.port=11111
#mapper.xml的路径
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# 配置slq打印日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
启动类添加对mapper包扫描的标签
@MapperScan("com.lqp.mybatisplustest.mapper")
也可以在config类里面添加
config
@Configuration
public class MybatisPlusConfig {
/**
* mybatis-plus SQL执行效率插件【生产环境可以关闭】
*/
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
entity
@Data
@TableName("t_user")
public class Users {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String acc;
private String phone;
private String password;
}
mapper
public interface UserMapper extends BaseMapper<Users> {
Users selectUsersById(Integer userId);
}
mapper.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="com.lqp.mybatisplustest.mapper.UserMapper">
</mapper>
service
public interface UserService extends IService<Users> {
Users findUsersById(Integer userId);
}
service实现类
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, Users> implements UserService {
@Override
public Users findUsersById(Integer userId) {
return baseMapper.selectUsersById(userId);
}
}
controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findUsersById")
public Users findUsersById(Integer userId){
return userService.findUsersById(userId);
}
}
这样就好了,整个项目大概是这样,如果有不对的地方欢迎指正