TKmybatis的框架介绍和原理分析及Mybatis新特性

tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效,下面来看看这个框架的基本使用,后面会对相关源码进行分析,感兴趣的同学可以看一下,挺不错的一个工具

实现对员工表的增删改查的代码
java的dao层接口

  1. public interface WorkerMapper extends Mapper<Worker> {
  2. }

xml映射文件

  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="com.jjs.kaiwen.dao.WorkerMapper">
  4. <resultMap id="BaseResultMap" type="com.jjs.kaiwen.model.Worker">
  5. <!--
  6. WARNING - @mbggenerated
  7. -->
  8. <id column="id" jdbcType="INTEGER" property="id" />
  9. <result column="worker_id" jdbcType="VARCHAR" property="workerId" />
  10. <result column="name" jdbcType="VARCHAR" property="name" />
  11. <result column="org_id" jdbcType="INTEGER" property="orgId" />
  12. <result column="status" jdbcType="VARCHAR" property="status" />
  13. <result column="role_id" property="roleId" jdbcType="INTEGER" />
  14. </resultMap>
  15. </mapper>

实体对象

  1. @Table(name = "worker")
  2. public class Worker {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private Integer id;
  6. @Column(name = "worker_id")
  7. private String workerId;
  8. private String name;
  9. @Column(name = "org_id")
  10. private Integer orgId;
  11. private String status;
  12. @Column(name = "role_id")
  13. private Integer roleId;
  14. // getters and setters ...
  15. }

以上就是实现对Worker进行增删改查的所有代码,包括选择性更新、插入、删除等,所有的方法列表如下

这里写图片描述

以后对表字段的添加或修改只需要更改实体对象的注解,不需要修改xml映射文件,如将worker_id改成worker_no

  1. @Column(name = "worker_no")
  2. private String workerNo;

数据源的配置,只需要将org.mybatis.spring.mapper.MapperScannerConfigurer改成tk.mybatis.spring.mapper.MapperScannerConfigurer,然后加一个属性
,也可不加,因为框架提供了默认实现

  1. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  2. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  3. <property name="basePackage" value="com.jjs.zanbi.dao" />
  4. <property name="properties">
  5. <value>
  6. mappers=tk.mybatis.mapper.common.Mapper
  7. </value>
  8. </property>
  9. </bean>

用这个库之后写代码感觉在飞…….如果只是简单的了解此框架到这里就可以了,下面是对框架实现原理的分析

原理的简单分析

此框架为我们实现这些功能所有的改动都在Mapper层面,所有的Mapper都继承了tk.mybatis.mapper.common.Mapper

public interface WorkerMapper extends Mapper<Worker> {}

Mapper接口的声明如下,可以看到Mapper接口实现了所有常用的方法

  1. public interface Mapper<T> extends
  2. BaseMapper<T>,
  3. ExampleMapper<T>,
  4. RowBoundsMapper<T>,
  5. Marker {
  6. }

看一下完整的UML图,太大了,可以用新窗口打开,放大之后再看
这里写图片描述

这里选择一个接口:SelectOneMapper接口,对于源码进行简单分析,此接口声明如下:

  1. public interface SelectOneMapper<T> {
  2. /**
  3. * 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
  4. *
  5. * @param record
  6. * @return
  7. */
  8. @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
  9. T selectOne(T record);
  10. }

@SelectProvider是mybatis3之后提供的,用于灵活的设置sql来源,这里设置了服务提供类和方法,但这个库并没有直接用method指定的方法来返回sql,而是在运行时进行解析的,代码如下

  1. public class BaseSelectProvider extends MapperTemplate {
  2. public String selectOne(MappedStatement ms) {
  3. Class<?> entityClass = getEntityClass(ms);
  4. //修改返回值类型为实体类型
  5. setResultType(ms, entityClass);
  6. StringBuilder sql = new StringBuilder();
  7. sql.append(SqlHelper.selectAllColumns(entityClass));
  8. sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
  9. sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
  10. return sql.toString();
  11. }
  12. }

到这里我们就大概知道了这个库为我们提供便利的原理了,总的来说就是这个库帮我们提供了对表的基本操作的sql,帮我们省了很多工作量,而且维护起来也很方便,否则我们的xml文件动不动就几百行甚至上千行

对源码的探索不能到这里停止,最起码要分析到与另一个框架的整合点

我们知道,mybatis的mapper接口是在启动的时候被框架以JdkProxy的形式封装了的,具体对应的类是MapperFactoryBean,这个类中有一个checkDaoConfig()方法,是从父类继承并重写了该方法,继承结构如下

MapperFactoryBean -> SqlSessionDaoSupport -> DaoSupport

这里的DaoSupport就是spring提供的Dao的抽象,代码如下

  1. public abstract class DaoSupport implements InitializingBean {
  2. // spring 完成属性设置后会调用此方法
  3. @Override
  4. public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
  5. // 这里提供了接口供子类去实现
  6. checkDaoConfig();
  7. // Let concrete implementations initialize themselves.
  8. try {
  9. initDao();
  10. }
  11. catch (Exception ex) {
  12. throw new BeanInitializationException("Initialization of DAO failed", ex);
  13. }
  14. }
  15. protected abstract void checkDaoConfig() throws IllegalArgumentException;
  16. protected void initDao() throws Exception {
  17. }
  18. }

