文章目錄
mybatis
環境配置
搭建
注意事項
1)創建IUserDao.xml和IUserDao.java時,名稱時爲了和以前的只是保持一致。mybatis中,持久層的操作接口名稱和映射文件也叫做:Mapper。IUserDao=IUserMapper
2)在IDAEA中創建目錄時,與包不一樣
創建包:com.xxx.dao三級目錄
創建目錄:com.xxx.dao一級目錄
3)mybatis的映射配置文件位置必須與dao接口的包結構相同
4)映射配置文件的mapper標籤namespace屬性的取值必須是dao接口的全限定類名
5)映射配置文件的操作配置,id屬性的取值必須時dao接口的方法名
遵從345點後,開發中就無需再寫dao的實現類
開發
步驟
第一步:讀取配置文件
第二步:創建SqlSessionFactory工廠
第三步:創建SqlSession
第四步:創建Dao接口的代理
第五步:執行dao的方法
第六步:釋放資源
注意事項
不要忘記在映射配置中告知mybatis要封裝到哪個實體類中
配置方法:指定實體類的全限定類名
如果在註解開發:
配置方法:應該使用class屬性指定被註解的dao全限定類名
<mapper class="com.itheima.dao.IUserDao"/>
註解開發
減少編寫 Mapper 映射 文件
將傳入的數據都當成一個字符串,會對自動傳入的數據加一個雙引號。如:order by #user_id#,如果傳入的值是111,那麼解析成sql時的值爲order by “111”, 如果傳入的值是id,則解析成的sql爲order by “id”.
1)在很大程度上防止sql注入
2)使用#{}格式的語法會導致MyBatis創建預處理語句屬性並以它爲背景設置安全的值(比如?)
$
將傳入的數據直接顯示生成在sql中。如:order by ,如果傳入的值是111,那麼解析成sql時的值爲order by user_id, 如果傳入的值是id,則解析成的sql爲order by id.
1)不能防止sql注入
2)一般用於傳入數據庫對象,例如傳入表名
常見註解
@Insert:實現新增
@Update:實現更新
@Delete:實現刪除
@Select:實現查詢
@Result:實現結果集封裝
@Results:可以與@Result 一起使用,封裝多個結果集
@ResultMap:實現引用
@Results 定義的封裝
@One:實現一對一結果集封裝
@Many:實現一對多結果集封裝 @SelectProvider: 實現動態 SQL 映射 @CacheNamespace:實現註解二級緩存的使
CRUD操作
select
@Select("select * from user where id = #{uid} ")
User findById(Integer userId);
insert
@Insert("insert into user(username,sex,birthday,address)values(#{username},#{sex},#{birthday},#{address} )")
int saveUser(User user);
update
@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id} ")
int updateUser(User user)
delete
@Delete("delete from user where id = #{uid} ")
int deleteUser(Integer userId);
數據不一致(別名問題)
@Results&&@Result
@Results(id="userMap", /*唯一標識,使其他語句可以使用*/ value= {
@Result(id=true,column="id",property="userId"),
@Result(column="username",property="userName"),
@Result(column="sex",property="userSex"),
@Result(column="address",property="userAddress"),
@Result(column="birthday",property="userBirthday") })
@ResultMap("userMap") //@ResultMap(value={"userMap"})
複雜查詢
@Results
@Results 註解 代替的是標籤 該註解中可以使用單個@Result 註解,也可以使用@Result 集合 @Results({@Result(),@Result()})或@Results(@Result())
@Resutl 註解 代替了 標籤和標籤
@Result 中 屬性介紹:
id 是否是主鍵字段(主鍵)
column 數據庫的列名
property 需要裝配的屬性名(實現類的屬性名)
one 需要使用的@One 註解(@Result(one=@One)())) many 需要使用的@Many 註解(@Result(many=@many)()))
@One 註解(一對一)
代替了標籤,是多表查詢的關鍵,在註解中用來指定子查詢返回單一對象。
@One 註解屬性介紹:
select 指定用來多表查詢的 sqlmapper fetchType 會覆蓋全局的配置參數:
lazyLoadingEnabled。
使用格式:
@Result(column=" “,property=”",one=@One(select=""))
例如:
@Results(id="accountMap", value= {
@Result(id=true,column="id",property="id"),
@Result(column="uid",property="uid"),
@Result(column="money",property="money"),
@Result(column="uid", property="user",
one=@One(select="com.itheima.dao.IUserDao.findById", fetchType=FetchType.LAZY) ) })
@Many 註解(多對一)
代替了標籤,是是多表查詢的關鍵,在註解中用來指定子查詢返回對象集合。
注意:聚集元素用來處理“一對多”的關係。需要指定映射的 Java 實體類的屬性,屬性的 javaType (一般爲 ArrayList)但是註解中可以不定義;
使用格式: @Result(property="",column="",many=@Many(select=""))
@Results(id="userMap", value= {
@Result(id=true,column="id",property="userId"),
@Result(column="username",property="userName"),
@Result(column="sex",property="userSex"),
@Result(column="address",property="userAddress"),
@Result(column="birthday",property="userBirthday"),
@Result(column="id",property="accounts",
many=@Many( select="com.itheima.dao.IAccountDao.findByUid", fetchType=FetchType.LAZY ) ) })
緩存配置
一級緩存
默認開啓
二級緩存
1)SqlMapConfig 中開啓二級緩存支持
<!-- 配置二級緩存 -->
<settings>
<!-- 開啓二級緩存的支持 -->
<setting name="cacheEnabled" value="true"/> </settings>
2)在持久層接口中使用註解配置二級緩存
@CacheNamespace(blocking=true)//mybatis 基於註解方式實現配置二級緩存
public interface IUserDao {}