Spring Boot整合MyBatis-Plus
连接Mysql数据库
< ! -- mysql -- >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< / dependency>
properties配置数据库信息
# mysql
spring. datasource. url = xxxxxx
spring. datasource. username = xxxxxx
spring. datasource. password = xxxxxx
spring. datasource. driver- class - name = com. mysql. cj. jdbc. Driver
数据库测试成功后,整合MyBatis-Plus
< ! -- mybatis plus-- >
< ! -- mybatis- plus- boot- starter中已经导入了MyBatis - Plus 的依赖-- >
< dependency>
< groupId> com. baomidou< / groupId>
< artifactId> mybatis- plus- boot- starter< / artifactId>
< version> 3.1 .2 < / version>
< / dependency>
在启动类配置扫描mapper文件(也可以在每个mapper文件上加@Mapper注解,相对下面的更方便)
@MapperScan ( basePackages = { "com.xxx.xxx.mapper" } )
public class PracticeApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( PracticeApplication . class , args) ;
}
}
如果你的xml文件放在了resources目录下,还要在POM文件做一下配置,将xml文件打包
< build>
< ! -- 打包resources目录下的xml文件 -- >
< resources>
< resource>
< directory> src/ main/ java< / directory>
< includes>
< include> * *
简单实现
实体类
@Data
@TableName ( "user" )
public class User {
@TableId ( type = IdType . AUTO)
private Long id;
@TableField ( "name" )
private String name;
}
Mapper
public interface UserMapper extends BaseMapper < User > {
}
Mapping文件
< mapper namespace= "com.xxx.xxx.mapper.UserMapper" >
< / mapper>
Service接口
public interface UserService extends IService < User > {
UserInfoDto getUser ( String name) ;
}
ServiceImpl实现
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl < UserMapper , User > implements UserService {
@Override
public UserInfoDto getUser ( String name) {
UserInfoDto userInfoDto = new UserInfoDto ( ) ;
User user = this . baseMapper. selectOne ( new QueryWrapper < User > ( )
. lambda ( )
. eq ( User :: getName , name) ) ;
BeanUtils . copyProperties ( user, userInfoDto) ;
return userInfoDto;
}
}
Swagger调用结果
{
"id" : 1 ,
"name" : "test"
}
Spring Boot整合Swagger-UI实现自动生成接口文档 Spring Boot多数据源的简单配置和使用