zoukankan      html  css  js  c++  java
  • spring+mybatis省去重复工作的增删改查编码模式设计---------有自己实现mybatis的需要

    前提:在很多的业务方法上面,很多程序员在SERVICE和DAO层都做着重复的工作,基本上很多时间都浪费在一个基本表的增删改查上边了,所以本次编码模式将避免这种现象,而且保留了每个业务的扩展性

    一、设计类图

    二、代码

      1 package com.qysxy.product.app.common.dao;
      2 
      3 import com.yizhilu.os.core.dao.impl.common.GenericDaoImpl;
      4 import com.yizhilu.os.core.entity.PageEntity;
      5 
      6 import java.util.List;
      7 
      8 /**
      9  * @author fuguangli
     10  * @description 前沿DAO基础实现类
     11  * @Create date:    2017/3/14
     12  * @genericity E 泛型实体类
     13  */
     14 public abstract class AbstractBaseDao<E> extends GenericDaoImpl implements BaseDaoInter<E> {
     15     private String mapperName = "";
     16 
     17     public void setMapperName(String mapperName) {
     18         this.mapperName = mapperName;
     19     }
     20 
     21     public AbstractBaseDao() {
     22     }
     23 
     24 
     25     /**
     26      * 获取单条数据
     27      *
     28      * @param id
     29      * @return
     30      */
     31     @Override
     32     public E getById(Long id) {
     33         return this.selectOne(mapperName + ".getById", id);
     34     }
     35 
     36     /**
     37      * 获取单条数据
     38      *
     39      * @param entity
     40      * @return
     41      */
     42     @Override
     43     public E get(E entity) {
     44         return this.selectOne(mapperName + ".get", entity);
     45     }
     46 
     47     /**
     48      * 查询数据列表,如果需要分页,请设置分页对象,page不为空
     49      *
     50      * @param entity
     51      * @return
     52      */
     53     @Override
     54     public List findList(E entity, PageEntity page) {
     55         return this.queryForListPage(mapperName + ".findList", entity, page);
     56     }
     57 
     58     /**
     59      * 查询所有数据列表
     60      *
     61      * @param entity
     62      * @return
     63      */
     64     @Override
     65     public List findAllList(E entity) {
     66         return this.selectList(mapperName + ".findAllList", entity);
     67     }
     68 
     69 
     70     /**
     71      * 插入数据
     72      *
     73      * @param entity
     74      * @return
     75      */
     76     @Override
     77     public Long insert(E entity) {
     78         return this.insert(mapperName + ".insert", entity);
     79     }
     80 
     81     /**
     82      * 更新数据
     83      *
     84      * @param entity
     85      * @return
     86      */
     87     @Override
     88     public Long update(E entity) {
     89         return this.update(mapperName + ".update", entity);
     90     }
     91 
     92     /**
     93      * 删除数据
     94      *
     95      * @param id
     96      * @return
     97      * @see public int delete(T entity)
     98      */
     99     @Override
    100     public Long deleteById(Long id) {
    101         return this.delete(mapperName + ".deleteById", id);
    102     }
    103 
    104     /**
    105      * 删除数据
    106      *
    107      * @param entity
    108      * @return
    109      */
    110     @Override
    111     public Long delete(E entity) {
    112         return this.delete(mapperName + ".delete", entity);
    113     }
    114 
    115     /**
    116      * 添加数据 代码创建sql语句
    117      *
    118      * @param sql
    119      */
    120     @Override
    121     public int insertBySql(String sql) {
    122         return this.getSqlSession().insert(mapperName + ".insertBySql", sql);
    123     }
    124 
    125     /**
    126      * 更新数据 代码创建sql
    127      *
    128      * @param sql
    129      * @return
    130      */
    131     @Override
    132     public int updateBySql(String sql) {
    133         return this.getSqlSession().update(mapperName + ".updateBySql", sql);
    134     }
    135 
    136     /**
    137      * 删除数据 代码创建sql
    138      *
    139      * @param sql
    140      * @return
    141      */
    142     @Override
    143     public int deleteBySql(String sql) {
    144         return this.getSqlSession().delete(mapperName + ".deleteBySql", sql);
    145     }
    146 
    147     /**
    148      * 查找数据 代码创建sql
    149      *
    150      * @param sql
    151      * @return
    152      */
    153     @Override
    154     public List<E> findBySql(String sql) {
    155         return this.getSqlSession().selectList(mapperName + ".findBySql", sql);
    156     }
    157 }
      1 package com.qysxy.product.app.common.dao;
      2 
      3 import com.yizhilu.os.core.entity.PageEntity;
      4 
      5 import java.util.List;
      6 
      7 /**
      8  * @author fuguangli
      9  * @description 前沿DAO基础接口
     10  * @Create date:    2017/3/6
     11  * @genericity E 泛型实体类
     12  */
     13 public interface BaseDaoInter<E> {
     14 
     15     /**
     16      * 获取单条数据
     17      *
     18      * @param id
     19      * @return
     20      */
     21     E getById(Long id);
     22 
     23     /**
     24      * 获取单条数据
     25      *
     26      * @param entity
     27      * @return
     28      */
     29     E get(E entity);
     30 
     31     /**
     32      * 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<E>());
     33      *
     34      * @param entity
     35      * @return
     36      */
     37     List findList(E entity, PageEntity page);
     38 
     39     /**
     40      * 查询所有数据列表
     41      *
     42      * @param entity
     43      * @return
     44      */
     45     List<E> findAllList(E entity);
     46 
     47 
     48     /**
     49      * 插入数据
     50      *
     51      * @param entity
     52      * @return
     53      */
     54     Long insert(E entity);
     55 
     56     /**
     57      * 更新数据
     58      *
     59      * @param entity
     60      * @return
     61      */
     62     Long update(E entity);
     63 
     64     /**
     65      * 删除数据(一般为逻辑删除,更新del_flag字段为1)
     66      *
     67      * @param id
     68      * @return
     69      * @see int delete(E entity)
     70      */
     71     Long deleteById(Long id);
     72 
     73     /**
     74      * 删除数据(一般为逻辑删除,更新del_flag字段为1)
     75      *
     76      * @param entity
     77      * @return
     78      */
     79     Long delete(E entity);
     80 
     81     /**
     82      * 添加数据 代码创建sql语句
     83      *
     84      * @param sql
     85      */
     86     int insertBySql(String sql);
     87 
     88     /**
     89      * 更新数据 代码创建sql
     90      *
     91      * @param sql
     92      * @return
     93      */
     94     int updateBySql(String sql);
     95 
     96     /**
     97      * 删除数据 代码创建sql
     98      *
     99      * @param sql
    100      * @return
    101      */
    102     int deleteBySql(String sql);
    103 
    104     /**
    105      * 查找数据 代码创建sql
    106      *
    107      * @param sql
    108      * @return
    109      */
    110     List<E> findBySql(String sql);
    111 
    112 }
      1 package com.qysxy.product.app.common.service;
      2 
      3 import com.qysxy.product.app.common.dao.AbstractBaseDao;
      4 import com.qysxy.product.app.common.dao.BaseDaoInter;
      5 import com.yizhilu.os.core.entity.PageEntity;
      6 import org.springframework.beans.factory.annotation.Autowired;
      7 import org.springframework.transaction.annotation.Transactional;
      8 
      9 import java.util.List;
     10 
     11 /**
     12  * @author fuguangli
     13  * @description 前沿基础业务类
     14  * @Create date:    2017/3/6
     15  * @genericity D 泛型DAO接口
     16  * @genericity  E 泛型实体类接口
     17  */
     18 public abstract class AbstractBaseService<D extends BaseDaoInter<E>, E> implements AbstractBaseServiceInter<E> {
     19     /**
     20      * 持久层对象
     21      */
     22     @Autowired
     23     protected D dao;
     24 
     25     /**
     26      * 获取单条数据
     27      *
     28      * @param id
     29      * @return
     30      */
     31     @Override
     32     public E getById(Long id) {
     33         return dao.getById(id);
     34     }
     35 
     36     /**
     37      * 获取单条数据
     38      *
     39      * @param entity
     40      * @return
     41      */
     42     @Override
     43     public E get(E entity) {
     44         return dao.get(entity);
     45     }
     46 
     47     /**
     48      * 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<E>());
     49      *
     50      * @param entity
     51      * @param page
     52      * @return
     53      */
     54     @Override
     55     public List findList(E entity, PageEntity page) {
     56         return dao.findList(entity, page);
     57     }
     58 
     59     /**
     60      * 查询所有数据列表
     61      *
     62      * @param entity
     63      * @return
     64      */
     65     @Override
     66     public List<E> findAllList(E entity) {
     67         return dao.findAllList(entity);
     68     }
     69 
     70     /**
     71      * 插入数据
     72      *
     73      * @param entity
     74      * @return
     75      */
     76     @Override
     77     public Long insert(E entity) {
     78         return dao.insert(entity);
     79     }
     80 
     81     /**
     82      * 更新数据
     83      *
     84      * @param entity
     85      * @return
     86      */
     87     @Override
     88     public Long update(E entity) {
     89         return dao.update(entity);
     90     }
     91 
     92     /**
     93      * 删除数据(一般为逻辑删除,更新del_flag字段为1)
     94      *
     95      * @param id
     96      * @return
     97      * @see int delete(E entity)
     98      */
     99     @Override
    100     public Long deleteById(Long id) {
    101         return dao.deleteById(id);
    102     }
    103 
    104     /**
    105      * 删除数据(一般为逻辑删除,更新del_flag字段为1)
    106      *
    107      * @param entity
    108      * @return
    109      */
    110     @Override
    111     public Long delete(E entity) {
    112         return dao.delete(entity);
    113     }
    114 
    115     /**
    116      * 添加数据 代码创建sql语句
    117      *
    118      * @param sql
    119      */
    120     @Override
    121     public int insertBySql(String sql) {
    122         return dao.insertBySql(sql);
    123     }
    124 
    125     /**
    126      * 更新数据 代码创建sql
    127      *
    128      * @param sql
    129      * @return
    130      */
    131     @Override
    132     public int updateBySql(String sql) {
    133         return dao.updateBySql(sql);
    134     }
    135 
    136     /**
    137      * 删除数据 代码创建sql
    138      *
    139      * @param sql
    140      * @return
    141      */
    142     @Override
    143     public int deleteBySql(String sql) {
    144         return dao.deleteBySql(sql);
    145     }
    146 
    147     /**
    148      * 查找数据 代码创建sql
    149      *
    150      * @param sql
    151      * @return
    152      */
    153     @Override
    154     public List<E> findBySql(String sql) {
    155         return dao.findBySql(sql);
    156     }
    157 }
    package com.qysxy.product.app.common.service;
    
    import com.yizhilu.os.core.entity.PageEntity;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    /**
     * @author fuguangli
     * @description 前沿基础业务接口
     * @Create date:    2017/3/6
     * @genericity E 实体类
     */
    public interface AbstractBaseServiceInter<E> {
    
        /**
         * 获取单条数据
         *
         * @param id
         * @return
         */
        E getById(Long id);
    
        /**
         * 获取单条数据
         *
         * @param entity
         * @return
         */
        E get(E entity);
    
        /**
         * 查询数据列表,如果需要分页,请设置分页对象,如:entity.setPage(new Page<E>());
         *
         * @param entity
         * @return
         */
        List findList(E entity, PageEntity page);
    
        /**
         * 查询所有数据列表
         *
         * @param entity
         * @return
         */
        List<E> findAllList(E entity);
    
    
        /**
         * 插入数据
         *
         * @param entity
         * @return
         */
        Long insert(E entity);
    
        /**
         * 更新数据
         *
         * @param entity
         * @return
         */
        Long update(E entity);
    
        /**
         * 删除数据(一般为逻辑删除,更新del_flag字段为1)
         *
         * @param id
         * @return
         * @see int delete(E entity)
         */
        Long deleteById(Long id);
    
        /**
         * 删除数据(一般为逻辑删除,更新del_flag字段为1)
         *
         * @param entity
         * @return
         */
        Long delete(E entity);
    
        /**
         * 添加数据 代码创建sql语句
         *
         * @param sql
         */
        int insertBySql(String sql);
    
        /**
         * 更新数据 代码创建sql
         *
         * @param sql
         * @return
         */
        int updateBySql(String sql);
    
        /**
         * 删除数据 代码创建sql
         *
         * @param sql
         * @return
         */
        int deleteBySql(String sql);
    
        /**
         * 查找数据 代码创建sql
         *
         * @param sql
         * @return
         */
        List<E> findBySql(String sql);
    
    }

    以上都是公用的接口以及抽象类,接下来上测试接口及测试相关类

     1 package com.qysxy.product.test;
     2 
     3 import com.qysxy.product.app.common.dao.BaseDaoInter;
     4 
     5 /**
     6  * @author fuguangli
     7  * @description 前沿类
     8  * @Create date:    2017/3/14
     9  */
    10 public interface TestDao extends BaseDaoInter<TestData> {
    11 }
     1 package com.qysxy.product.test;
     2 
     3 import com.qysxy.product.app.common.dao.AbstractBaseDao;
     4 import org.springframework.stereotype.Repository;
     5 
     6 /**
     7  * @author fuguangli
     8  * @description 前沿测试类
     9  * @Create date:    2017/3/14
    10  */
    11 @Repository
    12 public class TestDaoImpl extends AbstractBaseDao<TestData> implements TestDao{
    13     public TestDaoImpl() {
    14         //一定要设置父类的命名空间
    15         super.setMapperName("TestDataMapper");
    16     }
    17 }
     1 package com.qysxy.product.test;
     2 
     3 import lombok.Data;
     4 import lombok.EqualsAndHashCode;
     5 
     6 import java.io.Serializable;
     7 
     8 /**
     9  * @author fuguangli
    10  * @description 前沿类
    11  * @Create date:    2017/3/14
    12  */
    13 @Data
    14 @EqualsAndHashCode
    15 public class TestData implements Serializable {
    16     private Long id;
    17     private String name;
    18 }
     1 package com.qysxy.product.test;
     2 
     3 import com.qysxy.product.app.common.service.AbstractBaseServiceInter;
     4 
     5 /**
     6  * @author fuguangli
     7  * @description 前沿类
     8  * @Create date:    2017/3/14
     9  */
    10 
    11 
    12 public interface TestService extends AbstractBaseServiceInter<TestData>{
    13 }
     1 package com.qysxy.product.test;
     2 
     3 import com.qysxy.product.app.common.service.AbstractBaseService;
     4 import org.springframework.stereotype.Service;
     5 
     6 /**
     7  * @author fuguangli
     8  * @description 前沿类
     9  * @Create date:    2017/3/14
    10  */
    11 @Service
    12 public class TestServiceImpl extends AbstractBaseService<TestDao,TestData> implements TestService {
    13 }
    package test;
    
    import com.qysxy.product.test.TestDao;
    import com.qysxy.product.test.TestData;
    import com.qysxy.product.test.TestService;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    /**
     * @author fuguangli
     * @description 前沿模型测试类
     * @Create date:    2017/3/14
     */
    public class ModuleTest extends BaseTest {
        @Autowired
        private TestDao testDao;
    
    
        @Autowired
        private TestService testService;
    
        @Test
        public void ge() {
            TestData testData = new TestData();
            testData.setId(2l);
            TestData testData1 = testService.get(testData);
            System.err.println(testData1);
        }
    }
     1 package test;
     2 
     3 
     4 import org.junit.runner.RunWith;
     5 import org.springframework.test.context.ContextConfiguration;
     6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     7 
     8 /**
     9  * 
    10 * @ClassName: BaseTest 
    11 * @Description: 基础测试类(其他测试类直接继承即可)
    12 * @author SuLong
    13 * @date 2017年1月4日  上午10:54:59
    14  */
    15 @RunWith(SpringJUnit4ClassRunner.class) // 整合
    16 @ContextConfiguration(locations = "classpath:applicationContext.xml") // 加载配置
    17 public class BaseTest {
    18 
    19 }

     mybatis映射文件:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="TestDataMapper">
     4 
     5     <sql id="columns">
     6     id,
     7     name
     8   </sql>
     9     <sql id="properties">
    10     #{id},
    11     #{name}
    12   </sql>
    13     <resultMap id="testDataResult" type="TestData">
    14         <result property="id" column="id"></result>
    15         <result property="name" column="name"></result>
    16     </resultMap>
    17 
    18     <select id="get" resultMap="testDataResult" parameterType="TestData">
    19         SELECT *
    20         FROM test_test
    21         <where>
    22             <if test="id!=null and id!=0">
    23                 and id=#{id}
    24             </if>
    25             <if test="name!=null and name!=''">
    26                and name=#{name}
    27             </if>
    28         </where>
    29     </select>
    30 
    31     <select id="getById" resultMap="testDataResult" parameterType="long">
    32     SELECT *
    33     FROM test_test a
    34     WHERE a.id = #{id}
    35   </select>
    36 
    37 
    38     <insert id="insert" parameterType="TestData">
    39     INSERT INTO test_test(
    40     id,
    41     name
    42     ) VALUES (
    43     #{id},
    44     #{name}
    45     )
    46   </insert>
    47 
    48 
    49     <!--自定义语句-->
    50     <insert id="insertBySql" parameterType="String">
    51     ${value}
    52   </insert>
    53     <delete id="deleteBySql" parameterType="String">
    54     ${value}
    55   </delete>
    56     <update id="updateBySql" parameterType="String">
    57     ${value}
    58   </update>
    59     <select id="findBySql" parameterType="String" resultType="java.lang.Object">
    60     ${value}
    61   </select>
    62 </mapper>

    总结:这样的话,保留了spring的依赖注入,同时,也保留了程序代码的扩展性,而且帮助编程人员实现了基本的增删改查

  • 相关阅读:
    物理-接触力:百科
    物理-二力平衡:百科
    物理-摩擦力:百科
    物理-电磁力/静电力:百科
    物理-重力:百科
    化学-分子间作用力:百科
    物理-分子力:百科
    物理-斥力:百科
    物理-粒子/能量-衰变:百科
    物理-超光速:百科
  • 原文地址:https://www.cnblogs.com/yuan951/p/6552697.html
Copyright © 2011-2022 走看看