tkmybatis是在mybatis框架的基礎上提供了很多工具,讓開發更加高效,下面來看看這個框架的基本使用,后面會對相關源碼進行分析,感興趣的同學可以看一下,挺不錯的一個工具
實現對員工表的增刪改查的代碼
java的dao層接口
-
public
interface WorkerMapper extends Mapper<Worker> {
-
}
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="com.jjs.kaiwen.dao.WorkerMapper">
-
<resultMap id="BaseResultMap" type="com.jjs.kaiwen.model.Worker">
-
<!--
-
WARNING - @mbggenerated
-
-->
-
<id column="id" jdbcType="INTEGER" property="id" />
-
<result column="worker_id" jdbcType="VARCHAR" property="workerId" />
-
<result column="name" jdbcType="VARCHAR" property="name" />
-
<result column="org_id" jdbcType="INTEGER" property="orgId" />
-
<result column="status" jdbcType="VARCHAR" property="status" />
-
<result column="role_id" property="roleId" jdbcType="INTEGER" />
-
</resultMap>
-
</mapper>
實體對象
-
@Table(name =
"worker")
-
public
class Worker {
-
@Id
-
@GeneratedValue(strategy = GenerationType.IDENTITY)
-
private Integer id;
-
-
@Column(name =
"worker_id")
-
private String workerId;
-
-
private String name;
-
-
@Column(name =
"org_id")
-
private Integer orgId;
-
-
private String status;
-
-
@Column(name =
"role_id")
-
private Integer roleId;
-
-
// getters and setters ...
-
}
以上就是實現對Worker進行增刪改查的所有代碼,包括選擇性更新、插入、刪除等,所有的方法列表如下
以后對表字段的添加或修改只需要更改實體對象的注解,不需要修改xml映射文件,如將worker_id改成worker_no
-
@Column(name =
"worker_no")
-
private String workerNo;
數據源的配置,只需要將org.mybatis.spring.mapper.MapperScannerConfigurer改成tk.mybatis.spring.mapper.MapperScannerConfigurer,然后加一個屬性
,也可不加,因為框架提供了默認實現
-
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
-
<property name="basePackage" value="com.jjs.zanbi.dao" />
-
<property name="properties">
-
<value>
-
mappers=tk.mybatis.mapper.common.Mapper
-
</value>
-
</property>
-
</bean>
用這個庫之后寫代碼感覺在飛…….如果只是簡單的了解此框架到這里就可以了,下面是對框架實現原理的分析
原理的簡單分析
此框架為我們實現這些功能所有的改動都在Mapper層面,所有的Mapper都繼承了tk.mybatis.mapper.common.Mapper
public interface WorkerMapper extends Mapper<Worker> {}
Mapper接口的聲明如下,可以看到Mapper接口實現了所有常用的方法
-
public
interface Mapper<T> extends
-
BaseMapper
<
T
>,
-
ExampleMapper
<
T
>,
-
RowBoundsMapper
<
T
>,
-
Marker
{
-
-
}
看一下完整的UML圖,太大了,可以用新窗口打開,放大之后再看
這里選擇一個接口:SelectOneMapper接口,對於源碼進行簡單分析,此接口聲明如下:
-
public
interface SelectOneMapper<T> {
-
-
/**
-
* 根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號
-
*
-
*
@param
record
-
*
@return
-
*/
-
@SelectProvider(
type = BaseSelectProvider.
class, method =
"dynamicSQL")
-
T selectOne(T record);
-
-
}
@SelectProvider是mybatis3之后提供的,用於靈活的設置sql來源,這里設置了服務提供類和方法,但這個庫並沒有直接用method指定的方法來返回sql,而是在運行時進行解析的,代碼如下
-
public
class BaseSelectProvider extends MapperTemplate {
-
-
public
String
selectOne
(MappedStatement ms) {
-
Class<?> entityClass = getEntityClass(ms);
-
//修改返回值類型為實體類型
-
setResultType(ms, entityClass);
-
StringBuilder sql =
new StringBuilder();
-
sql.append(SqlHelper.selectAllColumns(entityClass));
-
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
-
sql.append(SqlHelper.whereAllIfColumns(entityClass, isNotEmpty()));
-
return sql.toString();
-
}
-
}
到這里我們就大概知道了這個庫為我們提供便利的原理了,總的來說就是這個庫幫我們提供了對表的基本操作的sql,幫我們省了很多工作量,而且維護起來也很方便,否則我們的xml文件動不動就幾百行甚至上千行
對源碼的探索不能到這里停止,最起碼要分析到與另一個框架的整合點
我們知道,mybatis的mapper接口是在啟動的時候被框架以JdkProxy的形式封裝了的,具體對應的類是MapperFactoryBean,這個類中有一個checkDaoConfig()方法,是從父類繼承並重寫了該方法,繼承結構如下
MapperFactoryBean -> SqlSessionDaoSupport -> DaoSupport
這里的DaoSupport就是spring提供的Dao的抽象,代碼如下
-
public
abstract
class DaoSupport implements InitializingBean {
-
-
// spring 完成屬性設置后會調用此方法
-
@Override
-
public
final
void
afterPropertiesSet
()
throws
IllegalArgumentException, BeanInitializationException {
-
// 這里提供了接口供子類去實現
-
checkDaoConfig();
-
-
// Let concrete implementations initialize themselves.
-
try {
-
initDao();
-
}
-
catch (Exception ex) {
-
throw
new BeanInitializationException(
"Initialization of DAO failed", ex);
-
}
-
}
-
-
protected
abstract
void
checkDaoConfig
()
throws
IllegalArgumentException;
-
-
protected
void
initDao
()
throws
Exception {
-
}
-
-
}
框架自定義的MapperFactoryBean重寫了checkDaoConfig()方法,完成對所有sql語句的設置,代碼如下
-
@Override
-
protected
void
checkDaoConfig
() {
-
super.checkDaoConfig();
-
//通用Mapper
-
if (mapperHelper.isExtendCommonMapper(getObjectType())) {
-
//這里去處理該類所對應的MappedStatement,封裝在helper類中處理
-
mapperHelper.processConfiguration(getSqlSession().getConfiguration(), getObjectType());
-
}
-
}
MapperHelper的processConfiguration方法如下
-
public
void
processConfiguration
(Configuration configuration, Class<?> mapperInterface) {
-
String prefix;
-
if (mapperInterface !=
null) {
-
prefix = mapperInterface.getCanonicalName();
-
}
else {
-
prefix =
"";
-
}
-
for (Object
object :
new ArrayList<Object>(configuration.getMappedStatements())) {
-
if (
object instanceof MappedStatement) {
-
MappedStatement ms = (MappedStatement)
object;
-
//檢查這個MappedStatement是否屬於此映射對象
-
if (ms.getId().startsWith(prefix) && isMapperMethod(ms.getId())) {
-
if (ms.getSqlSource() instanceof ProviderSqlSource) {
-
//去設置該statement的sql語句
-
setSqlSource(ms);
-
}
-
}
-
}
-
}
-
}
設置sql的邏輯,提供了幾種不同類型的sqlsource
-
public void setSqlSource(MappedStatement ms) throws Exception
{
-
if (this.mapperClass == getMapperClass(ms.getId())) {
-
throw new RuntimeException("請不要配置或掃描通用Mapper接口類:" + this.mapperClass);
-
}
-
Method method = methodMap.get(getMethodName(ms));
-
try
{
-
//第一種,直接操作ms,不需要返回值
-
if (method.getReturnType() == Void.TYPE) {
-
method.invoke(this, ms);
-
}
-
//第二種,返回SqlNode
-
else
if (SqlNode.
class.isAssignableFrom(
method.getReturnType())) {
-
SqlNode sqlNode = (SqlNode) method.invoke(this, ms);
-
DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);
-
setSqlSource(ms, dynamicSqlSource);
-
}
-
//第三種,返回
xml
形式的
sql
字符串
-
else
if
(String.class.equals(method.getReturnType()))
{
-
String xmlSql = (String) method.invoke(this, ms);
-
SqlSource sqlSource = createSqlSource(ms, xmlSql);
-
//替換原有的SqlSource
-
setSqlSource(ms, sqlSource);
到這里整個sql的獲取流程就分析完了,本人用這個庫寫過一個小項目,確實節省了開發的工作量,而且DAO層的結構更加清晰簡潔了
關於mybatis新特性
從3.4.0開始,mybatis提供對外部表的alias引用方法,多表聯合查詢就方便多了,我們先看原始的方式是怎樣做的
-
select a.id,a.name,b.bid,b.bname .....
-
from
user a
-
left
join room b
原始的方式是將所有的表字段列出來,再來看用新特性怎樣做
-
select id="selectUsers" resultType="map">
-
select
-
<include refid="user_col_sql_id"><property name="alias" value="t1"/>,
-
<include refid="room_col_sql_id"><property name="alias" value="t2"/>
-
from user t1
-
left join room t2
-
</select>
這里主要就是對基本的sql進行了復用,如果對表進行了修改只要在原始的sql節點修改就可以了,就算是5個表的聯合查詢,sql也是清晰易懂,維護起來會更輕松
新版本的mybatis對於對象映射也提供了更友好的方式,直接使用外部的ResultMap再加上查詢語句中的別名就映射完成了
-
<resultMap id="workerResultMap" type="com.jjs.kaiwen.model.Worker" extends="BaseResultMap">
-
<association property="room" columnPrefix="b_" resultMap="com.jjs.kaiwen.dao.OrgMapper.BaseResultMap"/>
-
</resultMap>
更進一步
敏銳的程序員可能會提出問題,如當多表查詢的時候可能會存在字段名稱相同的情況,這里的解決方案是給include添加另一個屬性
-
<include refid="user_col_sql_id_with_alias">
-
<property name="alias" value="t"/>
-
<property name="prefix" value="t_"/>
-
</include>
包含prefix的sqlNode如下
-
<sql id=
"base_column_with_alias">
-
${
alias}.
ID as
${prefix}
ID,
-
${
alias}.
WORKER_ID as
${prefix}
WORKER_ID,
-
${
alias}.
NAME as
${prefix}
NAME,
-
${
alias}.
ZB_ROLE_ID as
${prefix}
ZB_ROLE_ID,
-
${
alias}.
ORG_ID as
${prefix}
ORG_ID,
-
${
alias}.
STATUS as
${prefix}
STATUS
-
<
/sql>
如果說覺得手動寫包含alias和prefix的字段麻煩,可以用,mybatis代碼生成器的插件的方式實現,我自己寫了一個生成器的插件,可以代碼再這里,僅供參考
通用Service類
-
/**
-
* Created by Kaiwen
-
*/
-
@Service
-
public
abstract
class CommonServiceImpl<T,PK extends Serializable> implements CommonService<T,PK> {
-
/**
-
* 泛型注入
-
*/
-
@Autowired
-
private Mapper<T> mapper;
-
-
public
T selectByPrimaryKey(PK entityId) {
-
-
return mapper.selectByPrimaryKey(entityId);
-
}
-
-
public
int
deleteByPrimaryKey(PK entityId) {
-
return mapper.deleteByPrimaryKey(entityId);
-
}
-
-
public
int
insert(T record) {
-
return mapper.insert(record);
-
}
-
-
public
int
insertSelective(T record) {
-
return mapper.insertSelective(record);
-
}
-
-
public
int
updateByPrimaryKeySelective(T record) {
-
return mapper.updateByPrimaryKeySelective(record);
-
}
-
-
public
int
updateByPrimaryKey(T record) {
-
return mapper.updateByPrimaryKey(record);
-
}
-
-
public
List<T> selectByExample(Example example) {
-
return mapper.selectByExample(example);
-
}
-
}
注入方式區別
-
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
-
<property name="basePackage" value="com.jjshome.esf.core.dao.school" />
-
<property name="properties">
-
<value>
-
mappers=tk.mybatis.mapper.common.Mapper
-
</value>
-
</property>
-
</bean>
-
-
-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
<property name="basePackage" value="com.jjshome.esf.core.dao.community,com.jjshome.esf.core.dao.hsl"/>
-
</bean>
<實體類
-
package com.jjshome.esf.common.entity.school;
-
-
import java.util.Date;
-
import javax.persistence.*;
-
-
@Table(name =
"XQ_SCHOOL_AREA")
-
public
class SchoolArea {
-
/**
-
* 主鍵ID
-
*/
-
@Id
-
@Column(name =
"ID")
-
private Integer id;
-
-
/**
-
* 城市編碼
-
*/
-
@Column(name =
"CITY_CODE")
-
private String cityCode;
-
-
/**
-
* 學區名稱
-
*/
-
@Column(name =
"NAME")
-
private String name;
-
-
/**
-
* 學區名稱拼音
-
*/
-
@Column(name =
"NAME_SPELL")
-
private String nameSpell;
-
-
/**
-
* 狀態,1:正常,0:刪除
-
*/
-
@Column(name =
"STATUS")
-
private Byte status;
-
-
/**
-
* 添加人
-
*/
-
@Column(name =
"CREATE_ID")
-
private String createId;
-
-
-
@Transient
-
private Integer primaryCount;
//小學數量
-
@Transient
-
private Integer middleCount;
//初中數量
-
@Transient
-
private Integer highCount;
//高中數量
TK mybatis Mapper文件內容
-
<?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=
"com.jjshome.esf.core.dao.school.ISchoolAreaDAO" >
-
<resultMap id=
"BaseResultMap" type=
"com.jjshome.esf.common.entity.school.SchoolArea" >
-
<!--
-
WARNING - @mbggenerated
-
-->
-
<id column=
"ID"
property=
"id" jdbcType=
"INTEGER" />
-
<result column=
"CITY_CODE"
property=
"cityCode" jdbcType=
"VARCHAR" />
-
<result column=
"NAME"
property=
"name" jdbcType=
"VARCHAR" />
-
<result column=
"NAME_SPELL"
property=
"nameSpell" jdbcType=
"VARCHAR" />
-
<result column=
"STATUS"
property=
"status" jdbcType=
"TINYINT" />
-
<result column=
"CREATE_ID"
property=
"createId" jdbcType=
"VARCHAR" />
-
<result column=
"CREATE_DATE"
property=
"createDate" jdbcType=
"TIMESTAMP" />
-
<result column=
"UPDATE_ID"
property=
"updateId" jdbcType=
"VARCHAR" />
-
<result column=
"UPDATE_DATE"
property=
"updateDate" jdbcType=
"TIMESTAMP" />
-
<result column=
"CITY_NAME"
property=
"cityName"/>
-
<result column=
"PRIMARY_COUNT"
property=
"primaryCount"/>
-
<result column=
"MIDDLE_COUNT"
property=
"middleCount"/>
-
<result column=
"HIGH_COUNT"
property=
"highCount"/>
-
</resultMap>
-
-
<resultMap id=
"SchoolDetailArea" type=
"com.jjshome.esf.common.entity.school.SchoolAreaDetail"
-
extends=
"com.jjshome.esf.core.dao.school.ISchoolInfoDAO.SchoolInfo">
-
<result column=
"SCHOOL_AREA_NAME"
property=
"schoolAreaName"/>
-
</resultMap>
-
-
<
select id=
"selectByPage" parameterType=
"map" resultMap=
"BaseResultMap">
-
-
SELECT A.*, C.NAME
AS CITY_NAME,
-
(
SELECT COUNT(*)
FROM XQ_SCHOOL_INFO B
WHERE A.ID=B.AREA_ID
AND B.TYPE=
'553' AND B.STATUS = 1 ) AS PRIMARY_COUNT,
-
(
SELECT COUNT(*)
FROM XQ_SCHOOL_INFO B
WHERE A.ID=B.AREA_ID
AND B.TYPE=
'554' AND B.STATUS = 1 ) AS MIDDLE_COUNT,
-
(
SELECT COUNT(*)
FROM XQ_SCHOOL_INFO B
WHERE A.ID=B.AREA_ID
AND B.TYPE=
'555' AND B.STATUS = 1 ) AS HIGH_COUNT
-
FROM XQ_SCHOOL_AREA A
-
LEFT
JOIN YW_CITY_SETTING C
ON A.CITY_CODE = C.CODE
-
<
where>
-
<
if test=
"name != null and name != '' "> A.NAME
LIKE CONCAT(
'%',#{NAME},'%') </if>
-
<
if test=
"areaCityCode != null and areaCityCode != '' "> A.CITY_CODE =
#{areaCityCode} </if>
-
<
if test=
"keywords != null and keywords != '' ">
-
( A.NAME
LIKE CONCAT(
'%',#{keywords},'%')
-
)
-
</
if>
-
</
where>
-
</
select>
-
-
-
<
select id=
"selectAreaIdAndKeyWord" parameterType=
"java.util.Map" resultMap=
"BaseResultMap">
-
SELECT
-
*
-
FROM
-
XQ_SCHOOL_AREA
-
WHERE
-
1=
1
-
<
if test=
"cityId != null">
-
AND CITY_CODE=
#{cityId}
-
</
if>
-
<
if test=
"key != null and key!=''">
-
AND (NAME
like CONCAT(
#{key},'%' ) or NAME_SPELL like CONCAT(#{key},'%' ))
-
</
if>
-
AND
-
STATUS=
1
-
<
if test=
"pageSize != null">
-
limit
#{pageSize}
-
</
if>
-
</
select>
-
-
-
<!--查詢學區詳情列表-->
-
<
select id=
"selectAreaDetailByPage" parameterType=
"map" resultMap=
"SchoolDetailArea">
-
-
SELECT A.* ,B.NAME
AS SCHOOL_AREA_NAME ,C.NAME
AS CITY_NAME,D.NAME
AS AREA_NAME
FROM XQ_SCHOOL_INFO A
-
LEFT
JOIN XQ_SCHOOL_AREA B
ON A.AREA_ID = B.ID
-
LEFT
JOIN YW_CITY_SETTING C
ON A.CITY_CODE = C.CODE
-
LEFT
JOIN YW_CITY_SETTING D
ON A.AREA_CODE = D.CODE
-
-
WHERE A.STATUS =
1
AND B.STATUS =
1
-
<
if test=
"areaId != null and areaId.length() > 0">
AND A.AREA_ID =
#{areaId} </if>
-
<
if test=
"typeList != null and typeList.size > 0">
-
AND
-
A.TYPE
IN
-
<foreach collection=
"typeList" item=
"item" index=
"index" open=
"(" close=
")" separator=
",">
-
#{item}
-
</foreach>
-
</
if>
-
<
if test=
"name != null and name != '' ">
AND A.NAME
LIKE CONCAT(
'%',#{name},'%') </if>
-
</
select>
-
-
</mapper>
普通mybatisMapper文件
-
<?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=
"com.jjshome.esf.core.dao.school.ISchoolInfoDAO">
-
<resultMap
id=
"SchoolInfo"
type=
"com.jjshome.esf.common.entity.school.SchoolInfo">
-
<
id column=
"ID"
property=
"id"/>
-
<
result column=
"NAME"
property=
"name"/>
-
<
result column=
"NAME_SPELL"
property=
"nameSpell"/>
-
<
result column=
"ALIAS"
property=
"alias"/>
-
<
result column=
"ALIAS_SPELL"
property=
"aliasSpell"/>
-
<
result column=
"TYPE"
property=
"type" typeHandler=
"com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
-
<
result column=
"AREA_ID"
property=
"areaId"/>
-
<
result column=
"CITY_CODE"
property=
"cityCode"/>
-
<
result column=
"AREA_CODE"
property=
"areaCode"/>
-
<
result column=
"ADDR"
property=
"addr"/>
-
<
result column=
"START_TIME"
property=
"startTime"/>
-
<
result column=
"MOTTO"
property=
"motto"/>
-
<
result column=
"WEB_SITE"
property=
"webSite"/>
-
<
result column=
"PHONE"
property=
"phone"/>
-
<
result column=
"FEATURE"
property=
"feature" typeHandler=
"com.jjshome.esf.core.component.handler.DictValueListTypeHandler"/>
-
<
result column=
"LNG"
property=
"lng"/>
-
<
result column=
"LAT"
property=
"lat"/>
-
<
result column=
"UNIT_PRICE"
property=
"unitPrice"/>
-
<
result column=
"SALE_PRICE"
property=
"salePrice"/>
-
<
result column=
"NATURE_TYPE"
property=
"natureType" typeHandler=
"com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
-
<
result column=
"NATURE_CITY"
property=
"natureCity" typeHandler=
"com.jjshome.esf.core.component.handler.DictValueTypeHandler"/>
-
<
result column=
"SCHOOL_DEGREE"
property=
"schoolDegree"/>
-
<
result column=
"ENROL_DEGREE"
property=
"enrolDegree"/>
-
<
result column=
"IMG_DEGREE"
property=
"imgDegree"/>
-
<
result column=
"STATUS"
property=
"status"/>
-
<
result column=
"CREATE_ID"
property=
"createId"/>
-
<
result column=
"CREATE_DATE"
property=
"createDate"/>
-
<
result column=
"UPDATE_ID"
property=
"updateId"/>
-
<
result column=
"UPDATE_DATE"
property=
"updateDate"/>
-
-
<
result column=
"CITY_NAME"
property=
"cityName" />
-
<
result column=
"AREA_NAME"
property=
"areaName" />
-
<
result column=
"SCHOOL_DISTRICT_NAME"
property=
"schoolDistrictName" />
-
<
result column=
"SALE_COUNT"
property=
"saleCount" />
-
</resultMap>
-
-
<sql
id=
"Base_Column_List">
-
ID,
-
NAME,
-
NAME_SPELL,
-
ALIAS,
-
ALIAS_SPELL,
-
TYPE,
-
AREA_ID,
-
CITY_CODE,
-
AREA_CODE,
-
ADDR,
-
START_TIME,
-
MOTTO,
-
WEB_SITE,
-
PHONE,
-
FEATURE,
-
LNG,
-
LAT,
-
UNIT_PRICE,
-
SALE_PRICE,
-
NATURE_TYPE,
-
NATURE_CITY,
-
SCHOOL_DEGREE,
-
ENROL_DEGREE,
-
IMG_DEGREE,
-
STATUS,
-
CREATE_ID,
-
CREATE_DATE,
-
UPDATE_ID,
-
UPDATE_DATE,
-
SALE_COUNT,
-
SALE_COUNT
-
</sql>
-
-
<select
id=
"selectById" resultMap=
"SchoolInfo" parameterType=
"java.lang.Integer">
-
SELECT
-
i.*,
-
yc.NAME
as
'CITY_NAME',
-
ya.NAME
as
'AREA_NAME',
-
xq.NAME
as
'SCHOOL_DISTRICT_NAME'
-
FROM
-
XQ_SCHOOL_INFO i
-
LEFT JOIN YW_CITY_SETTING yc ON i.CITY_CODE = yc.CODE
-
LEFT JOIN YW_CITY_SETTING ya ON i.AREA_CODE = ya.CODE
-
LEFT JOIN XQ_SCHOOL_AREA xq ON i.AREA_ID = xq.ID
-
WHERE
-
i.ID =
#{id,jdbcType=INTEGER}
-
</select>
-
-
<delete
id=
"deleteById" parameterType=
"java.util.Map">
-
UPDATE
-
XQ_SCHOOL_INFO
-
SET
-
STATUS =
0,
-
UPDATE_ID =
#{updateId},
-
UPDATE_DATE = NOW()
-
WHERE
-
ID =
#{id,jdbcType=INTEGER}
-
</delete>
-
-
<delete
id=
"batchDeleteByIds" parameterType=
"java.util.Map">
-
UPDATE
-
XQ_SCHOOL_INFO
-
SET
-
STATUS =
0,
-
UPDATE_ID =
#{updateId},
-
UPDATE_DATE = NOW()
-
WHERE
-
ID IN (
${ids})
-
</delete>
-
-
<update
id=
"deleteAreaRelation" parameterType=
"com.jjshome.esf.common.entity.school.SchoolInfo">
-
update XQ_SCHOOL_INFO
-
SET AREA_ID = NULL,
-
UPDATE_DATE = NOW()
-
WHERE
-
ID =
#{id}
-
</update>
-
-
<insert
id=
"insert" parameterType=
"com.jjshome.esf.common.entity.school.SchoolInfo">
-
<selectKey resultType=
"Integer" keyProperty=
"id">
-
SELECT LAST_INSERT_ID()
-
</selectKey>
-
INSERT INTO XQ_SCHOOL_INFO
-
(NAME,
-
NAME_SPELL,
-
ALIAS,
-
ALIAS_SPELL,
-
TYPE,
-
AREA_ID,
-
CITY_CODE,
-
AREA_CODE,
-
ADDR,
-
START_TIME,
-
MOTTO,
-
WEB_SITE,
-
PHONE,
-
FEATURE,
-
LNG,
-
LAT,
-
UNIT_PRICE,
-
SALE_PRICE,
-
NATURE_TYPE,
-
NATURE_CITY,
-
SCHOOL_DEGREE,
-
ENROL_DEGREE,
-
IMG_DEGREE,
-
STATUS,
-
CREATE_ID,
-
CREATE_DATE,
-
UPDATE_ID,
-
UPDATE_DATE)
-
VALUES
-
(
#{name,jdbcType=VARCHAR},
-
#{nameSpell,jdbcType=VARCHAR},
-
#{alias,jdbcType=VARCHAR},
-
#{aliasSpell,jdbcType=VARCHAR},
-
#{type,jdbcType=INTEGER},
-
#{areaId,jdbcType=INTEGER},
-
#{cityCode,jdbcType=VARCHAR},
-
#{areaCode,jdbcType=VARCHAR},
-
#{addr,jdbcType=VARCHAR},
-
#{startTime,jdbcType=DATE},
-
#{motto,jdbcType=VARCHAR},
-
#{webSite,jdbcType=VARCHAR},
-
#{phone,jdbcType=VARCHAR},
-
#{feature,jdbcType=VARCHAR},
-
#{lng,jdbcType=DECIMAL},
-
#{lat,jdbcType=DECIMAL},
-
#{unitPrice},
-
#{salePrice},
-
#{natureType,jdbcType=INTEGER},
-
#{natureCity,jdbcType=INTEGER},
-
#{schoolDegree,jdbcType=INTEGER},
-
#{enrolDegree,jdbcType=INTEGER},
-
#{imgDegree,jdbcType=INTEGER},
-
#{status,jdbcType=TINYINT},
-
#{createId,jdbcType=VARCHAR},
-
#{createDate,jdbcType=DATE},
-
#{updateId,jdbcType=VARCHAR},
-
#{updateDate,jdbcType=DATE})
-
</insert>
-
<insert
id=
"insertSelective" parameterType=
"com.jjshome.esf.common.entity.school.SchoolInfo">
-
<selectKey resultType=
"Integer" keyProperty=
"id">
-
SELECT LAST_INSERT_ID()
-
</selectKey>
-
INSERT INTO XQ_SCHOOL_INFO
-
<trim prefix=
"(" suffix=
")" suffixOverrides=
",">
-
<
if
test=
"name != null">
-
NAME,
-
</
if>
-
<
if
test=
"nameSpell != null">
-
NAME_SPELL,
-
</
if>
-
<
if
test=
"alias != null">
-
ALIAS,
-
</
if>
-
<
if
test=
"aliasSpell != null">
-
ALIAS_SPELL,
-
</
if>
-
<
if
test=
"type != null">
-
TYPE,
-
</
if>
-
<
if
test=
"areaId != null">
-
AREA_ID,
-
</
if>
-
<
if
test=
"cityCode != null">
-
CITY_CODE,
-
</
if>
-
<
if
test=
"areaCode != null">
-
AREA_CODE,
-
</
if>
-
<
if
test=
"addr != null">
-
ADDR,
-
</
if>
-
<
if
test=
"startTime != null">
-
START_TIME,
-
</
if>
-
<
if
test=
"motto != null">
-
MOTTO,
-
</
if>
-
<
if
test=
"webSite != null">
-
WEB_SITE,
-
</
if>
-
<
if
test=
"phone != null">
-
PHONE,
-
</
if>
-
<
if
test=
"feature != null">
-
FEATURE,
-
</
if>
-
<
if
test=
"lng != null">
-
LNG,
-
</
if>
-
<
if
test=
"lat != null">
-
LAT,
-
</
if>
-
<
if
test=
"UNIT_PRICE != null">
-
UNIT_PRICE,
-
</
if>
-
<
if
test=
"SALE_PRICE != null ">
-
SALE_PRICE,
-
</
if>
-
<
if
test=
"natureType != null">
-
NATURE_TYPE,
-
</
if>
-
<
if
test=
"natureCity != null">
-
NATURE_CITY,
-
</
if>
-
<
if
test=
"schoolDegree != null">
-
SCHOOL_DEGREE,
-
</
if>
-
<
if
test=
"enrolDegree != null">
-
ENROL_DEGREE,
-
</
if>
-
<
if
test=
"imgDegree != null">
-
IMG_DEGREE,
-
</
if>
-
<
if
test=
"status != null">
-
STATUS,
-
</
if>
-
<
if
test=
"createId != null">
-
CREATE_ID,
-
</
if>
-
<
if
test=
"createDate != null">
-
CREATE_DATE,
-
</
if>
-
<
if
test=
"updateId != null">
-
UPDATE_ID,
-
</
if>
-
<
if
test=
"updateDate != null">
-
UPDATE_DATE,
-
</
if>
-
</trim>
-
<trim prefix=
"VALUES (" suffix=
")" suffixOverrides=
",">
-
<
if
test=
"name != null">
-
#{name,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"nameSpell != null">
-
#{nameSpell,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"alias != null">
-
#{alias,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"aliasSpell != null">
-
#{aliasSpell,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"type != null">
-
#{type,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"areaId != null">
-
#{areaId,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"cityCode != null">
-
#{cityCode,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"areaCode != null">
-
#{areaCode,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"addr != null">
-
#{addr,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"startTime != null">
-
#{startTime,jdbcType=DATE},
-
</
if>
-
<
if
test=
"motto != null">
-
#{motto,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"webSite != null">
-
#{webSite,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"phone != null">
-
#{phone,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"feature != null">
-
#{feature,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"lng != null">
-
#{lng,jdbcType=DECIMAL},
-
</
if>
-
<
if
test=
"lat != null">
-
#{lat,jdbcType=DECIMAL},
-
</
if>
-
<
if
test=
"unitPrice ! =null">
-
#{unitPrice},
-
</
if>
-
<
if
test=
"salePrice">
-
#{salePrice},
-
</
if>
-
<
if
test=
"natureType != null">
-
#{natureType,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"natureCity != null">
-
#{natureCity,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"schoolDegree != null">
-
#{schoolDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"enrolDegree != null">
-
#{enrolDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"imgDegree != null">
-
#{imgDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"status != null">
-
#{status,jdbcType=TINYINT},
-
</
if>
-
<
if
test=
"createId != null">
-
#{createId,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"createDate != null">
-
#{createDate,jdbcType=DATE},
-
</
if>
-
<
if
test=
"updateId != null">
-
#{updateId,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"updateDate != null">
-
#{updateDate,jdbcType=DATE},
-
</
if>
-
</trim>
-
</insert>
-
<update
id=
"updateSelective" parameterType=
"com.jjshome.esf.common.entity.school.SchoolInfo">
-
UPDATE XQ_SCHOOL_INFO
-
<
set>
-
<
if
test=
"name != null">
-
NAME=
#{name,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"nameSpell != null">
-
NAME_SPELL=
#{nameSpell,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"alias != null">
-
ALIAS=
#{alias,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"aliasSpell != null">
-
ALIAS_SPELL=
#{aliasSpell,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"type != null">
-
TYPE=
#{type,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"type != null">
-
AREA_ID=
#{areaId,jdbcType=INTEGER},
-
</
if>
-
-
<
if
test=
"cityCode != null">
-
CITY_CODE=
#{cityCode,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"areaCode != null">
-
AREA_CODE=
#{areaCode,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"addr != null">
-
ADDR=
#{addr,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"startTime != null">
-
START_TIME=
#{startTime,jdbcType=DATE},
-
</
if>
-
<
if
test=
"motto != null">
-
MOTTO=
#{motto,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"webSite != null">
-
WEB_SITE=
#{webSite,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"phone != null">
-
PHONE=
#{phone,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"feature != null">
-
FEATURE=
#{feature,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"lng != null">
-
LNG=
#{lng,jdbcType=DECIMAL},
-
</
if>
-
<
if
test=
"lat != null">
-
LAT=
#{lat,jdbcType=DECIMAL},
-
</
if>
-
<
if
test=
"salePrice != null">
-
UNIT_PRICE=
#{unitPrice},
-
</
if>
-
<
if
test=
"salePrice != null">
-
SALE_PRICE=
#{salePrice},
-
</
if>
-
<
if
test=
"natureType != null">
-
NATURE_TYPE=
#{natureType,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"natureCity != null">
-
NATURE_CITY=
#{natureCity,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"schoolDegree != null">
-
SCHOOL_DEGREE=
#{schoolDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"enrolDegree != null">
-
ENROL_DEGREE=
#{enrolDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"imgDegree != null">
-
IMG_DEGREE=
#{imgDegree,jdbcType=INTEGER},
-
</
if>
-
<
if
test=
"status != null">
-
STATUS=
#{status,jdbcType=TINYINT},
-
</
if>
-
<
if
test=
"createId != null">
-
CREATE_ID=
#{createId,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"createDate != null">
-
CREATE_DATE=
#{createDate,jdbcType=DATE},
-
</
if>
-
<
if
test=
"updateId != null">
-
UPDATE_ID=
#{updateId,jdbcType=VARCHAR},
-
</
if>
-
<
if
test=
"updateDate != null">
-
UPDATE_DATE=
#{updateDate,jdbcType=DATE},
-
</
if>
-
<
if
test=
"saleCount != null">
-
SALE_COUNT=
#{saleCount},
-
</
if>
-
</
set>
-
WHERE
-
ID =
#{id,jdbcType=INTEGER}
-
</update>
-
<select
id=
"selectList" parameterType=
"com.jjshome.esf.common.entity.school.SchoolInfo" resultMap=
"SchoolInfo">
-
SELECT
-
<include refid=
"Base_Column_List" />
-
FROM
-
XQ_SCHOOL_INFO
-
WHERE
-
STATUS =
1
-
<
if
test=
"areaId != null and areaId != null"> AND AREA_ID =
#{areaId} </if>
-
-
</select>
-
-
<select
id=
"selectSchoolInfoAll" resultMap=
"SchoolInfo">
-
SELECT
-
<include refid=
"Base_Column_List" />
-
FROM
-
XQ_SCHOOL_INFO
-
WHERE
-
STATUS =
1
-
ORDER BY ID DESC
-
</select>
-
-
<select
id=
"selectSchoolInfo" parameterType=
"com.jjshome.esf.common.model.SchoolInfoSearchModel" resultMap=
"SchoolInfo">
-
SELECT
-
i.*,
-
yc.NAME
as
'CITY_NAME',
-
ya.NAME
as
'AREA_NAME'
-
FROM
-
XQ_SCHOOL_INFO i
-
LEFT JOIN YW_CITY_SETTING yc ON i.CITY_CODE = yc.CODE
-
LEFT JOIN YW_CITY_SETTING ya ON i.AREA_CODE = ya.CODE
-
WHERE
-
i.STATUS =
1
-
<
if
test=
"city != null and city != '' ">
-
AND i.CITY_CODE=
#{city}
-
</
if>
-
<
if
test=
"area != null and area != '' ">
-
AND i.AREA_CODE=
#{area}
-
</
if>
-
<
if
test=
"schoolId != null and schoolId != ''">
-
AND i.ID=
#{schoolId}
-
</
if>
-
-
<
if
test=
"schoolName != null and schoolName != ''">
-
AND i.NAME LIKE concat(
'%',
#{schoolName},'%')
-
</
if>
-
-
<
if
test=
"schoolDistrictId != null and schoolDistrictId != ''">
-
AND i.AREA_ID=
#{schoolDistrictId}
-
</
if>
-
<
if
test=
"schoolType != null and schoolType != '' ">
-
AND i.TYPE=
#{schoolType}
-
</
if>
-
<
if
test=
"key != null and key != '' ">
-
AND (i.NAME LIKE CONCAT(
'%',
#{key},'%') OR i.ALIAS LIKE CONCAT('%', #{key}, '%'))
-
</
if>
-
/*priceType ==
1:起售價
2:房源售均價*/
-
<
if
test=
"priceType == 1">
-
<
if
test=
"salePriceStart != null and salePriceStart != '' ">
-
AND SALE_PRICE >=
#{salePriceStart}
-
</
if>
-
<
if
test=
"salePriceEnd != null and salePriceEnd != '' ">
-
AND SALE_PRICE <=
#{salePriceEnd}
-
</
if>
-
</
if>
-
<
if
test=
"priceType == 2">
-
<
if
test=
"salePriceStart != null and salePriceStart != '' ">
-
AND UNIT_PRICE >=
#{salePriceStart}
-
</
if>
-
<
if
test=
"salePriceEnd != null and salePriceEnd != '' ">
-
AND UNIT_PRICE <=
#{salePriceEnd}
-
</
if>
-
</
if>
-
<
if
test=
"perfectSituation == 1">
-
AND SCHOOL_DEGREE =
100
-
</
if>
-
<
if
test=
"perfectSituation == 2">
-
AND SCHOOL_DEGREE <
100
-
</
if>
-
ORDER BY ID DESC
-
-
</select>
-
-
<select
id=
"selectSchoolByNameAndCityAndArea" parameterType=
"java.util.Map" resultMap=
"SchoolInfo">
-
SELECT
-
<include refid=
"Base_Column_List" />
-
FROM
-
XQ_SCHOOL_INFO
-
WHERE
-
STATUS =
1
-
AND NAME =
#{name}
-
AND CITY_CODE=
#{city}
-
AND AREA_CODE=
#{area}
-
AND TYPE =
#{type}
-
</select>
-
-
<select
id=
"selectAreaIdAndKeyWord" parameterType=
"java.util.Map" resultMap=
"SchoolInfo">
-
SELECT
-
XSI.*,CYCS.NAME AS
'CITY_NAME',AYCS.NAME AS
'AREA_NAME'
-
FROM
-
XQ_SCHOOL_INFO XSI
-
LEFT JOIN YW_CITY_SETTING CYCS ON XSI.CITY_CODE = CYCS.CODE
-
LEFT JOIN YW_CITY_SETTING AYCS ON XSI.AREA_CODE = AYCS. CODE
-
WHERE
-
1=
1
-
<
if
test=
"areaId != null and areaId != ''">
-
AND XSI.AREA_CODE=
#{areaId}
-
</
if>
-
<
if
test=
"key != null and key!=''">
-
AND (XSI.NAME like CONCAT(
#{key},'%' ) or XSI.NAME_SPELL like CONCAT(#{key},'%' ))
-
</
if>
-
AND
-
XSI.STATUS=
1
-
<
if
test=
"pageSize != null">
-
limit
#{pageSize}
-
</
if>
-
</select>
-
-
-
<select
id=
"selectAreaIdList" parameterType=
"map" resultType=
"integer">
-
SELECT DISTINCT AREA_ID FROM XQ_SCHOOL_INFO WHERE NAME LIKE CONCAT(
'%',
#{schoolName},'%')
-
</select>
-
-
-
<select
id=
"selectSchoolList" parameterType=
"map" resultMap=
"SchoolInfo">
-
SELECT
-
<include refid=
"Base_Column_List" />
-
FROM
-
XQ_SCHOOL_INFO
-
WHERE
-
STATUS =
1
-
<
if
test=
"idList != null and idList.size gt 0">
-
AND ID IN
-
<foreach collection=
"idList"
item=
"item" index=
"index" open=
"(" close=
")" separator=
",">
-
#{item}
-
</foreach>
-
</
if>
-
-
<
if
test=
"areaId != null and areaId != null"> AND AREA_ID =
#{areaId} </if>
-
-
</select>
-
</mapper>