Mybatis细节补充
一对一、一对多查询配置
这里的一对多就是指一个表的记录对应着另一个表,多对多就是指多个表对应着另外多个表。本文中以Account为例,一个Account表对应着一个user,这就是一对一;而一个Role对用多个user,一个user也对应着多个role,这就是一对多。
- 使用xml文件形式进行配置
一对一和一对多的配置区别不大,主要就是在dao.xml文件配置中有点区别,其他步骤都一样。首先就是自定义类,然后配置相应的日常主文件xml,接着就是对不同的dao.xml文件的配置。最后可以测试结果。
一对一的dao.xml文件的配置,以Account表为例,利用Account表查询到user表中的详细信息,具体配置如下:
<resultMap id="accountUserMap" type="account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<association property="user" column="uid" javaType="user">
<id property="userId" column="id"></id>
<!--非主键字段的对应-->
<result property="userName" column="username"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="accountUserMap">
select u.*,a.id as aid,a.uid,a.money from account a , user u where u.id = a.uid;
</select>
一对多的配置如下,以Role为例查询到用户信息,具体配置如下:
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="user">
<id column="id" property="id"></id>
<result column="username" property="username"></result>
<result column="address" property="address"></result>
<result column="sex" property="sex"></result>
<result column="birthday" property="birthday"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
- 使用注解形式进行配置
实现查阅account表能得到user的具体信息,即一对一的实现,one就是注解标志,其中的fetchType是加载类型,默认有EAGER、LAZY和DEFAULT三种类型。一般来说,若是一对一一般采用立即加载,即为EAGER;若是一对多则采用延迟加载,即为LAZY。
//位于AccountDao.interface下
@Select("select * from account")
@Results(id="accountMap",value = {
@Result(id=true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "money",property = "money"),
@Result(property = "user",column = "uid",one=@One(select="com.cb.dao.userDao.findById",fetchType= FetchType.EAGER))
})
List<Account> findAll();
//位于userDao.interface下
@Select("select * from user where id=#{id} ")
User findById(Integer userId);
实现查阅用户得到多个账户信息,即一对多的实现,与上类似。
//位于userDao.interface下
@Select("select * from user")
@Results(id="userMap",value={
@Result(id=true,column = "id",property = "userId"),
@Result(column = "username",property = "userName"),
@Result(column = "address",property = "userAddress"),
@Result(column = "sex",property = "userSex"),
@Result(column = "birthday",property = "userBirthday"),
@Result(property = "accounts",column = "id",
many = @Many(select = "com.cb.dao.AccountDao.findAccountByUid",
fetchType = FetchType.LAZY))
})
List<User> findAll();
//位于AccountDao.interface下
@Select("select * from account where uid = #{userId}")
List<Account> findAccountByUid(Integer userId);
配置一级缓存和二级缓存
使用xml文件进行配置
- 一级缓存
默认开启的,如果要配置的话首先在主配置文件中(SqlMapConfig.xml)加上配置项:
<!--配置参数-->
<settings>
<!--开启Mybatis支持延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
然后根据映射的不同在各自的dao.xml文件中进行相应的配置
一对一映射如下,在association标签中加入column和select,select属性指定的内容:查询用户的唯一标识。类似于windows中的路径,能代表唯一性。column属性指定的内容:用户根据id查询时,所需要的参数的值。
<association property="user" column="uid" javaType="user" select="com.cb.dao.userDao.findById"></association>
多对多映射如下,在collection标签中加入column和select。
<collection property="accounts" ofType="account" select="com.cb.dao.AccountDao.findAccountByUid" column="id"></collection>
- 二级缓存
首先在主配置文件中(SqlMapConfig.xml)加上配置项:
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
然后在相应的dao.xml文件中进行相应的声明配置,加入下面一行配置:
<!--开启user支持二级缓存-->
<cache/>
使用注解配进行二级缓存的配置
默认一级缓存开启,所以介绍一下二级缓存的配置,与xml类似,首先在主文件中加上setting配置项,同上。然后使用注解在相应的dao接口文件中进行申明,如下在useDao接口中开启。
@CacheNamespace(blocking = true)
public interface userDao
用注解进行基本CURD操作配置
用注解进行Mybatis的配置比起xml文件简单的多,当然主文件xml是必不可少的,但是它避免了需要对每一个dao文件进行xml配置的繁琐操作。注解配置针对CRUD一共有四个注解
-
@Select
@Select("select * from user") List<User> findAll();
-
@Insert
@Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
void saveUser(User user);
-
@Update
@Update("update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} where id=#{id}") void updateUser(User user);
-
@Delete
@Delete("delete from user where id=#{id} ")
void deleteUser(Integer userId);