锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产乱码精品一区二区三区四区 ,av影片在线看,日韩电影在线看http://www.aygfsteel.com/keweibo/category/50241.html涓涓? Java 澶氳兘 Delphi,Powerbuilder ... zh-cnWed, 30 Nov 2011 09:11:14 GMTWed, 30 Nov 2011 09:11:14 GMT60MyBatis insert鎿嶄綔榪斿洖涓婚敭(杞?http://www.aygfsteel.com/keweibo/articles/365211.htmlKeKeWed, 30 Nov 2011 08:06:00 GMThttp://www.aygfsteel.com/keweibo/articles/365211.htmlhttp://www.aygfsteel.com/keweibo/comments/365211.htmlhttp://www.aygfsteel.com/keweibo/articles/365211.html#Feedback0http://www.aygfsteel.com/keweibo/comments/commentRss/365211.htmlhttp://www.aygfsteel.com/keweibo/services/trackbacks/365211.html鍘熸枃:http://289972458.iteye.com/blog/1001851

鍦ㄤ嬌鐢∕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灞炴у氨宸茬粡琚祴鍊間簡



Ke 2011-11-30 16:06 鍙戣〃璇勮
]]>
mybatis3.05 鍒嗛〉鏀惰棌http://www.aygfsteel.com/keweibo/articles/355693.htmlKeKeWed, 03 Aug 2011 09:15:00 GMThttp://www.aygfsteel.com/keweibo/articles/355693.htmlhttp://www.aygfsteel.com/keweibo/comments/355693.htmlhttp://www.aygfsteel.com/keweibo/articles/355693.html#Feedback0http://www.aygfsteel.com/keweibo/comments/commentRss/355693.htmlhttp://www.aygfsteel.com/keweibo/services/trackbacks/355693.html浠ヤ笅鍏у渚嗚嚜:
http://zhaohe162.blog.163.com/blog/static/38216797201131262952990/

1.鎶奾ibernate涓嬬殑dialect鍖呭叏閮ㄦ嫹璐濆埌mybatis鍖呯殑jdbc鐩綍涓嬶紝濡備笅鍥炬墍紺猴細

mybatis涓嬬殑鍒嗛〉錛屾敮鎸佹墍鏈夌殑鏁版嵁搴?- 鏂皹浼ょ棔 - 鏂皹灞? style=
 



2.瀹氫箟涓涓猂esultSetHandler  Interceptor

package cn.machi.utils;

import java.sql.Statement;
import java.util.Properties;

import org.apache.ibatis.executor.resultset.FastResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.RowBounds;

@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;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.PreparedStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.jdbc.dialect.OracleDialect;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.RowBounds;

@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;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.apache.log4j.Logger;


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));


==============================================================
鎸夌収涓婇潰鐨勫仛浜?浣嗛亱琛屾檪鍗繪媼鍑虹己灝?IN OUT鍙冩暩涔嬮鐨勯尟瑾?
寰屼締鎻涗簡Dialect欏炴枃浠跺氨OK浜?OracleDialect鍏у鍙冭冭嚜:
http://rapid-framework.googlecode.com/svn/trunk/rapid-framework/src/rapid_framework_common/cn/org/rapid_framework/jdbc/dialect/




Ke 2011-08-03 17:15 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 西乌| 丁青县| 福建省| 重庆市| 武鸣县| 淳安县| 丽江市| 镇宁| 无棣县| 兴文县| 明星| 长泰县| 渝中区| 夹江县| 习水县| 会理县| 弥渡县| 沙坪坝区| 黎平县| 西乡县| 兴宁市| 上杭县| 昌平区| 平乡县| 榆林市| 乐业县| 和林格尔县| 唐河县| 张家川| 东乡族自治县| 晋宁县| 高邮市| 廉江市| 乌拉特后旗| 新闻| 吉林省| 永春县| 九龙坡区| 甘泉县| 湟中县| 临潭县|