spingboot整合mybatis-plus
情况说明:一个简单的
spingboot
整合mybatis-plus
的demo
耗时一天未完成,项目无报错,正常启动,但使用postman请求接口一直404,最终诊断错误原因为spingboot
版本问题,创建项目时使用了spingboot 3
版本,切换为spingboot 2
版本解决问题。本人目前未研究使用
spingboot 3
,所以没深究其错误原因,以后使用到时再进行补充。
spingboot
整合mybatis-plus
本身难度不高,现将所用大致配置列出:
pom文件依赖添加
<dependencies>
<!-- Spring Boot Web Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Druid Spring Boot Starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!--lombok用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
application.yml配置文件
Druid的配置项根据情况增删
server:
port: 8081 #设置请求端口
servlet:
encoding:
charset: UTF-8
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jwt?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
druid:
initial-size: 5 # 初始化大小
min-idle: 10 # 最小连接数
max-active: 20 # 最大连接数
max-wait: 60000 # 获取连接时的最大等待时间
min-evictable-idle-time-millis: 300000 # 一个连接在池中最小生存的时间,单位是毫秒
time-between-eviction-runs-millis: 60000 # 多久才进行一次检测需要关闭的空闲连接,单位是毫秒
filters: stat # 配置扩展插件:stat-监控统计,log4j-日志,wall-防火墙(防止SQL注入),去掉后,监控界面的sql无法统计 ,wall
validation-query: SELECT 1 # 检测连接是否有效的 SQL语句,为空时以下三个配置均无效
test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,默认true,开启后会降低性能
test-on-return: true # 归还连接时执行validationQuery检测连接是否有效,默认false,开启后会降低性能
test-while-idle: true # 申请连接时如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效,默认false,建议开启,不影响性能
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
com.example.mybatis_plus.sys.mapper: debug
实体类
@Data
@TableName("user") //关联数据库表名
public class UserEntity {
@TableId("_id") //数据库主键注解
private Long id;
@TableField("login_name") //属性名与数据库字段名不相符时注解申明
private String loginName;
private String password;
@TableField("nick_name")
private String nickName;
@TableField(exist = false) //数据库查询时忽略该属性
private String sex;
}
业务层-Service
方法一:继承IService<>
接口,其实现类则直接可使用this
调用接口内方法,不用再注入mapper对象
public interface UserService extends IService<UserEntity> {
List<User> queryAll();
}
方法二:直接申明接口
public interface UserService {
List<User> queryAll();
}
实现类-Impl
方法一:继承ServiceImpl<M,T>
类,实现相应业务接口,则直接可使用this
调用接口内方法,不用再注入mapper
对象
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
@Override
public List<User> queryAll() {
return this.list();
}
}
方法二:实现相应业务接口,注入mapper
对象,执行相应操作方法
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryAll() {
return userMapper.selectList(null);
}
}
Dao层
在mapper
接口上可使用@Mapper
注解进行spring
的注册,spring
将动态的生成Bean
后注入到ServiceImpl
中
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {
}
控制层-Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/all")
public List<User> queryAll(){
return userService.queryAll();
}
}
启动类
当一个项目中存在多个Dao
层接口的时候,此时我们需要对每个接口类都写上@Mapper
注解,非常的麻烦,此时可以使用@MapperScan
注解来解决这个问题。让这个接口进行一次性的注入,不需要在写@Mapper
注解,如:
@SpringBootApplication
@MapperScan("longsir.cn.mapper")
public class JwtDmoApplication {
/**
* Java程序的入口函数,用于启动Spring Boot应用程序。
* @param args 命令行参数
*/
public static void main(String[] args) {
SpringApplication.run(JwtDmoApplication.class, args);
}
}