MyBatis映射文件详解

谁借莪1个温暖的怀抱¢ 2022-05-21 03:43 328阅读 0赞

0x00:文件介绍

在MyBatis中,Mapper映射文件就是sql语句的配置文件,其会在运行时加载sql语句并映射相应参数。在映射文件中,根据不同的sql语句性质,要使用不同的标签来包裹,其中涉及到的标签如下表:

0x01:增删改查示例

insert配置示例:

  1. <insert id="insertUser" parameterType="cn.com.mybatis.pojo.User">
  2. insert into user(username,password,gender,birthday,email,province,city)
  3. value(#{username},#{password},#{gender},#{birthday,jdbcType=DATE},#{email},#{province},#{city})
  4. </insert>

update配置示例:

  1. <update id="updateUserName" parameterType="cn.com.mybatis.pojo.User">
  2. update user set username=#{username} where id=#{id}
  3. </update>

delete配置示例:

  1. <delete id="deleteUser" parameterType="java.lang.Integer">
  2. delete from user where id=#{id}
  3. </delete>

select配置示例:

  1. <select id="findUserById" parameterType="int" resultType="cn.com.mybatis.pojo.User">
  2. select * from user where id=#{id}
  3. </select>

其中,paramterType为输入参数类型,resultType为输出参数类型,#{}为占位符,类似于sql语句中的?,起到预编译的作用。

0x02:属性拓展

在insert、update、delete、select配置标签中,可以配置很多属性,具体如下:

  1. <select
  2. id="selectPerson"
  3. parameterType="int"
  4. resultType="hashmap"
  5. resultMap="personResultMap"
  6. flushCache="false"
  7. useCache="true"
  8. timeout="10000"
  9. fetchSize="256"
  10. statementType="PREPARED"
  11. resultSetType="FORWARD_ONLY">
  12. <insert
  13. id="insertAuthor"
  14. parameterType="domain.blog.Author"
  15. flushCache="true"
  16. statementType="PREPARED"
  17. keyProperty=""
  18. keyColumn=""
  19. useGeneratedKeys=""
  20. timeout="20">
  21. <update
  22. id="updateAuthor"
  23. parameterType="domain.blog.Author"
  24. flushCache="true"
  25. statementType="PREPARED"
  26. timeout="20">
  27. <delete
  28. id="deleteAuthor"
  29. parameterType="domain.blog.Author"
  30. flushCache="true"
  31. statementType="PREPARED"
  32. timeout="20">

这些属性的含义如下表:

0x03:总结

MyBatis程序通过sql与数据库打交道,其sql语句是写在映射配置文件中的。通过insert、update、delete、select标签来配置增删改查语句,其属性含义参见上表。


公众号推荐:aFa攻防实验室

  1. **分享关于信息搜集、Web安全、内网安全、代码审计、红蓝对抗、JavaPython等方面的东西。**
  2. ![20191220230427373.jpg][]

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis映射详解

    Mybatis映射详解 在最近的工作中,碰到一个比较复杂的返回结果,发现简单映射已经解决不了这个问题了,只好去求助百度,学习mybatis复杂映射应该怎么写,将学习笔记结

    相关 Mybatis-映射文件

    Mybatis-映射文件(二) Mybatis的真正强大在于它的映射语句,也是它的魔力所在。由于它的异常强大,映射器XML文件就显得相对简单。如果拿他跟具有相同功能的JD