Mybatis的注解开发
1. 配置开发环境
- 主配置文件配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbcConfig.properties"></properties> <typeAliases> <package name="itlearn.zhi.domain"></package> </typeAliases> <!-- 配置环境 --> <environments default="mysql"> <!-- 配置mysql的环境 --> <environment id="mysql"> <!-- 配置事务的类型 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源(连接池) --> <dataSource type="POOLED"> <!-- 配置连接事务的四个基本信息 --> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <!-- 配置带有注解的接口所在包 --> <package name="itlearn.zhi.dao"></package> </mappers> </configuration>
2. 使用注解完成单表操作
package itlearn.zhi.dao; import itlearn.zhi.domain.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface IUserDao { @Select("select * from user") public List<User> findAll(); //更新 @Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id =#{id}") public void updateUser(User user); //根据ID查询用户 @Select("select * from user where id = #{id}") public User findUserById(Integer id); //插入用户 @Insert("insert into user values(null,#{username},#{birthday},#{sex},#{address})") public void insertUser(User user); @Delete("delete from user where id = #{id}") public void deleteUser(Integer id); //模糊查询 @Select("select * from user where username like #{username}") public List<User> findUserAsLike(String username); }
3. 使用注解解决实体类与数据表字段的不同名映射【重点】
@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") })
4. 使用注解完成多表操作【重点:one和many的使用】
package itlearn.zhi.dao; import itlearn.zhi.domain.Account; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface IAccountDao { /** * 查询所有账户的同时查询出对应的user用户 * @return */ @Results(id = "accountUser",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 = "itlearn.zhi.dao.IUserDao.findUserById", fetchType = FetchType.EAGER )) }) @Select("select * from account") public List<Account> findAll(); @ResultMap(value={"accountUser"})//指定引用映射关系 @Select("select * from account where UID = #{id}") public Account findAccountById(Integer id); }
注意:
1. 使用@Results()配置实体类与数据表对应关系 2. id="userMap":为该配置设置一个唯一标识,当其他方法也需要使用该配置时候,直接通过 @ResultMap("accountUser")调用。 3. value={}:在{}中使用@Result配置实体类与数据表的对应关系 4. @Result():配置实体类属性与数据表字段对应。 5. id=true: 主键配置 6. property:实体类中的属性名 7. column:数据表字段名 在配置User实体类的映射属性的时候,使用的@Result()中的属性解释: 1. property:account实体类中User实体类的映射属性名( private User user;即user) 2. column:查询user要使用到的字段 3. one:当account实体类中对User的映射属性不是集合映射,而是单个映射的时候,使用one,集合映射的话使用many 4. select:IUserDao中执行查询的方法的全限定类名 5. fetchType:执行加载的方式{FetchType.EAGER:立即加载、FetchType.LAZY:延迟加载}
package itlearn.zhi.dao; import itlearn.zhi.domain.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType; import java.util.List; public interface IUserDao { @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 = "itlearn.zhi.dao.IAccountDao.findAccountById",fetchType = FetchType.LAZY)) }) @Select("select * from user") public List<User> findAll(); //根据ID查询用户 @Select("select * from user where id = #{id}") public User findUserById(Integer id); }
注意:
此处因使用的是集合映射,所以需要使用many来配置。不是集合映射,则需要使用one
5. 使用注解完成Mybatis的二级缓存
注意:一级缓存始终不需要开启,不需要关闭
使用注解开启二级缓存
步骤: { 1. 在SqlMapConfig.xml配置文件中配置参数,开启二级缓存 <!-- 配置开启二级缓存 --> <settings> <setting name="cacheEnabled" value="true"/> </settings> 2. 在需要使用二级缓存的dao接口类上添加:@CacheNamespace(blocking = true) 如: @CacheNamespace(blocking = true) public interface IUserDao { ······ }
注意:注解一定要写在接口类名上方