1. 程式人生 > >MyBatis入門基礎(一) 為什麼要用Mybatis

MyBatis入門基礎(一) 為什麼要用Mybatis

複製程式碼
package com.mybatis.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import com.mybatis.entity.User; /** * @ClassName: MybatisService * @Description: TODO(mybatis入門程式) * @author warcaft * @date 2015-6-27 下午4:49:49 * */ public class MybatisService { // 根據Id查詢使用者資訊,得到一條記錄結果 @Test public void findUserByIdTest() { // mybatis的配置檔案
String resource = "SqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); // 1.建立會話工場,傳入mybatis的配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream);
// 2.通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.通過sqlSession操作資料庫 // 第一個引數:對映檔案中的statement的Id,等於namespace + "." + statement的id; // 第二個引數:指定和對映檔案中所匹配的parameterType型別的引數; // sqlSession.selectOne結果是與對映檔案所匹配的resultType型別的物件; // selectOne:查詢一條結果 User user = sqlSession.selectOne("test.findUserById", 1); System.out.println(user.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 根據姓名模糊查詢使用者資訊,得到一條或多條記錄結果 @Test public void findUserByNameTest() { // mybatis的配置檔案 String resource = "SqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); // 1.建立會話工場,傳入mybatis的配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); // 2.通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.通過sqlSession操作資料庫 // 第一個引數:對映檔案中的statement的Id,等於namespace + "." + statement的id; // 第二個引數:指定和對映檔案中所匹配的parameterType型別的引數; // sqlSession.selectOne結果是與對映檔案所匹配的resultType型別的物件; // list中的user和resultType型別一致 List<User> list = sqlSession.selectList("test.findUserByName", "小"); System.out.println(list); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 新增使用者 @Test public void insertUserTest() { // mybatis的配置檔案 String resource = "SqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); // 1.建立會話工場,傳入mybatis的配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); // 2.通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); //插入使用者的物件 User user = new User(); user.setUsername("小紅"); user.setBirthday(new Date()); user.setSex("1"); user.setAddress("上海"); // 3.通過sqlSession操作資料庫 // 第一個引數:對映檔案中的statement的Id,等於namespace + "." + statement的id; // 第二個引數:指定和對映檔案中所匹配的parameterType型別的引數; // sqlSession.selectOne結果是與對映檔案所匹配的resultType型別的物件; sqlSession.insert("test.insertUser", user); //執行提交事務 sqlSession.commit(); //專案中經常需要 獲取新增的使用者的主鍵 System.out.println(user.getId()); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 根據Id刪除使用者 @Test public void deleteUserTest() { // mybatis的配置檔案 String resource = "SqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); // 1.建立會話工場,傳入mybatis的配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); // 2.通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.通過sqlSession操作資料庫 // 第一個引數:對映檔案中的statement的Id,等於namespace + "." + statement的id; // 第二個引數:指定和對映檔案中所匹配的parameterType型別的引數; // sqlSession.selectOne結果是與對映檔案所匹配的resultType型別的物件; //傳入Id,刪除使用者 sqlSession.delete("test.deleteUser", 7); //執行提交事務 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 根據Id更新使用者資訊 @Test public void updateUserTest() { // mybatis的配置檔案 String resource = "SqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); // 1.建立會話工場,傳入mybatis的配置檔案資訊 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); // 2.通過工廠得到SqlSession sqlSession = sqlSessionFactory.openSession(); //更新使用者的資訊 User user = new User(); user.setId(2); user.setUsername("小黑"); user.setBirthday(new Date()); user.setSex("2"); user.setAddress("上海"); // 3.通過sqlSession操作資料庫 // 第一個引數:對映檔案中的statement的Id,等於namespace + "." + statement的id; // 第二個引數:指定和對映檔案中所匹配的parameterType型別的引數; // sqlSession.selectOne結果是與對映檔案所匹配的resultType型別的物件; //更具Id更新使用者 sqlSession.update("test.updateUser", user); //執行提交事務 sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
複製程式碼