zoukankan      html  css  js  c++  java
  • springboot和MybatisPlus整合

    springboot和MybatisPlus整合

    1.介绍
    使用SpringBoot + Mybatis + MybatisPlus(MP) 全新注解方式,自动产生SQL语句,替代旧的通用Mapper。旧的通用Mapper是基于Mybatis拦截器的机制,而新的MybatisPlus是基于注解扫描机制,在启动服务时就进行加载,所以性能比旧的通用Mapper方式高很多。
    MyBatisPlus官网:http://mp.baomidou.com
    2. MybatisPlus注解

    Pojo对象上标识,自动驼峰规则
    @TableName("tb_cart")	//类和表的映射
    public class Cart extends BasePojo{
    	private static final long serialVersionUID = 1L;
    	//@TableField(exist=false)	//数据库表中不存在的字段要标识
    	@TableId(value="id",type=IdType.AUTO)	//主键自增
    	private Long id;
    	@TableField("user_id")	//属性和字段的映射
    	private Long userId;
    	@TableName("tb_cart")						//类和表的映射
    	@TableId(value="id",type=IdType.AUTO)	//主键自增
    	@TableField("user_id")					//属性和字段的映射
    	@TableField(exist=false)					//表中不存在的字段要标识
    

    3.Mybatis提供QBC( Query By Criteria),实现面向对象的查询方式。
    实体包装器 EntityWrapper,用于处理 sql 拼接,排序,实体参数查询等。注意其使用的是数据库字段而不是java属性

    例如:翻页查询
    public Page<T> selectPage(Page<T> page, EntityWrapper<T> entityWrapper) {
      if (null != entityWrapper) {
          entityWrapper.orderBy(page.getOrderByField(), page.isAsc());
      }
      page.setRecords(baseMapper.selectPage(page, entityWrapper));
      return page;
    }
    拼接SQL方式 一:
        EntityWrapper<User> ew = new EntityWrapper<User>();
        ew.setEntity(new User(1));
        ew.where("user_name={0}", "'tonychen'").and("id=1")
                .orNew("user_status={0}", "0").or("status=1")
                .notLike("user_nickname", "tony")
                .andNew("new=xx").like("hhh", "ddd")
                .andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
                .groupBy("x1").groupBy("x2,x3")
                .having("x1=11").having("x3=433")
                .orderBy("name").orderBy("birthday,address");
        System.out.println(ew.getSqlSegment());
    拼接 SQL方式 二:
    int buyCount = selectCount(Condition.create()
                    .setSqlSelect("sum(quantity)")
                    .isNull("order_id")
                    .eq("user_id", 1)
                    .eq("type", 1)
                    .in("status", new Integer[]{0, 1})
                    .eq("product_id", 1)
                    .between("created_time", startDate, currentDate);
    
    

    4.pom.xml

     <!-- mybatisplus与springboot整合 -->
        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatisplus-spring-boot-starter</artifactId>
          <version>1.0.5</version>
        </dependency>
        <!-- MP 核心库 -->
        <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus</artifactId>
          <version>2.1.8</version>
        </dependency>
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-starter</artifactId>
          <version>1.2.3</version>
        </dependency>
    

    5.yml

    server:
      port: 9101
    
    spring:
      application:
        name: oa-user
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/oa566
        username: root
        password: 123456
    mybatis:
      mapUnderscoreToCamelCase: true
      typeAliasesPackage: com.ls.oa.pojo
      mapperLocations: classpath:mappers/*.xml
    
    mybatis-plus:
      configuration:
        map-underscore-to-camel-case: true
    
    logging:
      level:
        com.ls.oa.mapper: debug
    

    5.dao层

    public interface SysUsersMapper extends BaseMapper<SysUsers> {
    
    }
    

    6.实现类

    @Autowired
      private SysUsersMapper sysUsersMapper;
    
      @Override
      public Boolean queryUserName(String userName) {
        EntityWrapper<SysUsers> ew = new EntityWrapper<>();
        SysUsers sysUsers = new SysUsers();
        ew.setEntity(sysUsers);
        sysUsers.setUsername(userName);
        Integer count = sysUsersMapper.selectCount(ew);
        if(count<0){
          return true;
        }
        return false;
      }
    

    6.简单的写到这里,入门级别的,更多请访问MyBatisPlus官网:http://mp.baomidou.com
    7.若报错找出mapper请加的配置
    application.yml

    server:
      port: 8080
    
    #开发配置
    spring:
      profiles: dev
      datasource:
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/oauth2?serverTimezone=GMT%2B8
        username: root
        password: 123456
    ---
    #默认使用配置
    spring:
      profiles:
        active: dev
    mybatis:
      mapUnderscoreToCamelCase: true
      typeAliasesPackage: com.yd.pojo
      mapperLocations: classpath:mappers/*.xml
    mybatis-plus:
        map-underscore-to-camel-case: true
        mapper-locations: classpath:mappers/*.xml
        type-aliases-package: com.yd.pojo
    logging:
      level:
        com.yd.mapper: debug
    
    
  • 相关阅读:
    PyQt作品 – PingTester – 多点Ping测试工具
    关于和技术人员交流的一二三
    Pyjamas Python Javascript Compiler, Desktop Widget Set and RIA Web Framework
    Hybrid Qt applications with PySide and Django
    pyjamas build AJAX apps in Python (like Google did for Java)
    PyQt 维基百科,自由的百科全书
    InfoQ:请问为什么仍要选择Java来处理后端的工作?
    Eric+PyQt打造完美的Python集成开发环境
    python select module select method introduce
    GUI Programming with Python: QT Edition
  • 原文地址:https://www.cnblogs.com/szls-666/p/12494187.html
Copyright © 2011-2022 走看看