mybatis
复习
-
复习延迟加载
-
开启延迟加载
-
sqlMapcoonfig.xml主配置文件开启如下
-
<setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="true"/>
- 配置实现
- select属性指定配置文件 确认sql语句位置
- column属性 获取查询条件的数据(也就是另一张表根据什么列表属性查询的)
- 配置实现
userDao.xml
-
-
<resultMap id="findAllMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<collection property="accounts" column="id" ofType="account" select="com.qqhru.wjy.dao.AccountDao.findById"></collection>
</resultMap>
<select id="findAll" resultMap="findAllMap">
<!-- select user.*, a.id as aid, a.money as amoney from user left join account a on a.uid = user.id-->
select * from user
</select>
accountDao.xml
<select id="findById" resultType="account">
select * from account where uid = #{uid}
</select>
- 复习一级缓存,二级缓存
- 一级缓存, 存储在Sqlsession中
- 二级缓存,存储在SqlSessionFactory中
注解开发
-
环境搭建
-
单表crud
-
@Insert("insert into user (id, username, birthday, sex, address) values (#{id},#{username},#{birthday},#{sex},#{address})") void insertUser(User user); @Update("update user set username = #{username}, address = #{address}, birthday = #{birthday},sex = #{sex} where id = #{id}") void updateUser(User user); @Delete("delete from user where id = #{id} ") void dropUser(Integer id);
-
多表
-
results 和 result 注解
-
ID
-
one
-
many
一对多
@Select(value = "select * from user") @Results(id = "userMap", value = { @Result(id = true, property = "id",column = "id"), @Result(property = "username", column = "username"), @Result(property = "birthday", column = "birthday"), @Result(property = "sex", column = "sex"), @Result(property = "address", column = "address"), @Result(property = "accounts", column = "id" , many = @Many (select = "com.qqhru.wjy.dao.AccountDao.findById", fetchType = FetchType.LAZY)) }) List<User> findAll();
一对一
@Select(value = "select * from account") @Results(id = "AccountMap", value = { @Result(id = true,property = "ID", column = "ID"), @Result(property = "UID", column = "UID"), @Result(property = "MONEY", column = "MONEY"), @Result(property = "user", column = "UID", one = @One( select = "com.qqhru.wjy.dao.UserDao.findById", fetchType = FetchType.LAZY )) }) List<Account> findAll();
缓存
-