文章目錄
XML映射文件介紹(實現簡單的CRUD)
關於XML映射文件,在之前也提到過,Mybatis根據映射文件,創建一個接口的動態代理。使用者可以使用這個代理對象對數據庫進行操作。
標籤簡介
insert
、update
、delete
標籤
分別映射插入、更新、刪除語句。
id屬性
用於在這個名稱空間(名稱空間由<mapper/>
的namespace
屬性指定,指定爲接口的全限定類名),唯一標識這個元素,可以被引用(這個後面再講)。id通常被命名爲對應的方法名。
parameterType屬性
用於指定傳入這條語句的參數的類型。Mybatis會自動推斷,所以這個屬性可以不指定
select
標籤
映射查詢語句。
它的id屬性和parameterType屬性的作用和上面的三個標籤完全一致。
resultType屬性
用於指定返回結果的類型。如果要返回一個集合,其中包含了多個javabean,則parameterType的類型爲javabean的全限定類名(而不是集合類型),根據接口中方法的返回值,可以確定返回的時單個的javabean還時javabean的集合
與resultMap屬性只能設置其中一個
resultMap屬性
用來引用一個<resultMap/>
。
與resultType屬性只能設置其中一個
resultMap
標籤
描述如何將查詢到的結果集封裝到javabean中。
在這裏,先只做簡單的介紹
id屬性
該名稱空間中的唯一標識,被其他標籤的resultMap
屬性引用。
type屬性
封裝後得到的javabean的全限定類名。
id
子標籤
用於將記錄中的主鍵映射到javabean中的一個字段。
column
屬性用於指定列名property
屬性用於指定字段名
column指定的列於property指定的字段相對應
result
子標籤
用於將記錄中的一列映射到javabean中的一個字段。
column
屬性用於指定列名property
屬性用於指定字段名
column指定的列於property指定的字段相對應
id和result的用法很相似,id用來指定主鍵列,result用來指定列(也可以用於主鍵那一列)。1
關於傳參的問題
對於簡單的CRUD,不涉及到多個參數,所以僅作簡單的說明。
使用#{參數名}
獲取參數,如果參數是一個對象的引用(且僅有一個參數),想要獲取其中的字段,可以使用#{字段}
。
映射文件的配置
<?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="com.test.dao.ICollegeDao">
<insert id="addCollege">
insert into college(college_name) values (#{name})
</insert>
<update id="updateCollege">
update college set college_name = #{name} where college_id = #{id}
</update>
<delete id="deleteCollegeById">
delete from college where id = #{id}
</delete>
<select id="getCollegeById" resultMap="college">
select * from college where college_id = #{id};
</select>
<select id="getAll" resultMap="college">
select * from college
</select>
<resultMap id="college" type="com.test.entity.College">
<id column="college_id" property="id"></id>
<result column="college_name" property="name"></result>
</resultMap>
</mapper>
問題:沒有提交
public void getCollege() throws IOException {
//自己封裝了一個獲取sqlSessionFactory的方法
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
ICollegeDao collegeDao = sqlSession.getMapper(ICollegeDao.class);
College college = collegeDao.getCollegeById(1);
collegeDao.addCollege(new College(null, "經濟管理學院"));
System.out.println(collegeDao.getAll());
}
執行上述代碼後,輸出結果如下
查看數據表
問題產生原因:沒有提交
解決方法:手動提交或設置sqlSession自動提交
先來介紹手動提交的方法:
public void getCollege() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
ICollegeDao collegeDao = sqlSession.getMapper(ICollegeDao.class);
College college = collegeDao.getCollegeById(1);
collegeDao.addCollege(new College(null, "經濟管理學院"));
System.out.println(collegeDao.getAll());
sqlSession.commit();
sqlSession.close();
}
關於sqlSession要注意的地方
sqlSession
是否自動提交
sqlSessionFactory.openSession()
可以用來傳遞一個boolean參數,用來指定是否自動提交。
非自動提交(需手動提交):
SqlSession sqlSession = sqlSessionFactory.openSession(false);
/*******等價於*******/
SqlSession sqlSession = sqlSessionFactory.openSession();
自動提交:
SqlSession sqlSession = sqlSessionFactory.openSession(true);
關閉sqlSession
使用完sqlSession
後,一定要關閉sqlSession.close()
,因爲關閉之後就不能在使用,所以不可以線程共享(如果共享,沒辦法保證其他線程不會關閉)。也就是說,每個事務,都需要一個“新的”sqlSession