zoukankan      html  css  js  c++  java
  • springboot+mybatis项目自动生成

    springboot_data_access_demo基于rapid,根据自定义模版生成的基于mybatis+mysql的数据库访问示例项目。简单配置数据库信息,配置不同的生成策略生成可以直接运行访问数据库的项目,吸取了mybatis generator的动态条件优势,同时又稍有扩展。可以生成简单易懂的sql,支持大部分单表操作,一般情况下不需要自己动手写sql。模板可以根据自己需求做相应的修改(github地址)。

    1、自动生成支持的方法有:

    public interface BaseDaoMapper<T, E> {
        /**
         * 根据 key 查询
         * @param key 查询的id
         * @return
         */
        T getByPrimaryKey(E key);
    
        /**
         * 根据 keyList 查询
         * @param keyList 查询id的集合
         * @return
         */
        List<T> getByPrimaryKeyList(@Param("keyList") List<E> keyList);
    
        /**
         * 根据条件查询
         * @param example 查询条件
         * @return
         */
        T getSingleByPredicate(BaseExample example);
    
        /**
         * 根据条件查询所有
         * @param example 查询条件
         * @return
         */
        List<T> getAllByPredicate(BaseExample example);
    
        /**
         * 根据条件查询
         * @param example 查询条件
         * @return
         */
        int queryCount(BaseExample example);
    
        /**
         * 分页查询(配合pageHelper)
         * @param example 查询条件
         * @return
         */
        List<T> getListByPage(BaseExample example);
    
        /**
         * 根据key更新除了key以外的其他字段
         * @param record
         * @return
         */
        int updateByPrimaryKey(@Param("record") T record);
    
        /**
         * 按条件更新
         * @param record 需要更新的字段
         * @param example 需要满足的条件
         * @return
         */
        int updateByPredicate(@Param("record") T record, @Param("example")BaseExample example);
    
        /**
         * 新增记录
         * @param entity 需要新增的entity
         * @return
         */
        int insert(T entity);
    
        /**
         * 批量新增
         * @param list 批量新增的entity
         * @return
         */
        int batchInsert(@Param("list") List<T> list);
    }

    生成的mapper.xml示例

    2、自己扩展

    public interface UserinfoMapper extends BaseDaoMapper<UserinfoEntity, java.lang.Integer> {
    
    }

    在对应的Mapper里可以自己扩展,然后只需要在对应的 UserinfoExtendMapper.xml 里写自定义的sql 即可。

    3、生成结构

    生成的项目结构:(com-xxx-demo 可以通过生成器自己配置) 

    • mr-xxx-demo-common
    • mr-xxx-demo-dao
    • mr-xxx-demo-model
    • mr-xxx-demo-server
    • mr-xxx-demo-service 

    目前仅支持单个数据库的配置,后续考虑同时支持多个。此项目是以名为Test的数据库生成的示例

      xxxExample支持指定查询列、查询条件、排序字段生成动态sql

    示例: 

        public void test() {
            UserinfoEntity entity1 = new UserinfoEntity();
            entity1.setSex(1);
            entity1.setUsername("zhangsan");
    
            UserinfoEntity entity2 = new UserinfoEntity();
            entity2.setSex(2);
            entity2.setUsername("lisi");
    
    
            BaseExample example = UserinfoExample.builder()
                    //指定查询列为 id,username
                    .includeSelectFieldClause(UserinfoExample.builderSelectFieldCriteria().id().username())
                    //指定查询条件为 id in (5,6)
                    .addCriteria(UserinfoExample.builderCriteria().andIdIn(Lists.newArrayList(5, 6)))
                    //指定 order by id asc
                    .orderByClause(UserinfoExample.buildOrderByCriteria().orderByIdAsc())
                    .build();
    
            BaseExample updateExample = UserinfoExample.builder()
                    .addCriteria(UserinfoExample.builderCriteria().andIdIn(Lists.newArrayList(5, 6)))
                    .build();
    
            userinfoMapper.updateByPredicate(entity1, updateExample);
    
            userinfoMapper.getSingleByPredicate(example);
    
            userinfoMapper.getByPrimaryKey(1);
    
            userinfoMapper.getByPrimaryKeyList(Lists.newArrayList(1, 2, 3));
    
            userinfoMapper.batchInsert(Lists.newArrayList(entity1, entity2));
    
            userinfoMapper.getAllByPredicate(example);
    
            userinfoMapper.getListByPage(example);
    
            userinfoMapper.queryCount(example);
    
            entity1.setId(5);
            entity1.setUsername("lisi - 22222");
            userinfoMapper.updateByPrimaryKey(entity1);
    
            userinfoMapper.insert(entity2);
        }
    View Code
  • 相关阅读:
    程序调试的利器GDB
    Building a Android Development Environment
    手机的串号IMEI/ESN标示位置图解摩托罗拉官方教程
    Linux调试信息输出串口设备号的设置
    git忽略文件提交
    Spring @Transactional事务传播范围以及隔离级别
    自定义springbootstarter
    maven 命令将jar包安装到本地仓库
    oracle 常用命令
    Oracle 用户(user)和模式(schema)的区别
  • 原文地址:https://www.cnblogs.com/mr-yang-localhost/p/9742186.html
Copyright © 2011-2022 走看看