mybatis(3)---使用mybatis對錶進行CRUD操作
阿新 • • 發佈:2019-01-22
前一節介紹過如何使用mybatis查詢資料庫表t_user中的資料,本節介紹如何使用mybatis對資料庫表t_user進行crud操作
步驟如下:
1、UserMapper.xml檔案內容如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 為這個mapper指定一個唯一的namespace, namespace的值習慣上設定成包名+sql對映檔名,這樣就能夠保證namespace的值是唯一的 例如namespace="com.cn.mapping.userMapper"就是com.cn.mapping(包名)+userMapper(userMapper.xml檔案去除字尾) --> <mapper namespace="com.cn.mapping.UserMapper"> <!-- 在select標籤中編寫查詢的SQL語句, 設定select標籤的id屬性為selectUser, id屬性值必須是唯一的,不能夠重複 使用parameterType屬性指明查詢時使用的引數型別, resultType屬性指明查詢返回的結果集型別 resultType="com.cn.vo.UserVO" 就表示將查詢結果封裝成一個UserVO類的物件返回 UserVO類就是t_user表所對應的實體類 --> <!-- 根據id查詢得到一個user物件 --> <select id="selectUser" parameterType="int" resultType="com.cn.vo.UserVO"> select * from t_user where id=#{id} </select> <!-- 2、建立新使用者 因為建立新使用者需要傳遞引數,引數用parameterType指定 #{username},其中username為實體類UserVO中的欄位,必須對應 --> <insert id="insertUser" parameterType="com.cn.vo.UserVO"> insert into t_user( id, username, password )values( #{id}, #{username}, #{password} ) </insert> <!-- 3、修改使用者 --> <update id="updateUser" parameterType="com.cn.vo.UserVO"> update t_user t set t.username=#{username}, t.password=#{password} where t.id=#{id} </update> <!-- 4、刪除使用者 根據id刪除,需要傳遞的引數為int型別 --> <delete id="deleteUser" parameterType="int"> delete t.* from t_user t where t.id=#{id} </delete> <!-- 5、查詢所有的使用者 查詢無需傳遞引數不需要parameterType,但是查詢需要返回結果集,需要設定結果型別resultType --> <select id="selectAllUser" resultType="com.cn.vo.UserVO"> select t.* from t_user t </select> </mapper>
2、測試
crud測試需要建立session工廠和session.這些步驟都是公用的,可以建立一個工具類,程式碼如下:
package com.cn.utils; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * mybatis工具類 * */ public class MybatisUtil { /** * 建立sqlSessionFacoty * @throws IOException * */ public static SqlSessionFactory getSqlSessionFactory(){ String resource="mybatis.cfg.xml"; Reader reader=null; SqlSessionFactory sqlSessionFactory=null; try{ //本次不使用類載入器載入配置檔案,而是通過 mybatis提供的Resources類載入mybatis的配置檔案 reader=Resources.getResourceAsReader(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); }catch(Exception e){ e.printStackTrace(); } return sqlSessionFactory; } /** * 得到sqlSession * 適應於:提交事務操作的增加、刪除和修改操作的sqlSession獲取 * */ public static SqlSession getSqlSession(boolean isAutoCommit){ return getSqlSessionFactory().openSession(isAutoCommit); } /** * 得到sqlSession * 適用於查詢方式的獲得sqlSession * */ public static SqlSession getSqlSession(){ return getSqlSessionFactory().openSession(); } }
2.1、通過使用者id查詢使用者測試程式碼如下:
package com.cn.test; import org.apache.ibatis.session.SqlSession; import com.cn.utils.MybatisUtil; import com.cn.vo.UserVO; /** * 通過id查詢使用者測試 * */ public class SelectUserById { public static void main(String[] args) { //1.通過MybatisUtil工具類獲取sqlSession SqlSession sqlSession=MybatisUtil.getSqlSession(); //2.對映sql的標識字串 String statement="com.cn.mapping.UserMapper.selectUser"; //3.執行查詢操作 UserVO userVO=sqlSession.selectOne(statement,1); //4.列印結果 System.out.println(userVO.toString()); } }
2.2、查詢所有使用者,測試程式碼如下:
package com.cn.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.cn.utils.MybatisUtil;
import com.cn.vo.UserVO;
/**
* 查詢所有使用者測試
* */
public class SelectAllUser {
public static void main(String[] args) {
//1.通過mybatis工具類獲得sqlSession
SqlSession sqlSession=MybatisUtil.getSqlSession();
//2.對映sql的標識字串
String statement="com.cn.mapping.UserMapper.selectAllUser";
//3.執行session操作
List<UserVO> userList=sqlSession.selectList(statement);
//4.迴圈遍歷結果集
for(UserVO userVO:userList){
System.out.println(userVO.toString());
}
}
}
2.3、新增使用者,測試程式碼如下:
package com.cn.test;
import org.apache.ibatis.session.SqlSession;
import com.cn.utils.MybatisUtil;
import com.cn.vo.UserVO;
public class InsertUser {
public static void main(String[] args) {
//1.工具類獲得sqlSession
SqlSession session=MybatisUtil.getSqlSession(true);
//2.sql對映標識字串
String statement="com.cn.mapping.UserMapper.insertUser";
//3.執行session操作
UserVO userVO=new UserVO(1101,"Rise","123456");
int result=session.insert(statement,userVO);
System.out.println(result);
}
}
2.4、刪除使用者,測試程式碼如下:
package com.cn.test;
import org.apache.ibatis.session.SqlSession;
import com.cn.utils.MybatisUtil;
/**
* 刪除使用者
* */
public class DeleteUser {
public static void main(String[] args) {
//1.獲得sqlSession
SqlSession session=MybatisUtil.getSqlSession(true);
//2.執行sql對映標識的字串
String statement="com.cn.mapping.UserMapper.deleteUser";
//3.執行session操作
int result=session.delete(statement, 1101);
System.out.println(result);
}
}
2.5、修改使用者,測試程式碼如下:
package com.cn.test;
import org.apache.ibatis.session.SqlSession;
import com.cn.utils.MybatisUtil;
import com.cn.vo.UserVO;
/**
* 修改使用者測試
* */
public class UpdateUser {
public static void main(String[] args) {
//1.獲得sqlSession
SqlSession session=MybatisUtil.getSqlSession(true);
//2.執行sql對映的標識字串
String statement="com.cn.mapping.UserMapper.updateUser";
//3.執行session操作
UserVO userVO=new UserVO();
userVO.setId(1);
userVO.setUsername("admin");
userVO.setPassword("123456");
int result=session.update(statement, userVO);
System.out.println(result);
}
}