mybatis映射XML文件详解及实例

mybatis映射XML文件

一个简单的映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cnx.wxcar.mapper.CustomerMapper">
</mapper>

当然这个文件中没有任何的元素

The Mapper XML files have only a few first class elements :

  1. cache – Configuration of the cache for a given namespace.
  2. cache-ref – Reference to a cache configuration from another namespace.
  3. resultMap – The most complicated and powerful element that describes how to load your objects from the database result sets.
  4. sql – A reusable chunk of SQL that can be referenced by other statements.
  5. insert – A mapped INSERT statement.
  6. update – A mapped UPDATE statement.
  7. delete – A mapped DELETE statement.
  8. select – A mapped SELECT statement.

select

简单的例子:

<select id="selectPerson" parameterType="int" resultType="hashmap">
 SELECT * FROM PERSON WHERE ID = #{id}
</select>

select也有很多属性可以让你配置:

<select
 id="selectPerson"
 parameterType="int"
 parameterMap="deprecated"
 resultType="hashmap"
 resultMap="personResultMap"
 flushCache="false"
 useCache="true"
 timeout="10000"
 fetchSize="256"
 statementType="PREPARED"
 resultSetType="FORWARD_ONLY">

insert, update and delete

<insert
 id="insertAuthor"
 parameterType="domain.blog.Author"
 flushCache="true"
 statementType="PREPARED"
 keyProperty=""
 keyColumn=""
 useGeneratedKeys=""
 timeout="20">

<update
 id="updateAuthor"
 parameterType="domain.blog.Author"
 flushCache="true"
 statementType="PREPARED"
 timeout="20">

<delete
 id="deleteAuthor"
 parameterType="domain.blog.Author"
 flushCache="true"
 statementType="PREPARED"
 timeout="20">

语句:

