分享了以下tips: 一、事務(wù)管理 二、xml配置sql代碼段 三、#和$的區(qū)別 四、注意對、做轉(zhuǎn)義 五、依據(jù)字符串是否為空,動態(tài)組織sql語句 六、使用自定義的類型轉(zhuǎn)換器 七、resultMap的復(fù)用 一、事務(wù)管理 用戶執(zhí)行一個動作,后臺需依次更新多個表,如果其中有一個
分享了以下tips:
一、事務(wù)管理
二、xml配置sql代碼段
三、#和$的區(qū)別
四、注意對<、>做轉(zhuǎn)義
五、依據(jù)字符串是否為空,動態(tài)組織sql語句
六、使用自定義的類型轉(zhuǎn)換器
七、resultMap的復(fù)用
SqlSession session = getSqlSession(); int sqlResult = session.insert("insert a table", po); int sqlResult = session.update("update b table", po); //這時,前面的insert和update還沒真正執(zhí)行 session.commit(); //commit后,db才真正更新 session.close();
from t_comment where refer_type = #{type} and refer_id = #{referId}
…… ……
package test; public class TimeTypeHandler implements TypeHandler{ private final String TIME_TYPE = "yyyy-MM-dd HH:mm:ss"; public long strToLongTime(String dateStr) { if (null == dateStr) return 0L; SimpleDateFormat sdf = new SimpleDateFormat(TIME_TYPE); Date date = new Date(); try { date = sdf.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } if (date == null) return 0L; System.out.println(date); return date.getTime(); } public Long getResult(ResultSet arg0, String arg1) throws SQLException { String datestr = arg0.getString(arg1); return strToLongTime(datestr); } public Long getResult(ResultSet arg0, int arg1) throws SQLException { String datestr = arg0.getString(arg1); return strToLongTime(datestr); } public Long getResult(CallableStatement arg0, int arg1) throws SQLException { String datestr = arg0.getString(arg1); return strToLongTime(datestr); } public void setParameter(PreparedStatement arg0, int arg1, Long arg2, JdbcType arg3) throws SQLException { if (arg2 == null) { arg2 = 0L; } Date date = new Date(arg2); SimpleDateFormat sdf = new SimpleDateFormat(TIME_TYPE); String datetime = sdf.format(date); arg0.setString(arg1, datetime); } }
…… //這里定義了ExpDownloadPo這個sql查詢結(jié)果,要映射到j(luò)ava bean :test.ExpDownloadPo …… //first_download_time這個數(shù)據(jù),在賦值到firstDownloadTime時,要轉(zhuǎn)轉(zhuǎn)換,從jdbc的time型,轉(zhuǎn)換成javaType的long型,具體怎么轉(zhuǎn)換,因為前面第一點,定義了 ,因此會自動使用test.TimeTypeHandler這個轉(zhuǎn)換器
……
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com