• mybatis 之 DB的频繁修改应对策略


      mybatis 当前最流行的ORM框架,使用中最烦的事情是什么,无疑是需求的更改或者业务闭环思维不研究导致的数据库字段的增加或者删除,增加或删除一个字段需要在xml多个地方做相应的新增或删除字段操作,稍不谨慎,等待的将是一大波Exceptions,俗话说解决办法来源于问题的产生。接下来设计通用service 来解决DB频繁改动,带来的大量工作量。

      核心思路:

      0. mybatis 3.x 中通用mapper的集成使用

      1. 向上通用Service的设计(泛型思维 && 注入通用mapper)

      2. mapper 强制规范 (减少错误参数 && 加速开发)

      mapper 强制规范:

      0. 无级联查询(不是关联查询)的mapper 不写resultMap,如下这些代码坚决不要

     <resultMap id="BaseResultMap" type="com.javazx.batch.po.UserTo">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <result column="user_name" property="userName" jdbcType="VARCHAR"/>
            <result column="sex" property="sex" jdbcType="INTEGER"/>
            <result column="age" property="age" jdbcType="INTEGER"/>
            <result column="address" property="address" jdbcType="VARCHAR"/>
            <result column="create_time" property="createTime" jdbcType="DATE"/>
            <result column="update_time" property="updateTime" jdbcType="DATE"/>
            <result column="status" property="status" jdbcType="INTEGER"/>
            <result column="remark" property="remark" jdbcType="VARCHAR"/>
        </resultMap>

      1. xml 中单表操作相关代码坚决不要,比如最烦人的update ,insert 包含大量代码的代码块。

      2. xml 中仅剩下了下边这些字段集

    <sql id="Base_Column_List">
            id, user_name, sex,
            age, address,
            create_time, update_time, status, remark
        </sql>

      3. xml中sql操作都是关联表操作

      接下来系统说说通用Service的设计思路,以及设计步骤:

      0.控制层层代码变化不大,以前怎么样现在还怎么样:

    @RequestMapping("/test")
        @ResponseBody
        public Object test(Country country){
            List<Country> all = myService.selectCountryByExalple(country);
            logger.info("------------selectAll-----------" + all + "---------------------------");
            return all;
        }

      1.业务层继承通用Service便于调用能够适用任何实体任何表的单表操作功能的方法

      

    public interface MyService extends BaseTestService<Country>
    

       2. 通用Service -- 泛型的应用

    public interface BaseTestService<T>
    

       3. 通用Service实现,几乎实现了所有单表操作

      

    public class BaseTestServiceImpl<T>{
        @Autowired
        protected Mapper<T> mapper;
    
        public Mapper<T> getMapper() {
            return mapper;
        }
        
        public int delete(T entity) {
            return mapper.delete(entity);
        }
        public int delectByExample(Object example){
            return mapper.deleteByExample(example);
        }
        public int deleteByPrimaryKey(Object key){
            return mapper.deleteByPrimaryKey(key);
        }
        
        public int insert(T record){
            return mapper.insert(record);
        }
        public int insertSelective(T record){
            return mapper.insertSelective(record);
        }
        
        public List<T> select(T record){
            return mapper.select(record);
        }
        public List<T> selectAll(){
            return mapper.selectAll();
        }
        public List<T> selectByExalple(Object example){
            return mapper.selectByExample(example);
        }
        public List<T> selectByExampleAndRowBounds(Object example,RowBounds rowBounds){
            return mapper.selectByExampleAndRowBounds(example, rowBounds);
        }
        public T selectByPrimaryKey(Object key){
            return mapper.selectByPrimaryKey(key);
        }
        public List<T> selectByRowBounds(T record,RowBounds rowBounds){
            return mapper.selectByRowBounds(record, rowBounds);
        }
        public int selectCount(T record){
            return mapper.selectCount(record);
        }
        public int selectCountByExample(Object example){
            return mapper.selectCountByExample(example);
        }
        public T selectOne(T record){
            return mapper.selectOne(record);
        }
        
        
        public int updateByExample(T record,Object example){
            return mapper.updateByExample(record, example);
        }
        public int updateByExampleSelective(T record,Object example){
            return mapper.updateByExampleSelective(record, example);
        }
        public int updateByPrimaryKey(T record){
            return mapper.updateByPrimaryKey(record);
        }
        public int updateByprimaryKeySelective(T record){
            return mapper.updateByPrimaryKeySelective(record);
        }
        
    }

      4. 业务Service实现,总体上是通过java代码构建操作数据库条件,然后调用通用Service方法

      

    @Override
        public List<Country> selectCountryByExalple(Country country) {
            Example example = new Example(Country.class);
            Example.Criteria cirteria = example.createCriteria();
            if(StringUtils.isNotBlank(country.getCountryname())){
                cirteria.andLike("countryname", "%" + country.getCountryname() + "%");
            }
            return super.selectByExalple(example);
        }

    其中cirteria中包含了我们几乎能够试用到的所有方法

      第三方参加引用:

    <!--分页插件-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>
            <!--通用Mapper-->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper</artifactId>
                <version>${mapper.version}</version>
            </dependency>

      宗上,通用Service设计完成,简单且方便,实用且低复杂度,假设一个项目30个表,每个表字段适中,每个初始mapper中大约150行代码,使用上述xml规范,每个mapper大约7行代码左右,总代码由4500行减少到210行,这只是一个优点,关键点还是代码最后易维护,易迭代,不惧修改。

  • 相关阅读:
    剑指offer-二叉树的深度
    剑指offer-二叉树中和为某一值的路径
    剑指offer-数组中只出现一次的数字
    剑指offer-反转单词顺序列
    剑指offer-数字在排序数组中出现的次数
    剑指offer-第一个只出现一次的字符
    剑指offer-连续子数组的最大和
    剑指offer-数组中的逆序对
    CSS3滚动条美化,CSS3滚动条皮肤
    mouseover事件与mouseenter事件的区别
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/7127734.html
Copyright © 2020-2023  润新知