"

在使用ssm框架时候,应该会用到mybatis的Generator用来生成接口,xml映射文件等。自动生成的接口有基本的增删改查功能,但是有个问题,每次生成xml或者接口的时候会覆盖原来的,还有就是自动生成的操作不能够满足业务需求,有些操作要自己写,又不能让自动生成的文件覆盖自己写的。做法就是自己写一个接口继承自动生成的接口,mybatis中不光接口可以继承,xml文件也可以继承。

自动生成的接口GoodMapper.java

package cn.cxnxs.mapper;

import cn.cxnxs.model.po.Goods;

import cn.cxnxs.model.po.GoodsExample;

import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface GoodsMapper {

    long countByExample(GoodsExample example);

    int deleteByExample(GoodsExample example);

    int deleteByPrimaryKey(String id);

    int insert(Goods record);

    int insertSelective(Goods record);

    List<Goods> selectByExample(GoodsExample example);

    Goods selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") Goods record, @Param("example") GoodsExample example);

    int updateByExample(@Param("record") Goods record, @Param("example") GoodsExample example);

    int updateByPrimaryKeySelective(Goods record);

    int updateByPrimaryKey(Goods record);

}

自定义子类接口GoodsDao.java

package cn.cxnxs.dao;

import java.util.List;

import cn.cxnxs.mapper.GoodsMapper;

import cn.cxnxs.model.po.Goods;

public interface GoodsDao extends GoodsMapper{

        //拓展一个新方法

	public List<Goods> selectAllGoods();

}

