好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis注解与xml常用语句汇总

前言

mybatis 是一个支持普通sql查询,存储过程和高级映射的优秀持久层框架。mybatis消除了几乎所有的jdbc代码和参数的手工设置以及对结果集的检索封装。mybatis可以使用简单的 xml 或 注解 用于配置和原始映射,将接口和java的pojo(plain old java objects,普通的java对象)映射成数据库中的记录。

本文将给大家详细介绍关于mybatis注解与xml常用语句的相关内容,下面话不多说了,来一起看看详细的介绍吧

mybatis注解使用

1.简单crud

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public interface usermapper {

  //查询

  @select ( "select * from user where id=#{id}" )

  user selectuser( int id);

  //查询全部

  @select ( "select * from user" )

  list<user> selectuserlist();

  //增加数据

  @insert ( "insert into user (name) values(#{name})" )

  boolean insertuser(string name);

  //修改用户

  @update ( "update user set name=#{name} where id=#{id}" )

  boolean updateuser( @param ( "name" ) string name, @param ( "id" ) int id);

  //删除用户

  @delete ( "delete from user where id=#{id}" )

  boolean deleteuser( int id);

}

2.一对一注解

?

1

2

3

4

5

6

7

8

9

10

@select ( "select * from user" )

@results ({

  @result (id = true ,property = "id" ,column = "id" ), //id=true 对应于主键

  @result (property = "uid" ,column = "uid" ),

  @result (property = "user" ,column = "uid" ,javatype = user. class ,

  one = @one (select = "com.example.dao.userdao.finduserbyid" ,fetchtype = fetchtype. default ))

  //user 对应实体类中一对一的实体类名字,uid表示通过uid外键查询user,javatype表示查询结果

  //映射成user类型对象,one=表示一对xx fetchtype.default默认是立即加载全部查询,使用lazy懒加载需要才查询

})

list<user> selectuserlist();

3,一对多注解

mybatis的xml配置

1.配置resultmap

?

1

2

3

4

5

6

7

<resultmap id= "baseresultmap" type= "xx" >

  <id column= "id" property= "id" jdbctype= "bigint" />

  <result column= "aa" property= "aa" jdbctype= "varchar" />

  <result column= "bb" property= "bb" jdbctype= "integer" />

  <result column= "cc" property= "cc" jdbctype= "decimal" javatype= "java.math.bigdecimal" />

  <result column= "dd" property= "dd" jdbctype= "date" />

</resultmap>

2.通用sql短语

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<sql id= "base_column_list" >

  aa, bb

  </sql>

 

  <sql id= "where" >

  <trim prefix= "where" prefixoverrides= "and|or" >

  < if test= "id != null and id != ''" >

   and t.id = #{id}

  </ if >

  < if test= "content != null and content != ''" >

   and t.content like concat( '%' , #{content}, '%' )

  </ if >

  and t.app_code in

  <foreach item= "item" index= "index" collection= "appcodes"

   open= "(" separator= "," close= ")" >

   #{item}

  </foreach>

  and t.user_id=u.id and t.removed= 0

  </trim>

</sql>

3.需要验证的插入

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<insert id= "insert" parametertype= "xxx"

  usegeneratedkeys= "true" keycolumn= "id" keyproperty= "id" >

  insert into xxx (

  <trim suffixoverrides= "," >

   < if test= "title != null and title != '' " >

    title ,

   </ if >

  </trim>

  ) values (

  <trim suffixoverrides= "," >

   < if test= "title != null and title != '' " >

    #{title} ,

   </ if >

  </trim>

  )

</insert>

4.需要验证的更新

?

1

2

3

4

5

6

7

8

9

10

<update id= "update" parametertype= "xxx" >

  update xxx

  <set>

   < if test= "title != null and title != '' " >

    title = #{title} ,

   </ if >

  </set>

  where

  id = #{id}

</update>

5.<!--批量更新ticketid和seatno-->

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<update id= "xxxupdate" parametertype= "java.util.list" >

  update xxx

  <trim prefix= "set" suffixoverrides= "," >

   <trim prefix= "aa =case" suffix= "end," >

    <foreach collection= "orders" item= "item" index= "index" >

     < if test= "item.aa !=null" >

      when id=#{item.id} then #{item.aa}

     </ if >

    </foreach>

   </trim>

   <trim prefix= "bb =case" suffix= "end," >

    <foreach collection= "orders" item= "item" index= "index" >

     < if test= "item.bb !=null" >

      when id=#{item.id} then #{item.bb}

     </ if >

    </foreach>

   </trim>

  </trim>

  where id in

  <foreach collection= "orders" index= "index" item= "item" separator= "," open= "(" close= ")" >

   #{item.id,jdbctype=bigint}

  </foreach>

</update>

mybatis可以使用string给数据库int类型赋值

springboot中开启日志

?

1

#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.stdoutimpl

1.order by ${columnname}

这里 mybatis 不会修改或转义字符串。note 用这种方式接受用户的输入,并将其用于语句中的参数是不安全的,会导致潜在的 sql 注入攻击,因此要么不允许用户输入这些字段,要么自行转义并检验。

2.如何使用连接池。

首先实例化连接池数据源对象,让他实现datasourcefactory这个接口。然后实现方法。在mybatis。conf文件中设置数据连接池这个类,将数据库连接信息放在config.properties文件中。

3.mybatis.config文件中setting和数据源的设置参数区别

会被覆盖。

4.连接参数查询顺序

首先查询properties文件,然后查询resource文件,最后查询方法参数。重复的话会被覆盖。

5.druid连接池配置方式:

详见官网

druiddatasourcefactory首先实行setproperties方法,然后返回设置数据源方法。drui数据源也需要在datasource中设置properties文件

6.实体类的方法不定义也可以进行映射

7.mybatis默认是事务不提交

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://segmentfault.com/a/1190000016333566

查看更多关于mybatis注解与xml常用语句汇总的详细内容...

  阅读:47次