mybatis中怎么配置注解

发布时间:2021-08-04 14:10:30 作者:Leah
来源:亿速云 阅读:114

mybatis中怎么配置注解,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

注解与xml配置的对应关系

mybatis中注解就是简单不需要写配置文件,适合简单的数据处理,理解起来比较容易,不动态生成SQL时候可以用用。

需要绑定,有些时候不如配置文件,配置文件扩展强。 选择合适的方式应用在合适的场景,注解主要应用于sql语句比较简单容易理解的情况下可读性高;生成动态sql时用xml配置文件要更简洁,扩展性强

常用的注解和xml的对应关系

注解样例和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("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) 
 }

mybatis 注解和xml 优缺点

xml:

增加了xml文件,修改麻烦,条件不确定(ifelse判断),容易出错,特殊转义字符比如大于小于

注释:

  复杂sql不好用,搜集sql不方便,管理不方便,修改需重新编译

#和$区别:

相同

看完上述内容,你们掌握mybatis中怎么配置注解的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. mybatis的xml配置和注解配置
  2. 如何在SpringAOP中配置注解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:Android RecyclerView如何实现多种item布局

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》