框架自定义的MapperFactoryBean重写了checkDaoConfig()方法,完成对所有sql语句的设置,代码如下

  1. @Override
  2. protected void checkDaoConfig() {
  3. super.checkDaoConfig();
  4. //通用Mapper
  5. if (mapperHelper.isExtendCommonMapper(getObjectType())) {
  6. //这里去处理该类所对应的MappedStatement,封装在helper类中处理
  7. mapperHelper.processConfiguration(getSqlSession().getConfiguration(), getObjectType());
  8. }
  9. }

MapperHelper的processConfiguration方法如下

  1. public void processConfiguration(Configuration configuration, Class<?> mapperInterface) {
  2. String prefix;
  3. if (mapperInterface != null) {
  4. prefix = mapperInterface.getCanonicalName();
  5. } else {
  6. prefix = "";
  7. }
  8. for (Object object : new ArrayList<Object>(configuration.getMappedStatements())) {
  9. if (object instanceof MappedStatement) {
  10. MappedStatement ms = (MappedStatement) object;
  11. //检查这个MappedStatement是否属于此映射对象
  12. if (ms.getId().startsWith(prefix) && isMapperMethod(ms.getId())) {
  13. if (ms.getSqlSource() instanceof ProviderSqlSource) {
  14. //去设置该statement的sql语句
  15. setSqlSource(ms);
  16. }
  17. }
  18. }
  19. }
  20. }