自动生成的GoodsMapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.cxnxs.mapper.GoodsMapper">

  <resultMap id="BaseResultMap" type="cn.cxnxs.model.po.Goods">

    <id column="id" jdbcType="VARCHAR" property="id" />

    <result column="goods\_name" jdbcType="VARCHAR" property="goodsName" />

    <result column="price" jdbcType="DECIMAL" property="price" />

    <result column="remark" jdbcType="VARCHAR" property="remark" />

    <result column="create\_time" jdbcType="TIMESTAMP" property="createTime" />

    <result column="modify\_time" jdbcType="TIMESTAMP" property="modifyTime" />

  </resultMap>

  <sql id="Example\_Where\_Clause">

    <where>

      <foreach collection="oredCriteria" item="criteria" separator="or">

        <if test="criteria.valid">

          <trim prefix="(" prefixOverrides="and" suffix=")">

            <foreach collection="criteria.criteria" item="criterion">

              <choose>

                <when test="criterion.noValue">

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue">

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue">

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue">

                  and ${criterion.condition}

                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Update\_By\_Example\_Where\_Clause">

    <where>

      <foreach collection="example.oredCriteria" item="criteria" separator="or">

        <if test="criteria.valid">

          <trim prefix="(" prefixOverrides="and" suffix=")">

            <foreach collection="criteria.criteria" item="criterion">

              <choose>

                <when test="criterion.noValue">

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue">

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue">

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue">

                  and ${criterion.condition}

                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Base\_Column\_List">

    id, goods\_name, price, remark, create\_time, modify\_time

  </sql>

  <select id="selectByExample" parameterType="cn.cxnxs.model.po.GoodsExample" resultMap="BaseResultMap">

    select

    <if test="distinct">

      distinct

    </if>

    <include refid="Base\_Column\_List" />

    from ht\_goods

    <if test="\_parameter != null">

      <include refid="Example\_Where\_Clause" />

    </if>

    <if test="orderByClause != null">

      order by ${orderByClause}

    </if>

  </select>

  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">

    select 

    <include refid="Base\_Column\_List" />

    from ht\_goods

    where id = #{id,jdbcType=VARCHAR}

  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">

    delete from ht\_goods

    where id = #{id,jdbcType=VARCHAR}

  </delete>

  <delete id="deleteByExample" parameterType="cn.cxnxs.model.po.GoodsExample">

    delete from ht\_goods

    <if test="\_parameter != null">

      <include refid="Example\_Where\_Clause" />

    </if>

  </delete>

  <insert id="insert" parameterType="cn.cxnxs.model.po.Goods">

    insert into ht\_goods (id, goods\_name, price, 

      remark, create\_time, modify\_time

      )

    values (#{id,jdbcType=VARCHAR}, #{goodsName,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, 

      #{remark,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP}

      )

  </insert>

  <insert id="insertSelective" parameterType="cn.cxnxs.model.po.Goods">

    insert into ht\_goods

    <trim prefix="(" suffix=")" suffixOverrides=",">

      <if test="id != null">

        id,

      </if>

      <if test="goodsName != null">

        goods\_name,

      </if>

      <if test="price != null">

        price,

      </if>

      <if test="remark != null">

        remark,

      </if>

      <if test="createTime != null">

        create\_time,

      </if>

      <if test="modifyTime != null">

        modify\_time,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides=",">

      <if test="id != null">

        #{id,jdbcType=VARCHAR},

      </if>

      <if test="goodsName != null">

        #{goodsName,jdbcType=VARCHAR},

      </if>

      <if test="price != null">

        #{price,jdbcType=DECIMAL},

      </if>

      <if test="remark != null">

        #{remark,jdbcType=VARCHAR},

      </if>

      <if test="createTime != null">

        #{createTime,jdbcType=TIMESTAMP},

      </if>

      <if test="modifyTime != null">

        #{modifyTime,jdbcType=TIMESTAMP},

      </if>

    </trim>

  </insert>

  <select id="countByExample" parameterType="cn.cxnxs.model.po.GoodsExample" resultType="java.lang.Long">

    select count(\*) from ht\_goods

    <if test="\_parameter != null">

      <include refid="Example\_Where\_Clause" />

    </if>

  </select>

  <update id="updateByExampleSelective" parameterType="map">

    update ht\_goods

    <set>

      <if test="record.id != null">

        id = #{record.id,jdbcType=VARCHAR},

      </if>

      <if test="record.goodsName != null">

        goods\_name = #{record.goodsName,jdbcType=VARCHAR},

      </if>

      <if test="record.price != null">

        price = #{record.price,jdbcType=DECIMAL},

      </if>

      <if test="record.remark != null">

        remark = #{record.remark,jdbcType=VARCHAR},

      </if>

      <if test="record.createTime != null">

        create\_time = #{record.createTime,jdbcType=TIMESTAMP},

      </if>

      <if test="record.modifyTime != null">

        modify\_time = #{record.modifyTime,jdbcType=TIMESTAMP},

      </if>

    </set>

    <if test="\_parameter != null">

      <include refid="Update\_By\_Example\_Where\_Clause" />

    </if>

  </update>

  <update id="updateByExample" parameterType="map">

    update ht\_goods

    set id = #{record.id,jdbcType=VARCHAR},

      goods\_name = #{record.goodsName,jdbcType=VARCHAR},

      price = #{record.price,jdbcType=DECIMAL},

      remark = #{record.remark,jdbcType=VARCHAR},

      create\_time = #{record.createTime,jdbcType=TIMESTAMP},

      modify\_time = #{record.modifyTime,jdbcType=TIMESTAMP}

    <if test="\_parameter != null">

      <include refid="Update\_By\_Example\_Where\_Clause" />

    </if>

  </update>

  <update id="updateByPrimaryKeySelective" parameterType="cn.cxnxs.model.po.Goods">

    update ht\_goods

    <set>

      <if test="goodsName != null">

        goods\_name = #{goodsName,jdbcType=VARCHAR},

      </if>

      <if test="price != null">

        price = #{price,jdbcType=DECIMAL},

      </if>

      <if test="remark != null">

        remark = #{remark,jdbcType=VARCHAR},

      </if>

      <if test="createTime != null">

        create\_time = #{createTime,jdbcType=TIMESTAMP},

      </if>

      <if test="modifyTime != null">

        modify\_time = #{modifyTime,jdbcType=TIMESTAMP},

      </if>

    </set>

    where id = #{id,jdbcType=VARCHAR}

  </update>

  <update id="updateByPrimaryKey" parameterType="cn.cxnxs.model.po.Goods">

    update ht\_goods

    set goods\_name = #{goodsName,jdbcType=VARCHAR},

      price = #{price,jdbcType=DECIMAL},

      remark = #{remark,jdbcType=VARCHAR},

      create\_time = #{createTime,jdbcType=TIMESTAMP},

      modify\_time = #{modifyTime,jdbcType=TIMESTAMP}

    where id = #{id,jdbcType=VARCHAR}

  </update>

</mapper>

