MyBatis參考文檔:
中文版:http://www.mybatis.org/mybatis-3/zh/index.html
英文版:http://www.mybatis.org/mybatis-3/
工具
JDK 1.6及以上版本
MyBatis 3.30版本
MySQL 6.3版本
Eclipse4 及以上版本
Apache Maven 構(gòu)建工具
項目源碼下載地址:https://github.com/JFAlex/MyBatis/tree/master/MyBatis_No.3/alex
update用法
一個簡單的通過主鍵更新數(shù)據(jù)的UPDATE的列子:
在UserMapper接口中添加一個修改的方法:
public int updateById(SysUser sysUser);
這里的sysUser就是新的數(shù)據(jù)對象,然后在XML文件中添加響應(yīng)的映射數(shù)據(jù):
<update id="updateById">
update sys_user set user_name = #{userName},
user_password = #{userPassword},
user_email = #{userEmail}, user_info =
#{userInfo}, head_img = #{headImg
, jdbcType=BLOB}, create_time =
#{createTime,jdbcType=TIMESTAMP}
where id = #{id}
</update>
下面再在UserMapperTest測試類中添加一個測試方法:
@Test
public void testUpateById(){
// 獲取SqlSession
SqlSession sqlSession = getSqlSession();
// 獲取UserMapper接口
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//根據(jù)數(shù)據(jù)庫中的用戶的ID,查詢出用戶數(shù)據(jù)
SysUser user = userMapper.selectById(3L);
System.out.println("修改前數(shù)據(jù)為:" + user);
//修改用戶數(shù)據(jù)
user.setUserName("update");
user.setUserPassword("12345678");
user.setUserEmail("mybatis@my.test");
user.setUserInfo("update data");
//正常情況下應(yīng)該存入一張圖片
user.setHeadImg(new byte[]{1,2,3});
user.setCreateTime(new Date());
//將新建的對象插入數(shù)據(jù)庫中,特別注意這里的返回值result是執(zhí)行的SQL影響的行數(shù)
int result = userMapper.updateById(user);
System.out.println("修改成功數(shù)據(jù)條數(shù)為:" + result);
//修改后再次獲取用戶數(shù)據(jù)
SysUser user2 = userMapper.selectById(3L);
System.out.println("修改后數(shù)據(jù)為:" + user2);
}catch(Exception e){
e.printStackTrace();
} finally {
//為了不對其他的測試造成影響,此處進行數(shù)據(jù)回滾
//由于默認的sqlSessionFactory.openSession()是不會自動提交的舌涨,因此如果不手動進行commit操作,數(shù)據(jù)也不會寫入數(shù)據(jù)庫
sqlSession.rollback();
// 關(guān)閉SqlSession
sqlSession.close();
}
}
測試方法首先從數(shù)據(jù)庫中根據(jù)id獲取出一個已經(jīng)存在的用戶的信息扔字,然后對查詢出來的數(shù)據(jù)進行修改囊嘉,當然不能修改主鍵id的值,然后調(diào)用修改的接口革为,將修改后的數(shù)據(jù)更新扭粱,最后在執(zhí)行一個根據(jù)id獲取用戶信息,即為修改后的用戶信息震檩。
右鍵單擊測試類焊刹,在Run As選項中選擇JUnit Test執(zhí)行測試。測試通過恳蹲,控制臺將會打印如下日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1665404403.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 3(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 3, insert, 123456, mybatis@my.test, <<BLOB>>, <<BLOB>>, 2017-08-15 10:01:41.0
DEBUG [main] - <== Total: 1
修改前數(shù)據(jù)為:SysUser [id=3, userName=insert, userPassword=123456, userEmail=mybatis@my.test, userInfo=insert data, headImg=[1, 2, 3], createTime=Tue Aug 15 10:01:41 CST 2017]
DEBUG [main] - ==> Preparing: update sys_user set user_name = ?, user_password = ?, user_email = ?, user_info = ?, head_img = ?, create_time = ? where id = ?
DEBUG [main] - ==> Parameters: update(String), 12345678(String), mybatis@my.test(String), update data(String), java.io.ByteArrayInputStream@51c8530f(ByteArrayInputStream), 2017-08-15 14:44:13.635(Timestamp), 3(Long)
DEBUG [main] - <== Updates: 1
修改成功數(shù)據(jù)條數(shù)為:1
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 3(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 3, update, 12345678, mybatis@my.test, <<BLOB>>, <<BLOB>>, 2017-08-15 14:44:14.0
DEBUG [main] - <== Total: 1
修改后數(shù)據(jù)為:SysUser [id=3, userName=update, userPassword=12345678, userEmail=mybatis@my.test, userInfo=update data, headImg=[1, 2, 3], createTime=Tue Aug 15 14:44:14 CST 2017]
DEBUG [main] - Rolling back JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Returned connection 1665404403 to pool.
我們還可以通過設(shè)置UPDATE語句中的WHERE條件虐块,來修改一條或者多條數(shù)據(jù),基本的UPDATE用法就這么簡單嘉蕾。
delete用法
delete同update相似贺奠,我們通過主鍵來完成刪除數(shù)據(jù)。
在UserMapper接口中添加方法:
public int deleteById(Long id);
根據(jù)主鍵刪除數(shù)據(jù)時错忱,如果主鍵只有一個字段儡率,那么就可以像這個方法一樣使用一個參數(shù),這個方法對應(yīng)的UserMapper.xml中的代入如:
<update id="updateById">
update sys_user set user_name = #{userName},
user_password = #{userPassword},
user_email = #{userEmail}, user_info =
#{userInfo}, head_img = #{headImg
, jdbcType=BLOB}, create_time =
#{createTime,jdbcType=TIMESTAMP}
where id = #{id}
</update>
如果我們修改UserMapper接口中方法中傳遞的參數(shù)以清,如下:
public int deleteById(SysUser sysUser);
我們XML中的代碼不需要進行任何修改儿普,也是可以正確執(zhí)行的。
然后在測試類UserMapperTest中添加一個測試方法:
@Test
public void testDeleteById(){
// 獲取SqlSession
SqlSession sqlSession = getSqlSession();
// 獲取UserMapper接口
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//根據(jù)數(shù)據(jù)庫中的用戶的ID,查詢出用戶數(shù)據(jù)
SysUser user1 = userMapper.selectById(1L);
SysUser user2 = userMapper.selectById(2L);
//此時還能夠查詢出數(shù)據(jù)
System.out.println("執(zhí)行刪除前:"+user1);
System.out.println("執(zhí)行刪除前:"+user2);
//執(zhí)行刪除
int result1 = userMapper.deleteById(1L);
int result2 = userMapper.deleteById(user2);
System.out.println("刪除id=1數(shù)據(jù)條數(shù):" + result1);
System.out.println("刪除id=2數(shù)據(jù)條數(shù):" + result2);
SysUser user11 = userMapper.selectById(1L);
SysUser user22 = userMapper.selectById(2L);
//此時還能夠查詢出數(shù)據(jù)
System.out.println("執(zhí)行刪除后:"+user11);
System.out.println("執(zhí)行刪除后:"+user22);
}catch(Exception e){
e.printStackTrace();
} finally {
//為了不對其他的測試造成影響掷倔,此處進行數(shù)據(jù)回滾
//由于默認的sqlSessionFactory.openSession()是不會自動提交的眉孩,因此如果不手動進行commit操作,數(shù)據(jù)也不會寫入數(shù)據(jù)庫
sqlSession.rollback();
// 關(guān)閉SqlSession
sqlSession.close();
}
}
右鍵單擊測試類,在Run As選項中選擇JUnit Test執(zhí)行測試浪汪。測試通過巴柿,控制臺將會打印如下日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1665404403.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 1(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 1, admin, 123456, admin@mybais.alex, <<BLOB>>, <<BLOB>>, 2017-08-09 15:26:52.0
DEBUG [main] - <== Total: 1
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 2(Long)
TRACE [main] - <== Columns: id, user_name, user_password, user_email, user_info, head_img, create_time
TRACE [main] - <== Row: 2, test, 123456, test@mybais.alex, <<BLOB>>, <<BLOB>>, 2017-08-09 15:27:30.0
DEBUG [main] - <== Total: 1
執(zhí)行刪除前:SysUser [id=1, userName=admin, userPassword=123456, userEmail=admin@mybais.alex, userInfo=管理員, headImg=null, createTime=Wed Aug 09 15:26:52 CST 2017]
執(zhí)行刪除前:SysUser [id=2, userName=test, userPassword=123456, userEmail=test@mybais.alex, userInfo=測試用戶, headImg=null, createTime=Wed Aug 09 15:27:30 CST 2017]
DEBUG [main] - ==> Preparing: delete from sys_user where id = ?
DEBUG [main] - ==> Parameters: 1(Long)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: delete from sys_user where id = ?
DEBUG [main] - ==> Parameters: 2(Long)
DEBUG [main] - <== Updates: 1
刪除id=1數(shù)據(jù)條數(shù):1
刪除id=2數(shù)據(jù)條數(shù):1
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 1(Long)
DEBUG [main] - <== Total: 0
DEBUG [main] - ==> Preparing: select * from sys_user where id = ?
DEBUG [main] - ==> Parameters: 2(Long)
DEBUG [main] - <== Total: 0
執(zhí)行刪除后:null
執(zhí)行刪除后:null
DEBUG [main] - Rolling back JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@63440df3]
DEBUG [main] - Returned connection 1665404403 to pool.
項目源碼下載地址:https://github.com/JFAlex/MyBatis/tree/master/MyBatis_No.3/alex