配置文件
-
注解配置mapper扫描:@MapperScan("com.bjsxt.mybatisplus_lombok_swagger.mapper")
-
pom
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis plus和springboot整合-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
- application.properties
server.port=8081
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.1.1.1:60607/mybatisplus?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.username =root
spring.datasource.password =xxx
# configuration 不能和 config-location 同时出现
#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml
mybatis-plus.type-aliases-package=com.bjsxt.mybatisplus_lombok_swagger.model
mybatis-plus.config-location=classpath:mybatis-config.xml
#mybatis-plus.configuration.map-underscore-to-camel-case=true
#配置全局默认主键类型
mybatis-plus.global-config.db-config.id-type=auto
#删除标志
#删除是1
mybatis-plus.global-config.db-config.logicdelete-value=1
#未删除是0
mybatis-plus.global-config.db-config.logicnot-delete-value=0
#如果java实体类没加注解@TableLogic,则可以配置这个,推荐这⾥配置
mybatis-plus.global-config.db-config.logic-delete-field=deleted
model
@Data
@TableName(value="banner")//表名映射
public class BannerDO extends Model<BannerDO> {
@Version /*乐观锁*/
private Integer version;
@TableLogic /**/
private Integer deleted;
@TableId(type = IdType.AUTO)
private Integer id;
private String img;
private String url;
private Integer weight;
}
mapper
- 接口
public interface BannerMapper extends BaseMapper<BannerDO> {
List<BannerDO> list();
}
用法
基本用法
@Test /*查询全部数据*/
public void testBannerTest() throws JsonProcessingException {
List<BannerDO> list = bannerService.list();
System.out.println(new ObjectMapper().writeValueAsString(list));
}
bannerMapper.selectOne(new QueryWrapper<BannerDO>().eq("id",1));
bannerMapper.selectCount(null);
BannerDO bannerDO = new BannerDO();
bannerDO.setImg("111");
bannerDO.setUrl("xxxx");
bannerMapper.insert(bannerDO);
Map<String,String> condition = new HashMap<>();
bannerMapper.delete(new QueryWrapper<BannerDO>().eq("url","xxxx"));
System.out.println(bannerMapper.selectList(new QueryWrapper<BannerDO>().select("id","img")));
分页
@Configuration
public class MybatisPlusPageConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//乐观锁
return interceptor;
}
}
/**
* 测试分页
*/
@Test
public void testPage(){
QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("weight",2);
Page<BannerDO> page = new Page<>(1,3);
IPage<BannerDO> iPage = bannerMapper.selectPage(page,queryWrapper);
System.out.println("总条数:"+iPage.getTotal());
System.out.println("总页数:"+iPage.getPages());
System.out.println("数据:"+iPage.getRecords());
}
mybatisplus-generator
pom
<!-- 代码⾃动⽣成依赖 begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- 代码⾃动⽣成依赖 end-->
参考用法:见附件
标签:mybatisplus,spring,plus,整合,bannerMapper,mybatis,new,config From: https://www.cnblogs.com/dzystudy/p/17456023.html