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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM