首页 > 其他分享 >spring整合mybatisplus

spring整合mybatisplus

时间:2023-06-04 18:13:34浏览次数:39  
标签:mybatisplus spring plus 整合 bannerMapper mybatis new config

配置文件

  • 注解配置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

相关文章

  • springboot整合mybatis
    整合mybatis基础配置启动类添加@MapperScan配置文件#=================================数据库相关配置====================================spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://10.1.1.1:3306/db?useUnico......
  • Spring Quartz2.x
    Spring3整合Quartz2实现定时任务二:动态添加任务[url]http://www.dexcoder.com/selfly/article/308[/url][color=red]Spring3整合Quartz2实现定时任务三[/color]:动态暂停恢复修改和删除任务[url]http://www.dexcoder.com/selfly/article/311[/url]Quartz的cron表达式[url......
  • spring aop 如何切面到mvc 的controller, service
    [size=large][color=red]Spring+SpringMVC+Mybatis利用AOP自定义注解实现可配置日志快照记录[/color][/size][url]http://unkeltao.com/blog/2014/07/22/spring-plus-springmvc-plus-mybatis-aop/[/url][size=large][color=red]拦截Controller[/color][/si......
  • SpringMVC 3使用Fastjson代替Jackson
    [size=large][color=red]Json解析教程(四.FastJson的使用)[/color][/size][url]http://zyjustin9.iteye.com/blog/2020533[/url]1.[代码][Java]代码publicclassUser{privateLongid;privateStringname;publicLonggetId(){retur......
  • spring mvc 请求转发和重定向
    1.需求背景需求:springMVC框架controller间跳转,需重定向。有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示。本来以为挺简单的一件事情,并且个人认为比较常用的一种方式,一百度全都有了,这些根本不是问题,但是一百度居然出乎我的意料,一堆都不是我想......
  • SpringMVC 转换ajax的json数据乱码问题
    在springmvc3中,已经集成了Jackson(json处理器)来处理数据输出json格式,spring中封装的类是[color=blue]org.springframework.http.converter.json.MappingJackson2HttpMessageConverter[/color]这个json转换器,如果是[color=red]springmvc3.2[/color]之前的版本,可以使用[color=b......
  • Spring MVC 全局的异常处理
    Spring异常处理配置[url]http://panyongzheng.iteye.com/blog/2208146[/url]spring基于注解的全局异常处理方式[url]http://panyongzheng.iteye.com/blog/2067110[/url]使用SpringMVC统一异常处理实战[url]http://panyongzheng.iteye.com/blog/2213655[/url][color=red]怎么......
  • Spring MVC文件上传 文件上传解析 Spring MVC文件上传详解
    首先我要说的是springmvc的核心控制器DispachServlet,这个控制器主要是用来起调度作用,他里面默认就带了一个文件上传的视图解析器,叫multipartResolver,而这个视图解析器SpringMVC又提供了一个默认的实现,叫CommonMultipartResolver,说白了这个实现底层用的就是common-fileupload,......
  • Supporting Spring-WS and Spring MVC integration in a project
    [url]http://www.java-allandsundry.com/2011/06/supporting-spring-ws-and-spring-mvc.html[/url]SpringWSandSpringMVCprovidedifferentfrontcontrollerimplementationsasagatewaytothewebserviceandtheMVCfunctionalityrespectiv......
  • Spring CXF实例
    [b][color=red]参考:[/color][/b]CXFSpring整合——又一个helloword![url]http://pphqq.iteye.com/blog/1447800[/url]一个依赖问题:A:[color=red]如果希望以一种一致的方式实现webservice,特别是有跨语言的需求时,应该使用[b]Axis2[/b][/color]B:[color......