55.mybatis-plus

mybatis-plus

对mybatis做了增强,但是没有做更改,所以以前mybatis如何使用,现在一样,只不过可以选择使用其他的一些扩展功能来加强开发效率,特别是单表的CRUD。

  1. 提供了内置的分页插件
  2. 提供了基础的mapper以及service封装
  3. 提供了乐观锁的实现方式
  4. 提供了代码构造器的组件

导包

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

通过注解来对类与表的映射

  1. @TableName 在类上面映射表

  2. @TableId 在主键属性上面映射主键字段

  3. @TableField 普通属性对应字段的映射

    @Data
    @TableName("woniu_user")
    public class User {
    	
    	@TableId(value="user_id",type = IdType.AUTO)
    	private Integer id;
    	
    	@TableField("user_name")
    	private String name;
    	
    	@TableField("user_age")
    	private Integer age;
    
    }
    
    

编写mapper继承提供的通用mapper

public interface UserMapper extends BaseMapper<User>{
}

编写service集成通用service

public interface UserService extends IService<User>{
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {	
}

如果要使用分页功能,需要把分页拦截器配置到容器当中

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
   	PaginationInnerInterceptor pg = new PaginationInnerInterceptor(DbType.MYSQL);
    //设置分页溢出回到首页的处理
    pg.setOverflow(true);
    interceptor.addInnerInterceptor(pg);
    return interceptor;
}

业务层

@GetMapping("findByPage")
    public IPage<TbUser> find(){
        QueryWrapper<TbUser> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.ge("id", 1);
        return userMapper.selectPage(new Page<>(1, 2),sectionQueryWrapper);
}

mybatis-plus的特点以及跟mybatis的区别?

​ MyBatis:一种操作数据库的框架,提供一种Mapper类,支持让你用java代码进行增删改查的数据库操作,省去了每次都要手写sql语句的麻烦。但是!有一个前提,你得先在xml中写好sql语句,也是很麻烦的。
​ MP的存在就是为了稍稍弥补Mybatis的不足。在我们使用Mybatis时会发现,每当要写一个业务逻辑的时候都要在DAO层写一个方法,再对应一个SQL,即使是简单的条件查询、即使仅仅改变了一个条件都要在DAO层新增一个方法,针对这个问题,MP就提供了一个很好的解决方案,它可以让我们避免许多重复性的工作。

plus的代码生成器

  1. 导包

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-engine-core</artifactId>
        <version>2.2</version>
    </dependency>
    
  2. 修改生成器参数

    public class GenaratorUtil {
    	
    	public static void main(String[] args) {
    		  // 代码生成器
            AutoGenerator mpg = new AutoGenerator();
    
            // 全局配置
            GlobalConfig gc = new GlobalConfig();
            String projectPath = System.getProperty("user.dir");
            gc.setOutputDir(projectPath + "/src/main/java");
            gc.setAuthor("liwei");
            gc.setOpen(false);
            // gc.setSwagger2(true); 实体属性 Swagger2 注解
            mpg.setGlobalConfig(gc);
    
            // 数据源配置
            DataSourceConfig dsc = new DataSourceConfig();
            dsc.setUrl("jdbc:mysql:///task?charsetEncoding=utf-    
                       8&useSSL=false&ServerTimezone=Asia/shanghai");
            // dsc.setSchemaName("public");
            dsc.setDriverName("com.mysql.cj.jdbc.Driver");
            dsc.setUsername("root");
            dsc.setPassword("123321");
            mpg.setDataSource(dsc);
    
            // 包配置
            PackageConfig pc = new PackageConfig();
            pc.setParent("com.woniu");
            pc.setEntity("entity");
            pc.setMapper("mapper");
            pc.setService("service");
            pc.setServiceImpl("service.impl");
            pc.setController("controller");
            mpg.setPackageInfo(pc);
    
            // 自定义配置
            InjectionConfig cfg = new InjectionConfig() {
                @Override
                public void initMap() {
                    // to do nothing
                }
            };
    
            // 如果模板引擎是 freemarker
            //String templatePath = "/templates/mapper.xml.ftl";
            // 如果模板引擎是 velocity
            String templatePath = "/templates/mapper.xml.vm";
    
            // 自定义输出配置
            List<FileOutConfig> focList = new ArrayList<>();
            // 自定义配置会被优先输出
            focList.add(new FileOutConfig(templatePath) {
                @Override
                public String outputFile(TableInfo tableInfo) {
                   //定义输出xml文件的地址以及名字
                    return projectPath + "/src/main/resources/mapper/"
                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
            });
           
            cfg.setFileOutConfigList(focList);
            mpg.setCfg(cfg);
    
            // 配置模板
            TemplateConfig templateConfig = new TemplateConfig();
    
            templateConfig.setXml(null);
            mpg.setTemplate(templateConfig);
    
            // 策略配置
            StrategyConfig strategy = new StrategyConfig();
            strategy.setNaming(NamingStrategy.underline_to_camel);
            strategy.setColumnNaming(NamingStrategy.underline_to_camel);
            strategy.setEntityLombokModel(true);
            strategy.setRestControllerStyle(true);
            mpg.setStrategy(strategy);
            mpg.setTemplateEngine(new VelocityTemplateEngine());
            mpg.execute();
    	}
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值