分享了以下tips:
一、事務管理
二、xml配置sql代碼段
三、#和$的區別
四、注意對<、>做轉義
五、依據字符串是否爲空,動態組織sql語句
六、使用自定義的類型轉換器
七、resultMap的複用
一、事務管理
1、確保數據庫表的類型是InnoDB,而不是MyISAM
2、所有更新操作完成後,再執行commit
SqlSession session = getSqlSession();
int sqlResult = session.insert("insert a table", po);
int sqlResult = session.update("update b table", po);
//這時,前面的insert和update還沒真正執行
session.commit();
//commit後,db才真正更新
session.close();
二、xml配置sql代碼段
1、在xml中先用<sql>這個標籤,包裝代碼段,配置id
<sql id="getFeedBackList_fragment">
from t_comment where refer_type = #{type} and
refer_id = #{referId}
</sql>
2、在需要這個代碼段的地方,通過<include refid="id"/>語句,引入這個sql
<select id='getFeedBackList' parameterType='map' resultMap='feeback'>
select id
<include refid="getFeedBackList_fragment"/>
order by
create_time desc
</select>
三、#和$的區別
四、注意對<、>做轉義
五、依據字符串是否爲空,動態組織sql語句
六、使用自定義的類型轉換器
1、configuration xml文件配置
<configuration>
……
<typeHandlers>
<typeHandler handler="test.TimeTypeHandler"
javaType="Long" jdbcType="TIME" />
</typeHandlers>
……
</configuration>
2、自定義類型轉換器
package test;
public class TimeTypeHandler implements TypeHandler<Long>
{
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);
}
}
3、mapper xml配置
<mapper namespace="xxxx">
……
//這裏定義了ExpDownloadPo這個sql查詢結果,要映射到java bean :test.ExpDownloadPo
<resultMap id="ExpDownloadPo" type="test.ExpDownloadPo">
<id property="id" column="id" />
//first_download_time這個數據,在賦值到firstDownloadTime時,要轉轉換,從jdbc的time型,轉換成javaType的long型,具體怎麼轉換,因爲前面第一點,定義了<typeHandlers><typeHandler handler="test.TimeTypeHandler" javaType="Long" jdbcType="TIME" /></typeHandlers>,因此會自動使用test.TimeTypeHandler這個轉換器
<result property="firstDownloadTime" column="first_download_time" javaType="Long" jdbcType="TIME"/>
<result property="lastDownloadTime" column="last_download_time" javaType="Long" jdbcType="TIME"/>
</resultMap>
……
</mapper >
七、resultMap的複用
mapper文件A
<mapper namespace="com.blog.test">
……
<resultMap id="testbean" ></resultMap>
</mapper>
mapper文件B
<mapper namespace="B">
<select id="mysql" parameterType="map" resultMap="com.blog.test.testbean">
</mapper>