自定义的GoodsDao.xml (和父配置GoodsMapper.xml放同一个包里)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.cxnxs.dao.GoodsDao">

  <resultMap id="BaseResultMap" type="cn.cxnxs.model.po.Goods">

    <id column="id" jdbcType="VARCHAR" property="id" />

    <result column="goods\_name" jdbcType="VARCHAR" property="goodsName" />

    <result column="price" jdbcType="DECIMAL" property="price" />

    <result column="remark" jdbcType="VARCHAR" property="remark" />

    <result column="create\_time" jdbcType="TIMESTAMP" property="createTime" />

    <result column="modify\_time" jdbcType="TIMESTAMP" property="modifyTime" />

  </resultMap>

  

  <select id="selectAllGoods" resultMap="BaseResultMap">

    select 

  		\*

    from ht\_goods

  </select>

</mapper>

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

	xmlns:context="http://www.springframework.org/schema/context"

	xmlns:mvc="http://www.springframework.org/schema/mvc"

	xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:aop="http://www.springframework.org/schema/aop"

	xsi:schemaLocation="http://www.springframework.org/schema/beans  

                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  

                        http://www.springframework.org/schema/context  

                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  

                        http://www.springframework.org/schema/mvc  

                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  

                        http://www.springframework.org/schema/tx  

                        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 

                        http://www.springframework.org/schema/aop  

                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 引入配置文件 -->

	<bean id="propertyConfigurer"

		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

		<property name="location" value="classpath:jdbc.properties" />

	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

		destroy-method="close">

		<property name="driverClassName" value="${driver}" />

		<property name="url" value="${url}" />

		<property name="username" value="${username}" />

		<property name="password" value="${password}" />

		<!-- 初始化连接大小 -->

		<property name="initialSize" value="${initialSize}"></property>

		<!-- 连接池最大数量 -->

		<property name="maxActive" value="${maxActive}"></property>

		<!-- 连接池最大空闲 -->

		<property name="maxIdle" value="${maxIdle}"></property>

		<!-- 连接池最小空闲 -->

		<property name="minIdle" value="${minIdle}"></property>

		<!-- 获取连接最大等待时间 -->

		<property name="maxWait" value="${maxWait}"></property>

	</bean>

	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

		<property name="dataSource" ref="dataSource" />

		<!-- 自动扫描mapping.xml文件 -->

		<property name="mapperLocations" value="classpath:cn/cxnxs/mapper/\*.xml"></property>

	</bean>

	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

		<property name="basePackage" value="cn.cxnxs.dao" />

		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

	</bean>

	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->

	<bean id="transactionManager"

		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

		<property name="dataSource" ref="dataSource" />

	</bean>

	<!-- 开启事务注解 -->

	<tx:annotation-driven proxy-target-class="false" transaction-manager="appTransactionManager" />

</beans>

写个service用来测试

package cn.cxnxs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import cn.cxnxs.dao.GoodsDao;

import cn.cxnxs.model.po.Goods;

@Service

public class GoodsService {

	

	@Autowired

	private GoodsDao goodsDao;

	

	//子类中的方法

	public List<Goods> selectAll(){

		return goodsDao.selectAllGoods();

	}

	//继承自父类的方法

	public Goods selectByPrimaryKey(String id){

		return goodsDao.selectByPrimaryKey(id);

	}

}

package cn.cxnxs.service;

import java.math.BigDecimal;

import java.util.Date;

import java.util.List;

import org.apache.log4j.Logger;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.cxnxs.model.po.Goods;

import cn.cxnxs.util.IdGenerator;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:spring-application.xml", "classpath:spring-mvc.xml",

		"classpath:spring-mybatis.xml" })

public class GoodsServiceTest {

	@Autowired

	GoodsService goodsService;

	@Autowired

	IdGenerator idGenerator;

	Logger logger = Logger.getLogger(GoodsServiceTest.class);

	@Test

	public void testSelect() {

	        

	        //调用子类的方法

		List<Goods> goodsList = goodsService.selectAll();

		logger.debug(goodsList);

		

		//调用父类的方法

		Goods goods=goodsService.selectByPrimaryKey("995581139663978496");

		  logger.debug(goods.getRemark());

	}

}

运行结果:

![""QQ截图20180513230913.jpg""/](""{#ZC_BLOG_HOST#}zb_users/upload/2018/05/201805131526224046655906.jpg"" """QQ截图20180513230913.jpg""")从后台打印信息看出调用子类自定义方法的时候是调用的GoodsDao里的,调用父"