设置sql的逻辑,提供了几种不同类型的sqlsource

  1. public void setSqlSource(MappedStatement ms) throws Exception {
  2. if (this.mapperClass == getMapperClass(ms.getId())) {
  3. throw new RuntimeException("请不要配置或扫描通用Mapper接口类:" + this.mapperClass);
  4. }
  5. Method method = methodMap.get(getMethodName(ms));
  6. try {
  7. //第一种,直接操作ms,不需要返回值
  8. if (method.getReturnType() == Void.TYPE) {
  9. method.invoke(this, ms);
  10. }
  11. //第二种,返回SqlNode
  12. else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {
  13. SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
  14. DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
  15. setSqlSource(ms, dynamicSqlSource);
  16. }
  17. //第三种,返回xml形式的sql字符串
  18. else if (String.class.equals(method.getReturnType())) {
  19. String xmlSql = (String) method.invoke(this, ms);
  20. SqlSource sqlSource = createSqlSource(ms, xmlSql);
  21. //替换原有的SqlSource
  22. setSqlSource(ms, sqlSource);

到这里整个sql的获取流程就分析完了,本人用这个库写过一个小项目,确实节省了开发的工作量,而且DAO层的结构更加清晰简洁了

关于mybatis新特性

从3.4.0开始,mybatis提供对外部表的alias引用方法,多表联合查询就方便多了,我们先看原始的方式是怎样做的

  1. select a.id,a.name,b.bid,b.bname .....
  2. from user a
  3. left join room b
原始的方式是将所有的表字段列出来,再来看用新特性怎样做

  1. select id="selectUsers" resultType="map">
  2. select
  3. <include refid="user_col_sql_id"><property name="alias" value="t1"/>,
  4. <include refid="room_col_sql_id"><property name="alias" value="t2"/>
  5. from user t1
  6. left join room t2
  7. </select>

这里主要就是对基本的sql进行了复用,如果对表进行了修改只要在原始的sql节点修改就可以了,就算是5个表的联合查询,sql也是清晰易懂,维护起来会更轻松

新版本的mybatis对于对象映射也提供了更友好的方式,直接使用外部的ResultMap再加上查询语句中的别名就映射完成了

  1. <resultMap id="workerResultMap" type="com.jjs.kaiwen.model.Worker" extends="BaseResultMap">
  2. <association property="room" columnPrefix="b_" resultMap="com.jjs.kaiwen.dao.OrgMapper.BaseResultMap"/>
  3. </resultMap>

更进一步

敏锐的程序员可能会提出问题,如当多表查询的时候可能会存在字段名称相同的情况,这里的解决方案是给include添加另一个属性

  1. <include refid="user_col_sql_id_with_alias">
  2. <property name="alias" value="t"/>
  3. <property name="prefix" value="t_"/>
  4. </include>

包含prefix的sqlNode如下

  1. <sql id="base_column_with_alias">
  2. ${alias}.ID as ${prefix}ID,
  3. ${alias}.WORKER_ID as ${prefix}WORKER_ID,
  4. ${alias}.NAME as ${prefix}NAME,
  5. ${alias}.ZB_ROLE_ID as ${prefix}ZB_ROLE_ID,
  6. ${alias}.ORG_ID as ${prefix}ORG_ID,
  7. ${alias}.STATUS as ${prefix}STATUS
  8. </sql>

如果说觉得手动写包含alias和prefix的字段麻烦,可以用,mybatis代码生成器的插件的方式实现,我自己写了一个生成器的插件,可以代码再这里,仅供参考

通用Service类

  1. /**
  2. * Created by Kaiwen
  3. */
  4. @Service
  5. public abstract class CommonServiceImpl<T,PK extends Serializable> implements CommonService<T,PK> {
  6. /**
  7. * 泛型注入
  8. */
  9. @Autowired
  10. private Mapper<T> mapper;
  11. public T selectByPrimaryKey(PK entityId) {
  12. return mapper.selectByPrimaryKey(entityId);
  13. }
  14. public int deleteByPrimaryKey(PK entityId) {
  15. return mapper.deleteByPrimaryKey(entityId);
  16. }
  17. public int insert(T record) {
  18. return mapper.insert(record);
  19. }
  20. public int insertSelective(T record) {
  21. return mapper.insertSelective(record);
  22. }
  23. public int updateByPrimaryKeySelective(T record) {
  24. return mapper.updateByPrimaryKeySelective(record);
  25. }
  26. public int updateByPrimaryKey(T record) {
  27. return mapper.updateByPrimaryKey(record);
  28. }
  29. public List<T> selectByExample(Example example) {
  30. return mapper.selectByExample(example);
  31. }
  32. }

注入方式区别

  1. <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  2. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  3. <property name="basePackage" value="com.jjshome.esf.core.dao.school" />
  4. <property name="properties">
  5. <value>
  6. mappers=tk.mybatis.mapper.common.Mapper
  7. </value>
  8. </property>
  9. </bean>
  10. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  11. <property name="basePackage" value="com.jjshome.esf.core.dao.community,com.jjshome.esf.core.dao.hsl"/>
  12. </bean>
<

实体类

  1. package com.jjshome.esf.common.entity.school;
  2. import java.util.Date;
  3. import javax.persistence.*;
  4. @Table(name = "XQ_SCHOOL_AREA")
  5. public class SchoolArea {
  6. /**
  7. * 主键ID
  8. */
  9. @Id
  10. @Column(name = "ID")
  11. private Integer id;
  12. /**
  13. * 城市编码
  14. */
  15. @Column(name = "CITY_CODE")
  16. private String cityCode;
  17. /**
  18. * 学区名称
  19. */
  20. @Column(name = "NAME")
  21. private String name;
  22. /**
  23. * 学区名称拼音
  24. */
  25. @Column(name = "NAME_SPELL")
  26. private String nameSpell;
  27. /**
  28. * 状态,1:正常,0:删除
  29. */
  30. @Column(name = "STATUS")
  31. private Byte status;
  32. /**
  33. * 添加人
  34. */
  35. @Column(name = "CREATE_ID")
  36. private String createId;
  37. @Transient
  38. private Integer primaryCount; //小学数量
  39. @Transient
  40. private Integer middleCount; //初中数量
  41. @Transient
  42. private Integer highCount;//高中数量

TK mybatis Mapper文件内容

  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="com.jjshome.esf.core.dao.school.ISchoolAreaDAO" >
  4. <resultMap id="BaseResultMap" type="com.jjshome.esf.common.entity.school.SchoolArea" >
  5. <!--
  6. WARNING - @mbggenerated
  7. -->
  8. <id column="ID" property="id" jdbcType="INTEGER" />
  9. <result column="CITY_CODE" property="cityCode" jdbcType="VARCHAR" />
  10. <result column="NAME" property="name" jdbcType="VARCHAR" />
  11. <result column="NAME_SPELL" property="nameSpell" jdbcType="VARCHAR" />
  12. <result column="STATUS" property="status" jdbcType="TINYINT" />
  13. <result column="CREATE_ID" property="createId" jdbcType="VARCHAR" />
  14. <result column="CREATE_DATE" property="createDate" jdbcType="TIMESTAMP" />
  15. <result column="UPDATE_ID" property="updateId" jdbcType="VARCHAR" />
  16. <result column="UPDATE_DATE" property="updateDate" jdbcType="TIMESTAMP" />
  17. <result column="CITY_NAME" property="cityName"/>
  18. <result column="PRIMARY_COUNT" property="primaryCount"/>
  19. <result column="MIDDLE_COUNT" property="middleCount"/>
  20. <result column="HIGH_COUNT" property="highCount"/>
  21. </resultMap>
  22. <resultMap id="SchoolDetailArea" type="com.jjshome.esf.common.entity.school.SchoolAreaDetail"
  23. extends="com.jjshome.esf.core.dao.school.ISchoolInfoDAO.SchoolInfo">
  24. <result column="SCHOOL_AREA_NAME" property="schoolAreaName"/>
  25. </resultMap>
  26. <select id="selectByPage" parameterType="map" resultMap="BaseResultMap">
  27. SELECT A.*, C.NAME AS CITY_NAME,
  28. (SELECT COUNT(*) FROM XQ_SCHOOL_INFO B WHERE A.ID=B.AREA_ID AND B.TYPE='553' AND B.STATUS = 1 ) AS PRIMARY_COUNT,
  29. (SELECT COUNT(*) FROM XQ_SCHOOL_INFO B WHERE A.ID=B.AREA_ID AND B.TYPE='554' AND B.STATUS = 1 ) AS MIDDLE_COUNT,
  30. (SELECT COUNT(*) FROM XQ_SCHOOL_INFO B WHERE A.ID=B.AREA_ID AND B.TYPE='555' AND B.STATUS = 1 ) AS HIGH_COUNT
  31. FROM XQ_SCHOOL_AREA A
  32. LEFT JOIN YW_CITY_SETTING C ON A.CITY_CODE = C.CODE
  33. <where>
  34. <if test="name != null and name != '' "> A.NAME LIKE CONCAT('%',#{NAME},'%') </if>
  35. <if test="areaCityCode != null and areaCityCode != '' "> A.CITY_CODE = #{areaCityCode} </if>
  36. <if test="keywords != null and keywords != '' ">
  37. ( A.NAME LIKE CONCAT('%',#{keywords},'%')
  38. )
  39. </if>
  40. </where>
  41. </select>
  42. <select id="selectAreaIdAndKeyWord" parameterType="java.util.Map" resultMap="BaseResultMap">
  43. SELECT
  44. *
  45. FROM
  46. XQ_SCHOOL_AREA
  47. WHERE
  48. 1=1
  49. <if test="cityId != null">
  50. AND CITY_CODE=#{cityId}
  51. </if>
  52. <if test="key != null and key!=''">
  53. AND (NAME like CONCAT(#{key},'%' ) or NAME_SPELL like CONCAT(#{key},'%' ))
  54. </if>
  55. AND
  56. STATUS=1
  57. <if test="pageSize != null">
  58. limit #{pageSize}
  59. </if>
  60. </select>
  61. <!--查询学区详情列表-->
  62. <select id="selectAreaDetailByPage" parameterType="map" resultMap="SchoolDetailArea">
  63. SELECT A.* ,B.NAME AS SCHOOL_AREA_NAME ,C.NAME AS CITY_NAME,D.NAME AS AREA_NAME FROM XQ_SCHOOL_INFO A
  64. LEFT JOIN XQ_SCHOOL_AREA B ON A.AREA_ID = B.ID
  65. LEFT JOIN YW_CITY_SETTING C ON A.CITY_CODE = C.CODE
  66. LEFT JOIN YW_CITY_SETTING D ON A.AREA_CODE = D.CODE
  67. WHERE A.STATUS = 1 AND B.STATUS =1
  68. <if test="areaId != null and areaId.length() &gt; 0"> AND A.AREA_ID = #{areaId} </if>
  69. <if test="typeList != null and typeList.size &gt; 0">
  70. AND
  71. A.TYPE IN
  72. <foreach collection="typeList" item="item" index="index" open="(" close=")" separator=",">
  73. #{item}
  74. </foreach>
  75. </if>
  76. <if test="name != null and name != '' "> AND A.NAME LIKE CONCAT('%',#{name},'%') </if>
  77. </select>
  78. </mapper>

普通mybatisMapper文件

  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="com.jjshome.esf.core.dao.school.ISchoolInfoDAO">
  4. <resultMap id="SchoolInfo" type="com.jjshome.esf.common.entity.school.SchoolInfo">
  5. <id column="ID" property="id"/>
  6. <result column="NAME" property="name"/>
  7. <result column="NAME_SPELL" property="nameSpell"/>
  8. <result column="ALIAS" property="alias"/>
  9. <result column="ALIAS_SPELL" property="aliasSpell"/>
  10. <result column="TYPE" property="type" typeHandler="com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
  11. <result column="AREA_ID" property="areaId"/>
  12. <result column="CITY_CODE" property="cityCode"/>
  13. <result column="AREA_CODE" property="areaCode"/>
  14. <result column="ADDR" property="addr"/>
  15. <result column="START_TIME" property="startTime"/>
  16. <result column="MOTTO" property="motto"/>
  17. <result column="WEB_SITE" property="webSite"/>
  18. <result column="PHONE" property="phone"/>
  19. <result column="FEATURE" property="feature" typeHandler="com.jjshome.esf.core.component.handler.DictValueListTypeHandler"/>
  20. <result column="LNG" property="lng"/>
  21. <result column="LAT" property="lat"/>
  22. <result column="UNIT_PRICE" property="unitPrice"/>
  23. <result column="SALE_PRICE" property="salePrice"/>
  24. <result column="NATURE_TYPE" property="natureType" typeHandler="com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
  25. <result column="NATURE_CITY" property="natureCity" typeHandler="com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
  26. <result column="SCHOOL_DEGREE" property="schoolDegree"/>
  27. <result column="ENROL_DEGREE" property="enrolDegree"/>
  28. <result column="IMG_DEGREE" property="imgDegree"/>
  29. <result column="STATUS" property="status"/>
  30. <result column="CREATE_ID" property="createId"/>
  31. <result column="CREATE_DATE" property="createDate"/>
  32. <result column="UPDATE_ID" property="updateId"/>
  33. <result column="UPDATE_DATE" property="updateDate"/>
  34. <result column="CITY_NAME" property="cityName" />
  35. <result column="AREA_NAME" property="areaName" />
  36. <result column="SCHOOL_DISTRICT_NAME" property="schoolDistrictName" />
  37. <result column="SALE_COUNT" property="saleCount" />
  38. </resultMap>
  39. <sql id="Base_Column_List">
  40. ID,
  41. NAME,
  42. NAME_SPELL,
  43. ALIAS,
  44. ALIAS_SPELL,
  45. TYPE,
  46. AREA_ID,
  47. CITY_CODE,
  48. AREA_CODE,
  49. ADDR,
  50. START_TIME,
  51. MOTTO,
  52. WEB_SITE,
  53. PHONE,
  54. FEATURE,
  55. LNG,
  56. LAT,
  57. UNIT_PRICE,
  58. SALE_PRICE,
  59. NATURE_TYPE,
  60. NATURE_CITY,
  61. SCHOOL_DEGREE,
  62. ENROL_DEGREE,
  63. IMG_DEGREE,
  64. STATUS,
  65. CREATE_ID,
  66. CREATE_DATE,
  67. UPDATE_ID,
  68. UPDATE_DATE,
  69. SALE_COUNT,
  70. SALE_COUNT
  71. </sql>
  72. <select id="selectById" resultMap="SchoolInfo" parameterType="java.lang.Integer">
  73. SELECT
  74. i.*,
  75. yc.NAME as 'CITY_NAME',
  76. ya.NAME as 'AREA_NAME',
  77. xq.NAME as 'SCHOOL_DISTRICT_NAME'
  78. FROM
  79. XQ_SCHOOL_INFO i
  80. LEFT JOIN YW_CITY_SETTING yc ON i.CITY_CODE = yc.CODE
  81. LEFT JOIN YW_CITY_SETTING ya ON i.AREA_CODE = ya.CODE
  82. LEFT JOIN XQ_SCHOOL_AREA xq ON i.AREA_ID = xq.ID
  83. WHERE
  84. i.ID = #{id,jdbcType=INTEGER}
  85. </select>
  86. <delete id="deleteById" parameterType="java.util.Map">
  87. UPDATE
  88. XQ_SCHOOL_INFO
  89. SET
  90. STATUS = 0,
  91. UPDATE_ID = #{updateId},
  92. UPDATE_DATE = NOW()
  93. WHERE
  94. ID = #{id,jdbcType=INTEGER}
  95. </delete>
  96. <delete id="batchDeleteByIds" parameterType="java.util.Map">
  97. UPDATE
  98. XQ_SCHOOL_INFO
  99. SET
  100. STATUS = 0,
  101. UPDATE_ID = #{updateId},
  102. UPDATE_DATE = NOW()
  103. WHERE
  104. ID IN (${ids})
  105. </delete>
  106. <update id="deleteAreaRelation" parameterType="com.jjshome.esf.common.entity.school.SchoolInfo">
  107. update XQ_SCHOOL_INFO
  108. SET AREA_ID = NULL,
  109. UPDATE_DATE = NOW()
  110. WHERE
  111. ID = #{id}
  112. </update>
  113. <insert id="insert" parameterType="com.jjshome.esf.common.entity.school.SchoolInfo">
  114. <selectKey resultType="Integer" keyProperty="id">
  115. SELECT LAST_INSERT_ID()
  116. </selectKey>
  117. INSERT INTO XQ_SCHOOL_INFO
  118. (NAME,
  119. NAME_SPELL,
  120. ALIAS,
  121. ALIAS_SPELL,
  122. TYPE,
  123. AREA_ID,
  124. CITY_CODE,
  125. AREA_CODE,
  126. ADDR,
  127. START_TIME,
  128. MOTTO,
  129. WEB_SITE,
  130. PHONE,
  131. FEATURE,
  132. LNG,
  133. LAT,
  134. UNIT_PRICE,
  135. SALE_PRICE,
  136. NATURE_TYPE,
  137. NATURE_CITY,
  138. SCHOOL_DEGREE,
  139. ENROL_DEGREE,
  140. IMG_DEGREE,
  141. STATUS,
  142. CREATE_ID,
  143. CREATE_DATE,
  144. UPDATE_ID,
  145. UPDATE_DATE)
  146. VALUES
  147. (#{name,jdbcType=VARCHAR},
  148. #{nameSpell,jdbcType=VARCHAR},
  149. #{alias,jdbcType=VARCHAR},
  150. #{aliasSpell,jdbcType=VARCHAR},
  151. #{type,jdbcType=INTEGER},
  152. #{areaId,jdbcType=INTEGER},
  153. #{cityCode,jdbcType=VARCHAR},
  154. #{areaCode,jdbcType=VARCHAR},
  155. #{addr,jdbcType=VARCHAR},
  156. #{startTime,jdbcType=DATE},
  157. #{motto,jdbcType=VARCHAR},
  158. #{webSite,jdbcType=VARCHAR},
  159. #{phone,jdbcType=VARCHAR},
  160. #{feature,jdbcType=VARCHAR},
  161. #{lng,jdbcType=DECIMAL},
  162. #{lat,jdbcType=DECIMAL},
  163. #{unitPrice},
  164. #{salePrice},
  165. #{natureType,jdbcType=INTEGER},
  166. #{natureCity,jdbcType=INTEGER},
  167. #{schoolDegree,jdbcType=INTEGER},
  168. #{enrolDegree,jdbcType=INTEGER},
  169. #{imgDegree,jdbcType=INTEGER},
  170. #{status,jdbcType=TINYINT},
  171. #{createId,jdbcType=VARCHAR},
  172. #{createDate,jdbcType=DATE},
  173. #{updateId,jdbcType=VARCHAR},
  174. #{updateDate,jdbcType=DATE})
  175. </insert>
  176. <insert id="insertSelective" parameterType="com.jjshome.esf.common.entity.school.SchoolInfo">
  177. <selectKey resultType="Integer" keyProperty="id">
  178. SELECT LAST_INSERT_ID()
  179. </selectKey>
  180. INSERT INTO XQ_SCHOOL_INFO
  181. <trim prefix="(" suffix=")" suffixOverrides=",">
  182. <if test="name != null">
  183. NAME,
  184. </if>
  185. <if test="nameSpell != null">
  186. NAME_SPELL,
  187. </if>
  188. <if test="alias != null">
  189. ALIAS,
  190. </if>
  191. <if test="aliasSpell != null">
  192. ALIAS_SPELL,
  193. </if>
  194. <if test="type != null">
  195. TYPE,
  196. </if>
  197. <if test="areaId != null">
  198. AREA_ID,
  199. </if>
  200. <if test="cityCode != null">
  201. CITY_CODE,
  202. </if>
  203. <if test="areaCode != null">
  204. AREA_CODE,
  205. </if>
  206. <if test="addr != null">
  207. ADDR,
  208. </if>
  209. <if test="startTime != null">
  210. START_TIME,
  211. </if>
  212. <if test="motto != null">
  213. MOTTO,
  214. </if>
  215. <if test="webSite != null">
  216. WEB_SITE,
  217. </if>
  218. <if test="phone != null">
  219. PHONE,
  220. </if>
  221. <if test="feature != null">
  222. FEATURE,
  223. </if>
  224. <if test="lng != null">
  225. LNG,
  226. </if>
  227. <if test="lat != null">
  228. LAT,
  229. </if>
  230. <if test="UNIT_PRICE != null">
  231. UNIT_PRICE,
  232. </if>
  233. <if test="SALE_PRICE != null ">
  234. SALE_PRICE,
  235. </if>
  236. <if test="natureType != null">
  237. NATURE_TYPE,
  238. </if>
  239. <if test="natureCity != null">
  240. NATURE_CITY,
  241. </if>
  242. <if test="schoolDegree != null">
  243. SCHOOL_DEGREE,
  244. </if>
  245. <if test="enrolDegree != null">
  246. ENROL_DEGREE,
  247. </if>
  248. <if test="imgDegree != null">
  249. IMG_DEGREE,
  250. </if>
  251. <if test="status != null">
  252. STATUS,
  253. </if>
  254. <if test="createId != null">
  255. CREATE_ID,
  256. </if>
  257. <if test="createDate != null">
  258. CREATE_DATE,
  259. </if>
  260. <if test="updateId != null">
  261. UPDATE_ID,
  262. </if>
  263. <if test="updateDate != null">
  264. UPDATE_DATE,
  265. </if>
  266. </trim>
  267. <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
  268. <if test="name != null">
  269. #{name,jdbcType=VARCHAR},
  270. </if>
  271. <if test="nameSpell != null">
  272. #{nameSpell,jdbcType=VARCHAR},
  273. </if>
  274. <if test="alias != null">
  275. #{alias,jdbcType=VARCHAR},
  276. </if>
  277. <if test="aliasSpell != null">
  278. #{aliasSpell,jdbcType=VARCHAR},
  279. </if>
  280. <if test="type != null">
  281. #{type,jdbcType=INTEGER},
  282. </if>
  283. <if test="areaId != null">
  284. #{areaId,jdbcType=INTEGER},
  285. </if>
  286. <if test="cityCode != null">
  287. #{cityCode,jdbcType=VARCHAR},
  288. </if>
  289. <if test="areaCode != null">
  290. #{areaCode,jdbcType=VARCHAR},
  291. </if>
  292. <if test="addr != null">
  293. #{addr,jdbcType=VARCHAR},
  294. </if>
  295. <if test="startTime != null">
  296. #{startTime,jdbcType=DATE},
  297. </if>
  298. <if test="motto != null">
  299. #{motto,jdbcType=VARCHAR},
  300. </if>
  301. <if test="webSite != null">
  302. #{webSite,jdbcType=VARCHAR},
  303. </if>
  304. <if test="phone != null">
  305. #{phone,jdbcType=VARCHAR},
  306. </if>
  307. <if test="feature != null">
  308. #{feature,jdbcType=VARCHAR},
  309. </if>
  310. <if test="lng != null">
  311. #{lng,jdbcType=DECIMAL},
  312. </if>
  313. <if test="lat != null">
  314. #{lat,jdbcType=DECIMAL},
  315. </if>
  316. <if test="unitPrice ! =null">
  317. #{unitPrice},
  318. </if>
  319. <if test="salePrice">
  320. #{salePrice},
  321. </if>
  322. <if test="natureType != null">
  323. #{natureType,jdbcType=INTEGER},
  324. </if>
  325. <if test="natureCity != null">
  326. #{natureCity,jdbcType=INTEGER},
  327. </if>
  328. <if test="schoolDegree != null">
  329. #{schoolDegree,jdbcType=INTEGER},
  330. </if>
  331. <if test="enrolDegree != null">
  332. #{enrolDegree,jdbcType=INTEGER},
  333. </if>
  334. <if test="imgDegree != null">
  335. #{imgDegree,jdbcType=INTEGER},
  336. </if>
  337. <if test="status != null">
  338. #{status,jdbcType=TINYINT},
  339. </if>
  340. <if test="createId != null">
  341. #{createId,jdbcType=VARCHAR},
  342. </if>
  343. <if test="createDate != null">
  344. #{createDate,jdbcType=DATE},
  345. </if>
  346. <if test="updateId != null">
  347. #{updateId,jdbcType=VARCHAR},
  348. </if>
  349. <if test="updateDate != null">
  350. #{updateDate,jdbcType=DATE},
  351. </if>
  352. </trim>
  353. </insert>
  354. <update id="updateSelective" parameterType="com.jjshome.esf.common.entity.school.SchoolInfo">
  355. UPDATE XQ_SCHOOL_INFO
  356. <set>
  357. <if test="name != null">
  358. NAME=#{name,jdbcType=VARCHAR},
  359. </if>
  360. <if test="nameSpell != null">
  361. NAME_SPELL=#{nameSpell,jdbcType=VARCHAR},
  362. </if>
  363. <if test="alias != null">
  364. ALIAS=#{alias,jdbcType=VARCHAR},
  365. </if>
  366. <if test="aliasSpell != null">
  367. ALIAS_SPELL=#{aliasSpell,jdbcType=VARCHAR},
  368. </if>
  369. <if test="type != null">
  370. TYPE=#{type,jdbcType=INTEGER},
  371. </if>
  372. <if test="type != null">
  373. AREA_ID=#{areaId,jdbcType=INTEGER},
  374. </if>
  375. <if test="cityCode != null">
  376. CITY_CODE=#{cityCode,jdbcType=VARCHAR},
  377. </if>
  378. <if test="areaCode != null">
  379. AREA_CODE=#{areaCode,jdbcType=VARCHAR},
  380. </if>
  381. <if test="addr != null">
  382. ADDR=#{addr,jdbcType=VARCHAR},
  383. </if>
  384. <if test="startTime != null">
  385. START_TIME=#{startTime,jdbcType=DATE},
  386. </if>
  387. <if test="motto != null">
  388. MOTTO=#{motto,jdbcType=VARCHAR},
  389. </if>
  390. <if test="webSite != null">
  391. WEB_SITE=#{webSite,jdbcType=VARCHAR},
  392. </if>
  393. <if test="phone != null">
  394. PHONE=#{phone,jdbcType=VARCHAR},
  395. </if>
  396. <if test="feature != null">
  397. FEATURE=#{feature,jdbcType=VARCHAR},
  398. </if>
  399. <if test="lng != null">
  400. LNG=#{lng,jdbcType=DECIMAL},
  401. </if>
  402. <if test="lat != null">
  403. LAT=#{lat,jdbcType=DECIMAL},
  404. </if>
  405. <if test="salePrice != null">
  406. UNIT_PRICE=#{unitPrice},
  407. </if>
  408. <if test="salePrice != null">
  409. SALE_PRICE=#{salePrice},
  410. </if>
  411. <if test="natureType != null">
  412. NATURE_TYPE=#{natureType,jdbcType=INTEGER},
  413. </if>
  414. <if test="natureCity != null">
  415. NATURE_CITY=#{natureCity,jdbcType=INTEGER},
  416. </if>
  417. <if test="schoolDegree != null">
  418. SCHOOL_DEGREE=#{schoolDegree,jdbcType=INTEGER},
  419. </if>
  420. <if test="enrolDegree != null">
  421. ENROL_DEGREE=#{enrolDegree,jdbcType=INTEGER},
  422. </if>
  423. <if test="imgDegree != null">
  424. IMG_DEGREE=#{imgDegree,jdbcType=INTEGER},
  425. </if>
  426. <if test="status != null">
  427. STATUS=#{status,jdbcType=TINYINT},
  428. </if>
  429. <if test="createId != null">
  430. CREATE_ID=#{createId,jdbcType=VARCHAR},
  431. </if>
  432. <if test="createDate != null">
  433. CREATE_DATE=#{createDate,jdbcType=DATE},
  434. </if>
  435. <if test="updateId != null">
  436. UPDATE_ID=#{updateId,jdbcType=VARCHAR},
  437. </if>
  438. <if test="updateDate != null">
  439. UPDATE_DATE=#{updateDate,jdbcType=DATE},
  440. </if>
  441. <if test="saleCount != null">
  442. SALE_COUNT=#{saleCount},
  443. </if>
  444. </set>
  445. WHERE
  446. ID = #{id,jdbcType=INTEGER}
  447. </update>
  448. <select id="selectList" parameterType="com.jjshome.esf.common.entity.school.SchoolInfo" resultMap="SchoolInfo">
  449. SELECT
  450. <include refid="Base_Column_List" />
  451. FROM
  452. XQ_SCHOOL_INFO
  453. WHERE
  454. STATUS = 1
  455. <if test="areaId != null and areaId != null"> AND AREA_ID = #{areaId} </if>
  456. </select>
  457. <select id="selectSchoolInfoAll" resultMap="SchoolInfo">
  458. SELECT
  459. <include refid="Base_Column_List" />
  460. FROM
  461. XQ_SCHOOL_INFO
  462. WHERE
  463. STATUS = 1
  464. ORDER BY ID DESC
  465. </select>
  466. <select id="selectSchoolInfo" parameterType="com.jjshome.esf.common.model.SchoolInfoSearchModel" resultMap="SchoolInfo">
  467. SELECT
  468. i.*,
  469. yc.NAME as 'CITY_NAME',
  470. ya.NAME as 'AREA_NAME'
  471. FROM
  472. XQ_SCHOOL_INFO i
  473. LEFT JOIN YW_CITY_SETTING yc ON i.CITY_CODE = yc.CODE
  474. LEFT JOIN YW_CITY_SETTING ya ON i.AREA_CODE = ya.CODE
  475. WHERE
  476. i.STATUS = 1
  477. <if test="city != null and city != '' ">
  478. AND i.CITY_CODE=#{city}
  479. </if>
  480. <if test="area != null and area != '' ">
  481. AND i.AREA_CODE=#{area}
  482. </if>
  483. <if test="schoolId != null and schoolId != ''">
  484. AND i.ID=#{schoolId}
  485. </if>
  486. <if test="schoolName != null and schoolName != ''">
  487. AND i.NAME LIKE concat('%',#{schoolName},'%')
  488. </if>
  489. <if test="schoolDistrictId != null and schoolDistrictId != ''">
  490. AND i.AREA_ID=#{schoolDistrictId}
  491. </if>
  492. <if test="schoolType != null and schoolType != '' ">
  493. AND i.TYPE=#{schoolType}
  494. </if>
  495. <if test="key != null and key != '' ">
  496. AND (i.NAME LIKE CONCAT('%',#{key},'%') OR i.ALIAS LIKE CONCAT('%', #{key}, '%'))
  497. </if>
  498. /*priceType == 1:起售价 2:房源售均价*/
  499. <if test="priceType == 1">
  500. <if test="salePriceStart != null and salePriceStart != '' ">
  501. AND SALE_PRICE &gt;= #{salePriceStart}
  502. </if>
  503. <if test="salePriceEnd != null and salePriceEnd != '' ">
  504. AND SALE_PRICE &lt;= #{salePriceEnd}
  505. </if>
  506. </if>
  507. <if test="priceType == 2">
  508. <if test="salePriceStart != null and salePriceStart != '' ">
  509. AND UNIT_PRICE &gt;= #{salePriceStart}
  510. </if>
  511. <if test="salePriceEnd != null and salePriceEnd != '' ">
  512. AND UNIT_PRICE &lt;= #{salePriceEnd}
  513. </if>
  514. </if>
  515. <if test="perfectSituation == 1">
  516. AND SCHOOL_DEGREE = 100
  517. </if>
  518. <if test="perfectSituation == 2">
  519. AND SCHOOL_DEGREE &lt; 100
  520. </if>
  521. ORDER BY ID DESC
  522. </select>
  523. <select id="selectSchoolByNameAndCityAndArea" parameterType="java.util.Map" resultMap="SchoolInfo">
  524. SELECT
  525. <include refid="Base_Column_List" />
  526. FROM
  527. XQ_SCHOOL_INFO
  528. WHERE
  529. STATUS = 1
  530. AND NAME = #{name}
  531. AND CITY_CODE=#{city}
  532. AND AREA_CODE=#{area}
  533. AND TYPE = #{type}
  534. </select>
  535. <select id="selectAreaIdAndKeyWord" parameterType="java.util.Map" resultMap="SchoolInfo">
  536. SELECT
  537. XSI.*,CYCS.NAME AS 'CITY_NAME',AYCS.NAME AS 'AREA_NAME'
  538. FROM
  539. XQ_SCHOOL_INFO XSI
  540. LEFT JOIN YW_CITY_SETTING CYCS ON XSI.CITY_CODE = CYCS.CODE
  541. LEFT JOIN YW_CITY_SETTING AYCS ON XSI.AREA_CODE = AYCS. CODE
  542. WHERE
  543. 1=1
  544. <if test="areaId != null and areaId != ''">
  545. AND XSI.AREA_CODE=#{areaId}
  546. </if>
  547. <if test="key != null and key!=''">
  548. AND (XSI.NAME like CONCAT(#{key},'%' ) or XSI.NAME_SPELL like CONCAT(#{key},'%' ))
  549. </if>
  550. AND
  551. XSI.STATUS=1
  552. <if test="pageSize != null">
  553. limit #{pageSize}
  554. </if>
  555. </select>
  556. <select id="selectAreaIdList" parameterType="map" resultType="integer">
  557. SELECT DISTINCT AREA_ID FROM XQ_SCHOOL_INFO WHERE NAME LIKE CONCAT('%',#{schoolName},'%')
  558. </select>
  559. <select id="selectSchoolList" parameterType="map" resultMap="SchoolInfo">
  560. SELECT
  561. <include refid="Base_Column_List" />
  562. FROM
  563. XQ_SCHOOL_INFO
  564. WHERE
  565. STATUS = 1
  566. <if test="idList != null and idList.size gt 0">
  567. AND ID IN
  568. <foreach collection="idList" item="item" index="index" open="(" close=")" separator=",">
  569. #{item}
  570. </foreach>
  571. </if>
  572. <if test="areaId != null and areaId != null"> AND AREA_ID = #{areaId} </if>
  573. </select>
  574. </mapper>
原文地址:https://www.cnblogs.com/jpfss/p/10882165.html