锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鍦ㄤ嬌鐢∕yBatis鍋氭寔涔呭眰鏃訛紝insert璇彞榛樿鏄笉榪斿洖璁板綍鐨勪富閿鹼紝鑰屾槸榪斿洖鎻掑叆鐨勮褰曟潯鏁幫紱濡傛灉涓氬姟灞傞渶瑕佸緱鍒拌褰曠殑涓婚敭鏃訛紝鍙互閫氳繃閰嶇疆鐨勬柟寮忔潵瀹屾垚榪欎釜鍔熻兘
閽堝Sequence涓婚敭鑰岃█錛屽湪鎵цinsert sql鍓嶅繀欏繪寚瀹氫竴涓富閿肩粰瑕佹彃鍏ョ殑璁板綍錛屽Oracle銆丏B2錛屽彲浠ラ噰鐢ㄥ涓嬮厤緗柟寮忥細
<insert id="add" parameterType="vo.Category">
<selectKey resultType="java.lang.Short" order="BEFORE" keyProperty="id">
SELECT SEQ_TEST.NEXTVAL FROM DUAL
</selectKey>
insert into category (name_zh, parent_id,
show_order, delete_status, description
)
values (#{nameZh,jdbcType=VARCHAR},
#{parentId,jdbcType=SMALLINT},
#{showOrder,jdbcType=SMALLINT},
#{deleteStatus,jdbcType=BIT},
#{description,jdbcType=VARCHAR}
)
</insert>
閽堝鑷涓婚敭鐨勮〃錛屽湪鎻掑叆鏃朵笉闇瑕佷富閿紝鑰屾槸鍦ㄦ彃鍏ヨ繃紼嬭嚜鍔ㄨ幏鍙栦竴涓嚜澧炵殑涓婚敭錛屾瘮濡侻ySQL錛屽彲浠ラ噰鐢ㄥ涓嬩袱縐嶉厤緗柟寮忥細
<insert id="add" parameterType="vo.Category" useGeneratedKeys="true" keyProperty="id">
insert into category (name_zh, parent_id,
show_order, delete_status, description
)
values (#{nameZh,jdbcType=VARCHAR},
#{parentId,jdbcType=SMALLINT},
#{showOrder,jdbcType=SMALLINT},
#{deleteStatus,jdbcType=BIT},
#{description,jdbcType=VARCHAR}
)
</insert>
鎴?/span>
<insert id="add" parameterType="vo.Category">
<selectKey resultType="java.lang.Short" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
insert into category (name_zh, parent_id,
show_order, delete_status, description
)
values (#{nameZh,jdbcType=VARCHAR},
#{parentId,jdbcType=SMALLINT},
#{showOrder,jdbcType=SMALLINT},
#{deleteStatus,jdbcType=BIT},
#{description,jdbcType=VARCHAR}
)
</insert>
鍦ㄦ彃鍏ユ搷浣滃畬鎴愪箣鍚庯紝鍙傛暟category鐨刬d灞炴у氨宸茬粡琚祴鍊間簡
1.鎶奾ibernate涓嬬殑dialect鍖呭叏閮ㄦ嫹璐濆埌mybatis鍖呯殑jdbc鐩綍涓嬶紝濡備笅鍥炬墍紺猴細
2.瀹氫箟涓涓猂esultSetHandler Interceptor
package cn.machi.utils;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
@Intercepts( {@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DiclectResultSetHandlerInterceptor implements Interceptor
{
public Object intercept(Invocation invocation) throws Throwable
{
FastResultSetHandler resultSet = (FastResultSetHandler)invocation.getTarget();
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(resultSet,
"rowBounds");
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
ReflectUtil.setFieldValue(resultSet, "rowBounds", new RowBounds());
}
return invocation.proceed();
}
public Object plugin(Object target)
{
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties)
{
}
}
3.瀹氫箟涓涓猄tatementHandler鐨処nterceptor
package cn.machi.utils;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
@Intercepts( {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class DiclectStatementHandlerInterceptor implements Interceptor
{
private static final String DIALECT = "org.apache.ibatis.jdbc.dialect.OracleDialect";
public Object intercept(Invocation invocation) throws Throwable
{
RoutingStatementHandler statement = (RoutingStatementHandler)invocation.getTarget();
PreparedStatementHandler handler = (PreparedStatementHandler)ReflectUtil.getFieldValue(statement,
"delegate");
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(handler,
"rowBounds");
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
BoundSql boundSql = statement.getBoundSql();
String sql = boundSql.getSql();
OracleDialect dialect = (OracleDialect)Class.forName(DIALECT)
.newInstance();
sql = dialect.getLimitString(sql,
rowBounds.getOffset(),
rowBounds.getLimit());
ReflectUtil.setFieldValue(boundSql, "sql", sql);
}
return invocation.proceed();
}
public Object plugin(Object target)
{
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties)
{
}
}
4.瀹氫箟宸ュ叿綾籖eflectUtil
package cn.machi.utils;
imp
imp
imp
imp
imp
public class ReflectUtil
{
private static Logger log = Logger.getLogger(ReflectUtil.class);
private static Object operate(Object obj, String fieldName,
Object fieldVal, String type)
{
Object ret = null;
try
{
// 鑾峰緱瀵硅薄綾誨瀷
Class<? extends Object> classType = obj.getClass();
// 鑾峰緱瀵硅薄鐨勬墍鏈夊睘鎬?nbsp;
Field fields[] = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
Field field = fields[i];
if (field.getName().equals(fieldName))
{
String firstLetter = fieldName.substring(0, 1)
.toUpperCase(); // 鑾峰緱鍜屽睘鎬у搴旂殑getXXX()鏂規硶鐨勫悕瀛?nbsp;
if ("set".equals(type))
{
String setMethodName = "set" + firstLetter
+ fieldName.substring(1); // 鑾峰緱鍜屽睘鎬у搴旂殑getXXX()鏂規硶
Method setMethod = classType.getMethod(setMethodName,
new Class[] {field.getType()}); // 璋冪敤鍘熷璞$殑getXXX()鏂規硶
ret = setMethod.invoke(obj, new Object[] {fieldVal});
}
if ("get".equals(type))
{
String getMethodName = "get" + firstLetter
+ fieldName.substring(1); // 鑾峰緱鍜屽睘鎬у搴旂殑setXXX()鏂規硶鐨勫悕瀛?nbsp;
Method getMethod = classType.getMethod(getMethodName,
new Class[] {});
ret = getMethod.invoke(obj, new Object[] {});
}
return ret;
}
}
}
catch (Exception e)
{
log.warn("reflect error:" + fieldName, e);
}
return ret;
}
public static Object getVal(Object obj, String fieldName)
{
return operate(obj, fieldName, null, "get");
}
public static void setVal(Object obj, String fieldName, Object fieldVal)
{
operate(obj, fieldName, fieldVal, "set");
}
private static Method getDeclaredMethod(Object object, String methodName,
Class<?>[] parameterTypes)
{
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
{
try
{
//superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
}
catch (NoSuchMethodException e)
{
//Method 涓嶅湪褰撳墠綾誨畾涔? 緇х畫鍚戜笂杞瀷
}
}
return null;
}
private static void makeAccessible(Field field)
{
if (!Modifier.isPublic(field.getModifiers()))
{
field.setAccessible(true);
}
}
private static Field getDeclaredField(Object object, String filedName)
{
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
{
try
{
return superClass.getDeclaredField(filedName);
}
catch (NoSuchFieldException e)
{
//Field 涓嶅湪褰撳墠綾誨畾涔? 緇х畫鍚戜笂杞瀷
}
}
return null;
}
public static Object invokeMethod(Object object, String methodName,
Class<?>[] parameterTypes, Object[] parameters)
throws InvocationTargetException
{
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null)
{
throw new IllegalArgumentException("Could not find method ["
+ methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try
{
return method.invoke(object, parameters);
}
catch (IllegalAccessException e)
{
}
return null;
}
public static void setFieldValue(Object object, String fieldName,
Object value)
{
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field ["
+ fieldName + "] on target [" + object + "]");
makeAccessible(field);
try
{
field.set(object, value);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
public static Object getFieldValue(Object object, String fieldName)
{
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field ["
+ fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try
{
result = field.get(object);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
return result;
}
}
5.鏇存柊mapper configuration鏂囦歡錛屾坊鍔犲涓嬪嚑鏉★紝娉ㄦ剰plugins鍦ㄦ暣涓猚onfiguration鏂囦歡涓殑欏哄簭
<plugins>
<plugin interceptor="functionPoint.db.DiclectStatementHandlerInterceptor"/>
<plugin interceptor="functionPoint.db.DiclectResultSetHandlerInterceptor"/>
</plugins>
6.浣跨敤鏂規硶鍚宮ybatis閫昏緫鍒嗛〉錛屾嫤鎴櫒浼氳嚜鍔ㄦ嫤鎴墽琛孲QL鐨勫湴鏂癸紝鍔犱笂鍒嗛〉浠g爜錛?/strong>
getSqlSession().selectList(mapId, queryKey,new RowBounds(pageId, pageSize));