MyBatis联合查询配置详解

来源:互联网 发布:找客源软件 编辑:程序博客网 时间:2024/05/17 11:35

查询

<select id="findById" parameterType="int" resultType="book.entity.User">       select * from user where id = #{id}     <!--parameterType是参数类型-->        <!-- parameterType可以不写,通过在方法中添加@param来指定参数--></select>

插入

 <insert id="insertUser" parameterType="User" keyProperty="Id" useGeneratedKeys="true" statementType="PREPARED">     insert into user (userName,password) values (#{userName},#{password})    </insert> 下面两个属性都是insert中独有的,设置数据库中主键自增长: keyProperty ,默认值unset,设置为你实体类中的一个属性的名称  useGeneratedKeys ,取值范围true|false(默认值),设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。

更新

<update id="updateUser" parameterType="User">     update user set password=#{password} where id=#{id}    </update>

keyProperty ,默认值unset,设置为你实体类中的一个属性的名称

useGeneratedKeys ,取值范围true|false(默认值),设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。


联合查询配置介绍


联合查询和子查询都是用的association,主要针对的是数据库表中一对一的关系,比如一个作者表,作者也是一个用户,对应着一个user。

用到两个实体类对象,其中一个要包含另一个的集合
user
这里写图片描述
author
这里写图片描述
下面是User.xml的配置信息
例子一

<resultMap type="Author" id="AuthorMap">    <id property="id" column="authorid"/>    <result property="realName" column="realName"/>    <result property="IDCard" column="IDCard"/>     <!-- 每一个属性,对应着数据库中的表名 -->    <association property="user" column="userID" javaType="User">        <!-- property=user表示author.java中的一个字段 column=userID表示author表列,而实体类Author中对应着一个private User user 他两个是相互关联的 属性 javaType使用的识别名,表示类型是User类型的-->        <!-- 表示Author.java中的User属性对应的表通过author中的userID进行关联 -->        <!--可以在association中指定一条select=“selectId” 这样会根据column中的值作为查询条件,查询在此设置的select语句-->        <id property="id" column="user.id"/>        <result property="username" column="username"/>        <result property="password" column="password"/>        <!-- 以上表示配置user表中的各种属性 -->    </association> </resultMap> <select id="selectAuthorJoin" resultMap="AuthorMap">    select * from author inner join user on user.id=author.userID </select>

联合查询每次都将所有联合的表的数据全部取出,
其中当我们联合的表不需要取出所有的数据,可以在其中加入构造器,在构造器中加入我们需要的字段即可

    <association property="user" column="userID" javaType="User">        <constructor>            <arg column="username" javaType="String"/>            <arg column="password" javaType="String"/>        </constructor>    </association>

子查询

  <resultMap type="Author" id="SubAuthorMap">    <id property="id" column="authorid"/>    <result property="realName" column="realName"/>    <result property="IDCard" column="IDCard"/>     <association property="user" column="userID" javaType="User" select="findById">    <!-- 子查询中,select属性会将column中的userID传递给findById,就如同父子 -->    </association>  </resultMap>  <select id="findById" parameterType="int" resultType="User">       select * from user where id = #{id}  </select>

子查询每次会查询n+1次,会慢,但是配合持久化框架的延迟加载技术,在需要的时候查询,特殊情况下不必要查询出所有的数据。

懒加载配置

集合查询

集合查询就属于一对多的级联

要使用集合查询,两个相联表中的查询表中,要有集合对象
user表

private int id;    private String username;    private String password;    private List<Visit> visitList;

visit表

public class Visit {    private int visitID;    private int userID;    private Date visitDate;    private String visitIP;    //get set......    }
<resultMap type="User" id="visitMap">    <!--设置的属性都存在与 type="User" User实例类中的对象-->        <id property="id" column="id"/>        <result property="username" column="userName"/>        <result property="password" column="password"/>        <collection property="visitList" javaType="Arraylist" column="visitID" ofType="Visit">    <!--property表示User实体类中建立的List<Visit> visitList对象 -->        <!-- ofType表示ArrayList的属性-->        <result property="visitID" column="visitID"/>        <result property="visitIP" column="visitIP"/>        <result property="visitDate" column="visitDate"/>        </collection>    </resultMap>    <select id="selectVisit" resultMap="visitMap">        select * from user inner join visit on (user.id=visit.userID)    <!--联合查询,会查询联合的表的所有数据包括user和join表-->    </select>    如果想查询id为1的用户的所有浏览记录<select id="selectVisit" resultMap="visitMap">        select * from user inner join visit on (user.id=visit.userID) where user.id=1    </select>

集合查询相比于联合查询多了ofType属性,用来标记集合对象的类型
查询结果如下这里写图片描述

鉴别器 :在resultMap中的属性之一
目的:通过某个字段的值,选择为部分字段赋值

<discriminator javaType="已知字段的类型,一般为boolean或者int" column="已知字段">        <case value="判断已知字段的值1">            <result property="实体类属性1" column="列1"/>        </case>        <case value="判断已知字段的值2">            <result property="实体类属性2" column="列1"/>        </case>    </discriminator>

初学者,代码都亲测有效,若有错误请联系指正

原创粉丝点击