在springBoot项目中,使用Mybatis-Plus集成sql server数据库。
首先引入依赖
<!-- 加入sql server依赖-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<!-- 引入mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
配置springBoot依赖
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://ip:1433;databasename=数据库名
spring.datasource.username=账号
spring.datasource.password=密码
# 开启mybatis-plus的日志,方便查看sql语句的执行
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
配置mybatis-plus的分页
// 因为这是sql server 数据库,因此会和mysql的配置有点不一样,sql server版本不同,也会有些差别
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER2005)); // 高版本使用SQL_SERVER
return mybatisPlusInterceptor;
}
}
实体类
@TableName("zkt_table") // 对应表名
public class Zkt {
@TableId(type = IdType.AUTO) // 采用自增
private Long id;
@TableField(value = "SN")
private String SN;
private String location;
private String ip;
private String type;
@TableField(value = "lastTime")
private String lastTime;
private Integer state = 1;
private String park;
set ,get ...
}
mapper包
public interface ZktMapper extends BaseMapper<Zkt> {
}
service包
public interface ZktService {
Result fuzzy(ZktFuzzyParams zktFuzzyParams);
}
@Service
public class ZktServiceImpl implements ZktService {
@Resource
private ZktMapper zktMapper;
@Override
public Result fuzzy(ZktFuzzyParams zktFuzzyParams) {
LambdaQueryWrapper<Zkt> wrapper = new LambdaQueryWrapper();
if (StringUtils.hasText(zktFuzzyParams.getSN())){
wrapper.like(Zkt::getSN,zktFuzzyParams.getSN());
}
if (StringUtils.hasText(zktFuzzyParams.getPark())){
wrapper.like(Zkt::getPark,zktFuzzyParams.getPark());
}
Page<Zkt> zktPage = zktMapper.selectPage(new Page<>(zktFuzzyParams.getPageNum(), zktFuzzyParams.getPageSize()), wrapper);
return Result.success(zktPage);
}
controller包
@RestController
public class ZktController {
@Resource
private ZktService zktService;
@GetMapping("/fuzzy")
public Result fuzzy(ZktFuzzyParams zktFuzzyParams){
return zktService.fuzzy(zktFuzzyParams);
}
}
致此集成结束,如有不足还请大家斧正。