mybatis中注解就是简单不需要写配置文件,适合简单的数据处理,理解起来比较容易,不动态生成SQL时候可以用用。 需要绑定,有些时候不如配置文件,配置文件扩展强。 选择合适的方式应用在合适的场景,注解主要应用于sql语句比较简单容易理解的情况下可读性高;生成动态sql时用xml配置文件要更简洁,扩展性强
常用的注解和xml的对应关系
@CacheNamespace 类 <cache>
@CacheNamespaceRef 类 <cacheRef>
@Results 方法 <resultMap>
@Result 方法 <result> <id>
@One 方法 <association>
@Many 方法 <collection>
@select <select>
@Insert <insert>
@Update <update>
@Delete 方法 <delete>
@InsertProvider <insert> 允许创建动态SQL
@UpdateProvider <update> 允许创建动态SQL
@DeleteProvider <delete> 允许创建动态SQL
@SelectProvider <select> 允许创建动态SQL
@Param 参数 N/A 如果你的映射器的方法需要多个参数, 这个注解可以被应用于映射器的方法 参数来给每个参数一个名字。否则,多 参数将会以它们的顺序位置来被命名 (不包括任何 RowBounds 参数) 比如。 #{param1} , #{param2} 等 , 这 是 默 认 的 。 使用 @Param(“person”),参数应该被命名为 #{person}。
@Options 方法 映射语句的属性 这个注解提供访问交换和配置选项的 宽广范围, 它们通常在映射语句上作为 属性出现。 而不是将每条语句注解变复 杂,Options 注解提供连贯清晰的方式 来访问它们
注解样例和xml配置样例
举几个比较典型和常用的
一对一关联查询
注解方式
@Select("select * from authority")
@Results(id="au",
value=@Result(column="uid",
property="user",
one=@One(select="findUserByid",
fetchType=FetchType.LAZY)))
List<Authority> findAll();
@Select里面填写要查询的主表的sql语句
@Results里面映射一个id="au"的返回结果集
value=@Result()表示某一属性的映射关系
column为对应从表的外键名
property为主表实体类的从表实体类属性名
one表示一对一映射
fetchType=FetchType.LAZY表示为惰性加载,当查询的结构数据需要用到从表的数据才会调用select中的从表的查询方法
select为关联查询的另一个从表的查询方法
uid为select里的参数
findUserByid为mapper中定义的方法
@Select("select * from user where id = #{id}")
User findUserByid(int id);
此方法可以在xml中配置也可以在本方法中用注解配置
xml中配置方式
<resultMap type="com.jt.mybatis.entity.Authority" id="au">
<association property="user" column="uid" javaType="com.jt.mybatis.entity.User" select="findByUserId">
</association>
</resultMap>
<select id="findAll" resultMap="au">
select * from authority
</select>
<select id="findUserByid" resultType="com.jt.mybatis.entity.User">
select * from user where id= #{id}
</select>
测试方法
@Test
public void testA(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
mapper.findAll().get(0).getUser();
}
一对多关联查询
xml配置方式
<resultMap type="com.jt.mybatis.entity.User" id="user">
<id column="id" property="id" />
<collection property="authoritieList" column="id"
fetchType="lazy" select="findAuthorityByUid">
<id column="id" property="id" />
</collection>
</resultMap>
<select id="findUserByUserName" resultMap="user">
select * from user
where username = #{username}
</select>
<select id="findAuthorityByUid" resultType="com.jt.mybatis.entity.Authority">
select * from
authority where uid = #{uid}
</select>
注解方式
@Select("select * from user where username = #{username}")
@Results(id="user",
value=@Result(column="id",
property="authoritieList",
many=@Many(fetchType=FetchType.LAZY,
select="findAuthorityByUid")))
User findUserByUserName(String username);
@Select("select * from authority where uid = #{uid}")
List<Authority> findAuthorityByUid(int uid);
many表示一对多映射
测试方法
@Test
public void testB(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
mapper.findUserByUserName("admin").getAuthoritieList();
}
动态sql
注解方式
@SelectProvider(type=AuthorityProvider.class,method="returnSelectSQL")
List<Authority> findByIdAndUid(Authority authority);
class AuthorityProvider{
public String returnSelectSQL(Authority authority){
SQL sql = new SQL(){{
SELECT("*");
FROM("authority");
if(authority.getId() != 0){
WHERE("id = " + authority.getId());
}
if(authority.getUid() != 0){
WHERE("uid = " + authority.getUid());
}
}};
return sql.toString();
}
}
//用XXXProvider的注解是动态生成sql语句的,
//type=AuthorityProvider.class为生成动态语句的具体类
//method="returnSelectSQL"为生成动态语句的方法
//SQL类为动态生成sql的类
测试方法
@Test
public void testC(){
AuthorityMapper mapper = session.getMapper(AuthorityMapper.class);
Authority authority = new Authority();
mapper.findByIdAndUid(authority);
//执行此语句返回的sql语句为DEBUG [main] - ==> Preparing: SELECT * FROM authority
authority.setId(1);
mapper.findByIdAndUid(authority);
//执行此语句返回的sql语句为DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1)
authority.setUid(2);
mapper.findByIdAndUid(authority);
//执行此语句返回的sql语句为DEBUG [main] - ==> Preparing: SELECT * FROM authority WHERE (id = 1 AND uid = 2)
}