<insert id="insertAuthor">
 insert into Author (id,username,password,email,bio)
 values (#{id},#{username},#{password},#{email},#{bio})
</insert>

<update id="updateAuthor">
 update Author set
  username = #{username},
  password = #{password},
  email = #{email},
  bio = #{bio}
 where id = #{id}
</update>

<delete id="deleteAuthor">
 delete from Author where id = #{id}
</delete>

f your database supports auto-generated key fields (e.g. MySQL and SQL Server),上面的插入语句可以写成:

<insert id="insertAuthor" useGeneratedKeys="true"
  keyProperty="id">
 insert into Author (username,password,email,bio)
 values (#{username},#{password},#{email},#{bio})
</insert>

如果你的数据库还支持多条记录插入,可以使用下面这个语句:

<insert id="insertAuthor" useGeneratedKeys="true"
  keyProperty="id">
 insert into Author (username, password, email, bio) values
 <foreach item="item" collection="list" separator=",">
  (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
 </foreach>
</insert>

sql

这个element可以定义一些sql代码的碎片,然后在多个语句中使用,降低耦合。比如:

<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>

然后在下面的语句中使用:

<select id="selectUsers" resultType="map">
 select
  <include refid="userColumns"><property name="alias" value="t1"/></include>,
  <include refid="userColumns"><property name="alias" value="t2"/></include>
 from some_table t1
  cross join some_table t2
</select>

Result Maps

官网给了个最最复杂的例子

大体意思呢就是一个博客系统有一个作者,很多博文,博文中有一个作者,很多评论,很多标签(包括了一对多,一对一)

<!-- Very Complex Statement -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
 select
    B.id as blog_id,
    B.title as blog_title,
    B.author_id as blog_author_id,
    A.id as author_id,
    A.username as author_username,
    A.password as author_password,
    A.email as author_email,
    A.bio as author_bio,
    A.favourite_section as author_favourite_section,
    P.id as post_id,
    P.blog_id as post_blog_id,
    P.author_id as post_author_id,
    P.created_on as post_created_on,
    P.section as post_section,
    P.subject as post_subject,
    P.draft as draft,
    P.body as post_body,
    C.id as comment_id,
    C.post_id as comment_post_id,
    C.name as comment_name,
    C.comment as comment_text,
    T.id as tag_id,
    T.name as tag_name
 from Blog B
    left outer join Author A on B.author_id = A.id
    left outer join Post P on B.id = P.blog_id
    left outer join Comment C on P.id = C.post_id
    left outer join Post_Tag PT on PT.post_id = P.id
    left outer join Tag T on PT.tag_id = T.id
 where B.id = #{id}
</select>

<!-- Very Complex Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
 <constructor>
  <idArg column="blog_id" javaType="int"/>
 </constructor>
 <result property="title" column="blog_title"/>
 <association property="author" javaType="Author">
  <id property="id" column="author_id"/>
  <result property="username" column="author_username"/>
  <result property="password" column="author_password"/>
  <result property="email" column="author_email"/>
  <result property="bio" column="author_bio"/>
  <result property="favouriteSection" column="author_favourite_section"/>
 </association>
 <collection property="posts" ofType="Post">
  <id property="id" column="post_id"/>
  <result property="subject" column="post_subject"/>
  <association property="author" javaType="Author"/>
  <collection property="comments" ofType="Comment">
   <id property="id" column="comment_id"/>
  </collection>
  <collection property="tags" ofType="Tag" >
   <id property="id" column="tag_id"/>
  </collection>
  <discriminator javaType="int" column="draft">
   <case value="1" resultType="DraftPost"/>
  </discriminator>
 </collection>
</resultMap>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(0)

相关推荐

  • MyBatis快速入门之环境搭建和单表映射

    一.MyBatis简介 一说起对象关系映射框架,大家第一时间想到的肯定是Hibernate.Hibernate作为一个著名的框架,功能十分强大.我们只需要配置好实体类和数据表之间的关系,Hibernate就会自动帮我们完成生成并执行SQL语句,映射结果集这样的工作.但是也正是由于Hibernate如此强大的功能,导致了它的缺点:一是非常笨重,启动Hibernate的SessionFactory非常耗时,开销巨大:二是配置复杂,学习成本较高,系统调优也不容易:三是自定义查询功能较弱,查询结果如果不

  • MyBatis框架简介

    本文是我学习 MyBatis 的学习笔记和心得,也是我的第一篇技术文章,可能理解的比较浅显,也难免存在一些错误.如果您喜欢这篇文章,可以分享,并注明来源:如果您有疑问.意见或建议,欢迎留言批评指正,谢谢. 初见 MyBatis 就被它简介的风格所吸引了,大概是喜欢这种简单易上手的轻量级框架吧,功能强大却并不复杂. MyBatis 是一款在持久层使用的 SQL 映射框架,它可以将 SQL 语句单独写在 XML 配置文件中,或者用带有注释的 Mapper 映射类来完成 SQL 类型到 Java 类型

  • BootStrap+Mybatis框架下实现表单提交数据重复验证

    效果: jsp页面: <form class="form-horizontal lui-tj-bd" id="dbc_code_add_form" method="post"> <div class="row"> <div class="col-xs-12"> <!-- PAGE CONTENT BEGINS --> <div class="t

  • Mybatis中 SQL语句复用

    mapper.xml 中共用 mapper.xml 间共用 项目中也许我们会遇到一段sql语句被多个查询.增加等语句用到的情况,如何去偷懒呢,复用sql无疑是较好的选择 这里只提供简单的示范: 如果只是单表查询,并且希望共用的sql只会出现在同一个mapper.xml文件中,那么我们可以直接在 <mapper namespace="XXXXX"></mapper> 中写下面的业务代码 <sql id="unitSql"> a.us

  • Oracle在Mybatis中SQL语句的配置方法

    数据库中有下划线的字段在实体中应采用驼峰命名法,如P_NAME对应pName,实例如下: 1.XML文件中SQL语句配置(Geteway.xml文件) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-m

  • MyBatis关于二级缓存问题

    MyBatis提供一级缓存和二级缓存,其中一级缓存是sqlSession级别的缓存,不同的sqlSession之间的缓存互不影响.二级缓存是Mapper级别的缓存,多个sqlSession操作同一个Mapper,其二级缓存是可以共享的. MyBatis有多种二级缓存方案可供选择.其中对Memcached的支持较为成熟,现以Memcached为例介绍与spring项目的集成. 使用配置 配置pom.xml,添加依赖. <dependencies> ... <dependency> &

  • mybatis 项目配置文件实例详解

    mybatis项目配置 首先这事一个简单的mybatis项目配置文件: <?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"> <configur

  • 详解Spring Boot整合Mybatis实现 Druid多数据源配置

    一.多数据源的应用场景 目前,业界流行的数据操作框架是 Mybatis,那 Druid 是什么呢? Druid 是 Java 的数据库连接池组件.Druid 能够提供强大的监控和扩展功能.比如可以监控 SQL ,在监控业务可以查询慢查询 SQL 列表等.Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,能够提供基于 Filter-Chain 模式的插件体系. 2. DruidDataSource 高效可管理的数据库连接池 3. SQLParser 当业务数据量达

  • mybatis基本实例详解

    废话不多说了,先给大家分享mybatis基本实例代码,具体代码如下所示: <configuration> <properties resource="db.properties"> <property name="" value=""/> </properties> <!-- 起别名 --> <typeAliases> <package name="com.m

  • mybatis映射XML文件详解及实例

    mybatis映射XML文件 一个简单的映射文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="c

  • Mybatis中 XML配置详解

    Mybatis常用带有禁用缓存的XML配置 <?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> <

  • 基于Maven的pom.xml文件详解

    如下所示: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <!-- 父项

  • Servlet中操作文件详解及实例

    Servlet中操作文件详解及实例 因为Servlet本来就是一个.Java文件,因此servlet中操作文件和普通java文件操作文件是一样的. 读取文件主要代码: FileReader f=new FileReader("f:\\lissdy.txt"); BufferedReader bw=new BufferedReader(f); 读出一行数据 String num=bw.readLine(); 注意一定要关闭文件流 bw.close(); 写文件的方法与之类似,具体代码为:

  • Android 中Manifest.xml文件详解

    Android 中Manifest.xml文件详解 每一个Android项目都包含一个清单(Manifest)文件--AndroidManifest.xml,它存储在项目层次中的最底层.清单可以定义应用程序及其组件的结构和元数据. 它包含了组成应用程序的每一个组件(活动.服务.内容提供器和广播接收器)的节点,并使用Intent过滤器和权限来确定这些组件之间以及这些组件和其他应用程序是如何交互的. 它还提供了各种属性来详细地说明应用程序的元数据(如它的图标或者主题)以及额外的可用来进行安全设置和单

  • 如何自动生成Mybatis的Mapper文件详解

    前言 工作中使用mybatis时我们需要根据数据表字段创建pojo类.mapper文件以及dao类,并且需要配置它们之间的依赖关系,这样的工作很琐碎和重复,mybatis官方也发现了这个问题,因此给我们提供了mybatis generator工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 实际上,最非常流行MyBatis-Plus中内置了代码生成器:采用代码或者 Maven 插件可快速生成 Mapper . Model . Service . Co

  • log4j2.xml文件详解及在日志中加入全局guid

    目录 log4j2.xml文件及在日志中加入全局guid 只有定义了这个,上面的才会真实有效 想在日志中加入全局guid需要修改日志的格式 log4j2.x配置文件中各标签 1.Logger 完成日志信息的处理 2.Appender 设置在哪输出日志信息 3.Layout 设置日志信息的输出格式 4.Filters 5.Status 6.monitorInterval 7.Policies 配置日志相关策略 log4j2.xml文件及在日志中加入全局guid <Configuration sta

  • dom4j读取XML文件详解

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,现在越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j.这是必须使用的jar包. 上面说dom4j这么优秀,那么好用,那么从今天开始就跟大家一起分享dom4j的一些用法. dom4j的主要接口都在org.dom4j这个包里定义: 要想

  • php 使用fopen函数创建、打开文件详解及实例代码

    php中没有单独的文件创建函数,如果我们想创建函数,可以使用fopen(),fopen()函数字面意思是打开文件,但该函数也有创建文件的功能,当使用 fopen() 函数打开一个文件时,如果文件不存在,则会尝试创建该文件,并返回一个资源. php fopen函数介绍 fopen函数打开文件或者 URL 语法: resource fopen( string filename, string mode ) fopen()将 filename 指定的名字资源绑定到一个流上. 参数: 1. filena

  • MybatisPlus代码生成器含XML文件详解

    目录 MybatisPlus代码生成器含XML 所需依赖 代码如下 MybatisPlus代码生成器,自用版本不带xml MybatisPlus代码生成器含XML 所需依赖         <!--Mybatis-Plus-->         <dependency>             <groupId>com.baomidou</groupId>             <artifactId>mybatis-plus-boot-sta

随机推荐