• Mybatis Plus


    添加依赖:

            <!--使用mybatisplus代码生成器   自己加spring boot依赖工具包哦-->
            <dependency>
                <artifactId>velocity</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>commons-collections</artifactId>
                        <groupId>commons-collections</groupId>
                    </exclusion>
                </exclusions>
                <groupId>org.apache.velocity</groupId>
                <version>1.7</version>
            </dependency>
            <!--导入mybatis plus依赖包-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>2.1.9</version>
            </dependency>

    自定义生成模板引擎:

    
    
    package com.aaa.util;

    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.InjectionConfig;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
    import com.baomidou.mybatisplus.generator.config.rules.DbType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

    import java.util.Collections;

    /**
    * @Author: cws
    * @Date: 2020/7/9 0009 15:10
    * @Version 1.0
    * 使用mybatisplus自动生成代码
    */
    public class MyGenerator {
    public static void main(String[] args) {

    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    // 全局配置
    GlobalConfig globalConfig = new GlobalConfig();
    //输出目录
    globalConfig.setOutputDir("D://生成代码//");
    // 是否覆盖文件
    globalConfig.setFileOverride(true);
    // 开启 activeRecord 模式
    globalConfig.setActiveRecord(true);
    // XML 二级缓存
    globalConfig.setEnableCache(false);
    // XML ResultMap
    globalConfig.setBaseResultMap(true);
    // XML columList
    globalConfig.setBaseColumnList(true);
    //作者
    globalConfig.setAuthor("cws");
    // 自定义文件命名,注意 %s 会自动填充表实体属性! 例:表名User 则是UserDao
    globalConfig.setMapperName("%sDao");
    globalConfig.setXmlName("%sDao");
    globalConfig.setServiceName("%sService");
    globalConfig.setServiceImplName("%sServiceImpl");
    globalConfig.setControllerName("%sController");
    mpg.setGlobalConfig(globalConfig);
    DataSourceConfig sourceConfig = new DataSourceConfig();
    // 数据库类型
    sourceConfig.setDbType(DbType.MYSQL);
    sourceConfig.setTypeConvert(new MySqlTypeConvert() {
    // 自定义数据库表字段类型转换【可选】
    @Override
    public DbColumnType processTypeConvert(String fieldType) {
    System.out.println("转换类型:" + fieldType);
    return super.processTypeConvert(fieldType);
    }
    });
    //根据自己的需要改成自己的数据库类型
    sourceConfig.setDriverName("com.mysql.jdbc.Driver");
    sourceConfig.setUsername("root");
    sourceConfig.setPassword("123456");
    sourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/szqy08project?characterEncoding=utf8");
    mpg.setDataSource(sourceConfig);
    // 策略配置
    StrategyConfig strategyConfig = new StrategyConfig();
    // .setCapitalMode(true)// 全局大写命名
    // 此处可以修改为您的表前缀 例:实体tbl_user变成User
    strategyConfig.setTablePrefix(new String[]{"tbl_"});
    // 表名生成策略 生成小驼峰
    strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    mpg.setStrategy(strategyConfig);
    // 包配置
    PackageConfig packageConfig = new PackageConfig();
    // 自定义包路径
    packageConfig.setParent("com.aaa");
    // 这里是控制器包名,默认 web
    packageConfig.setController("controller");
    packageConfig.setMapper("dao");
    packageConfig.setEntity("entity");
    packageConfig.setService("service");
    packageConfig.setServiceImpl("service.impl");
    mpg.setPackageInfo(packageConfig);
    // 注入配置 可配可不配 mapper路径的问题
    InjectionConfig injectionConfig = new InjectionConfig() {
    @Override
    public void initMap() {
    }
    };
    injectionConfig.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig("/templates/mapper.xml.vm") {
    // 自定义输出文件目录
    @Override
    public String outputFile(TableInfo tableInfo) {
    return globalConfig.getOutputDir() + tableInfo.getEntityName() + "Dao.xml";
    }
    }));
    mpg.setCfg(injectionConfig);
    // 执行生成*/
    mpg.execute();
    }
    }
     

    主启动类:

    package com.aaa;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan(basePackages = "com.aaa.dao")   //扫描dao包
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }

     测试类:

    package com.aaa;
    
    import com.aaa.entity.Dept;
    import com.aaa.service.DeptService;
    import com.baomidou.mybatisplus.mapper.EntityWrapper;
    import com.baomidou.mybatisplus.mapper.Wrapper;
    import com.baomidou.mybatisplus.plugins.Page;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class Szqy08PsmsApplicationTests {
        @Autowired
        private DeptService deptService;
    
        @Test
        void saveDept() {
            Dept dept = new Dept();
            dept.setDeptName("学术部");
            deptService.insert(dept);
        }
        @Test
        void deleteDept() {
           /* Dept dept = new Dept();
            dept.setDeptName("学术部");*/
           //按照条件删除
          /*  Wrapper<Dept> wrapper= new EntityWrapper<>();
            wrapper.eq("dept_name","学术部");
            deptService.delete(wrapper);*/
            //按照主键批量删除boolean deleteBatchIds(Collection<? extends Serializable> var1);
    /*        List<Integer> ids= new ArrayList<>();
            ids.add(108);
            ids.add(109);
            deptService.deleteBatchIds(ids);*/
            //安装主键单个删除
           deptService.deleteById(110);
    
        }
    
        @Test
        void updateDept() {
            Dept dept = new Dept();
            dept.setDeptId(111);
            dept.setDeptName("军机处");
            deptService.updateById(dept);
        }
    
        /**
         * 测试排序
         */
        @Test
        void OrderDept() {
            Wrapper<Dept> wrapper= new EntityWrapper<>();
            wrapper.orderBy("order_num",false);
            List<Dept> deptList = deptService.selectList(wrapper);
            for (Dept dept : deptList) {
                System.out.println(dept.toString());
            }
        }
        /**
         * 测试模糊查询
         */
        @Test
        void likeDept() {
            Wrapper<Dept> wrapper= new EntityWrapper<>();
            wrapper.like("dept_name","市场");
            List<Dept> deptList = deptService.selectList(wrapper);
            for (Dept dept : deptList) {
                System.out.println(dept.toString());
            }
        }
        /**
         * 分页查询
         */
        @Test
        void pageDept() {
            /**
             *     public Page(int current, int size) {
             *         super(current, size);
             *         current 当前第几页
             *         size是一页显示多少行
             *         this.records = Collections.emptyList();
             *     }
             */
            Page<Dept> page = new Page(2,3);
            Page<Dept> deptPage = deptService.selectPage(page);
            //从分页结果中提取list集合
            List<Dept> deptList = deptPage.getRecords();
            for (Dept dept : deptList) {
                System.out.println(dept.toString());
            }
        }
    }
  • 相关阅读:
    CodeForces 1059B
    CodeForces 714A
    浅析母函数
    CodeForces 816C 思维
    CodeForces 816B 前缀和
    CodeForces
    Java项目读取resources资源文件路径那点事
    原型模式
    一次给女朋友转账引发我对分布式事务的思考
    连续最大字段和问题
  • 原文地址:https://www.cnblogs.com/cwshuo/p/13511300.html
Copyright © 2020-2023  润新知