mybatis多表联合查询

以前东拼西凑来的,不知道都是哪些链接!


User 、Role 的对应关系是,一个用户有多个角色,因此,在 User 的实体中加入一个 Role 的属性 private List<Role> roles;对应一对多的关系。

  1.   <select id="queryForList" resultMap="queryForListMap">  
  2.         SELECT  
  3.           u.id,  
  4.           u.username,  
  5.           u.password,  
  6.           r.id r_id,  
  7.           r.name r_name,  
  8.           r.jsms r_jsms,  
  9.           r.bz r_bz,  
  10.           r.jlzt r_jlzt,  
  11.           r.glbm r_glbm  
  12.         FROM  
  13.           user u  
  14.         LEFT JOIN  
  15.           role r  
  16.         ON  
  17.           u.id = r.userid  
  18.     </select>  

  1. <resultMap id="queryForListMap" type="com.sica.domain.User">  
  2.         <id column="id" property="id" jdbcType="VARCHAR"/>  
  3.         <result column="username" property="username" jdbcType="VARCHAR"/>  
  4.         <result column="password" property="password" jdbcType="VARCHAR"/>  
  5.         <collection property="roles" javaType="java.util.List" ofType="com.sica.domain.Role">  
  6.             <id column="r_id" property="id" jdbcType="VARCHAR" />  
  7.             <result column="r_name" property="name" jdbcType="VARCHAR" />  
  8.             <result column="r_jsms" property="jsms" jdbcType="VARCHAR" />  
  9.             <result column="r_bz" property="bz" jdbcType="VARCHAR" />  
  10.             <result column="r_jlzt" property="jlzt" jdbcType="INTEGER" />  
  11.             <result column="r_glbm" property="glbm" jdbcType="VARCHAR" />  
  12.         </collection>  
  13.     </resultMap>  

1.MyBatis实现一对一有几种方式?具体怎么操作的
        有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次,
        通过在resultMap里面配置association节点配置一对一的类就可以完成;


        嵌套查询是先查一个表,根据这个表里面
        的结果的外键id,去再另外一个表里面查询数据,也是通过association配置,但另外一个表
        的查询通过select属性配置

  1. <resultMap id=”blogResult” type=”Blog”>  
  2. <association property="author" column="blog_author_id" javaType="Author"  
  3. select=”selectAuthor”/>  
  4. </resultMap>  
  5.    
  6. <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>  
  7. SELECT * FROM BLOG WHERE ID = #{id}  
  8. </select>  
  9.    
  10. <select id=”selectAuthor” parameterType=”int” resultType="Author">  
  11. SELECT * FROM AUTHOR WHERE ID = #{id}  
  12. </select> 
2.MyBatis实现一对多有几种方式,怎么操作的
        有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次,通过在resultMap里面配
        置collection节点配置一对多的类就可以完成;

  1. <resultMap id="detailedBlogResultMap" type="Blog">  
  2. <constructor>  
  3. <idArg column="blog_id" javaType="int"/>  
  4. </constructor>  
  5. <result property="title" column="blog_title"/>  
  6. <association property="author" column="blog_author_id" javaType=" Author">  
  7. <id property="id" column="author_id"/>  
  8. <result property="username" column="author_username"/>  
  9. <result property="password" column="author_password"/>  
  10. <result property="email" column="author_email"/>  
  11. <result property="bio" column="author_bio"/>  
  12. <result property="favouriteSection" column="author_favourite_section"/>  
  13. </association>  
  14. <collection property="posts" ofType="Post">  
  15. <id property="id" column="post_id"/>  
  16. <result property="subject" column="post_subject"/>  
  17. <association property="author" column="post_author_id" javaType="Author"/>  
  18. <collection property="comments" column="post_id" ofType=" Comment">  
  19. <id property="id" column="comment_id"/>  
  20. </collection>  
嵌套查询是先查一个表,根据这个表里面的
结果的外键id,去再另外一个表里面查询数据,也是通过配置collection,但另外一个表的
查询通过select节点配置

3.使用resultType、ResultMap处理返回结果

resultType:指定返回值结果的完全限定名,处理多表查询的结果。多表查询需要定义vo封装查询的结果。只能用在单表查询或者定义vo的情况,如果是定义vo不能将关联的数据封装为需要获得某个类的对象


使用resultMap:不需要定义vo类,将关联数据对应的类,作为另外一个类的属性。

      第一步:定义resultMap

      第二步:引用resultMap

      使用resultMap:用来多表关联的复杂查询中,通过需要将关联查询的数据封装为某个实体类对象,如果有特殊业务需要或者说明需要将管理数据封装为实体类的对象,使用resultMap

4.Xml映射文件中,除了常见的select|insert|updae|delete标签之外,还有哪些标签?

还有很多其他的标签,<resultMap>、<parameterMap>、<sql>、<include>、<selectKey>,加上动态sql的9个标签,trim|where|set|foreach|if|choose|when|otherwise|bind等,其中<sql>为sql片段标签,通过<include>标签引入sql片段,<selectKey>为不支持自增的主键生成策略标签。


choose (when, otherwise)标签
if标签是与(and)的关系,而 choose 是或(or)的关系。
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
    </select>

parameterType使用了Java.lang.String类型 
bind标签的value值得写法:value="'%'+_parameter.getEmail() +'%'" 
SQL语句中使用: email like #{pattern},注意是#,而不是$ 
最后,测试时使用的代码也需要注意。

<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
        parameterType="java.lang.String">
        select id,username,email,status,createtime,updatetime from tb_user
        <bind name="pattern"value="'%'+_parameter.getEmail() +'%'" />
        <where>
            <if test="email != null and email != ''">
                email like #{pattern}
            </if>
        </where>
    </select>

selectKey 标签 生成主键

 在insert语句中,在Oracle经常使用序列、在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键。
  
  
  1.     <selectKey keyProperty="studentId" resultType="String" order="BEFORE">  
  2.         select nextval('student')  
  3.     </selectKey>  
属性 描述 取值
keyProperty selectKey 语句生成结果需要设置的属性。
resultType 生成结果类型,MyBatis 允许使用基本的数据类型,包括String 、int类型。
order

1:BEFORE,会先选择主键,然后设置keyProperty,再执行insert语句;

2:AFTER,就先运行insert 语句再运行selectKey 语句。

BEFORE(orcle)

AFTER(mysql)
if where

sql 片段    include


3、使用foreach进行sql语句拼接 
在向sql传递数组或List,mybatis使用foreach解析,我们可以使用foreach中元素进行sql语句的拼接,请求数据。 
通过一个例子来看: 
需求:SELECT * FROM USER WHERE id=1 OR id=10 OR id=16 
或者:SELECT * FROM USER WHERE id IN(1,10,16)

<if test="ids != null">     
        <foreach collection="ids"item="user_id"open="AND ("close=")" separator="or" >
    每次遍历需要拼接的串
                id= #{user_id}
        </foreach>
 </if>

其中,collection:指定输入对象中集合属性,item: 每个遍历生成对象,open:开始遍历时拼接串,close: 结束遍历是拼接的串,separator: 遍历的两个对象中需要拼接的串


使用set+if标签修改后,如果某项为null则不进行更新(如studentName为null则studentName不更新,sex,birthday更新),而是保持数据库原值。如下示例:

Xml代码  

[html]  view plain  copy
  1. <!-- 更新学生信息 -->     
  2. <update id="updateStudent" parameterType="StudentEntity">     
  3.     UPDATE STUDENT_TBL      
  4.     <set>    
  5.         <if test="studentName!=null and studentName!='' ">     
  6.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  7.         </if>     
  8.         <if test="studentSex!=null and studentSex!='' ">     
  9.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  10.         </if>     
  11.         <if test="studentBirthday!=null ">     
  12.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  13.         </if>     
  14.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  15.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  16.         </if>     
  17.     </set>     
  18.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  19. </update>  

3.2.3 trim
 trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

 where例子的等效trim语句:

Xml代码  

[html]  view plain  copy
  1. <!-- 查询学生list,like姓名,=性别 -->     
  2. <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">     
  3.     SELECT * from STUDENT_TBL ST      
  4.     <trim prefix="WHERE" prefixOverrides="AND|OR">     
  5.         <if test="studentName!=null and studentName!='' ">     
  6.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
  7.         </if>     
  8.         <if test="studentSex!= null and studentSex!= '' ">     
  9.             AND ST.STUDENT_SEX = #{studentSex}      
  10.         </if>     
  11.     </trim>     
  12. </select>  

bind标签

parameterType使用了Java.lang.String类型 
bind标签的value值得写法:value="'%'+_parameter.getEmail() +'%'" 
SQL语句中使用: email like #{pattern},注意是#,而不是$ 
最后,测试时使用的代码也需要注意。

<select id="findUserByFuzzyEmail" resultMap="BaseResultMap"
        parameterType="java.lang.String">
        select id,username,email,status,createtime,updatetime from tb_user
        <bind name="pattern"value="'%'+_parameter.getEmail() +'%'" />
        <where>
            <if test="email != null and email != ''">
                email like #{pattern}
            </if>
        </where>
    </select>

5、最佳实践中,通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗

答:Dao接口,就是Mapper接口,接口的全限名,就是映射文件中的namespace的值,接口的方法名,就是映射文件中MappedStatement的id值,接口方法内的参数,就是传递给sql的参数。Mapper接口是没有实现类的,当调用接口方法时,接口全限名+方法名拼接字符串作为key值,可唯一定位一个MappedStatement,举例:com.mybatis3.mappers.StudentDao.findStudentById,可以唯一找到namespace为com.mybatis3.mappers.StudentDao下面id = findStudentById的MappedStatement。在Mybatis中,每一个<select>、<insert>、<update>、<delete>标签,都会被解析为一个MappedStatement对象。

Dao接口里的方法,是不能重载的,因为是全限名+方法名的保存和寻找策略。

Dao接口的工作原理是JDK动态代理,Mybatis运行时会使用JDK动态代理为Dao接口生成代理proxy对象,代理对象proxy会拦截接口方法,转而执行MappedStatement所代表的sql,然后将sql执行结果返回。

6.使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值。

方法:在mapper中指定keyProperty属性,示例如下:


  1. <insert id="insertAndGetId" useGeneratedKeys="true" keyProperty="userId" parameterType="com.chenzhou.mybatis.User">  
  2.     insert into user(userName,password,comment)  
  3.     values(#{userName},#{password},#{comment})  
  4. </insert> 

7. Mybatis是如何将sql执行结果封装为目标对象并返回的?都有哪些映射形式?

第一种是使用<resultMap>标签,逐一定义列名和对象属性名之间的映射关系。

第二种是使用sql列的别名功能,将列别名书写为对象属性名,比如T_NAME AS NAME,对象属性名一般是name,小写,但是列名不区分大小写,Mybatis会忽略列名大小写,智能找到与之对应对象属性名

有了列名与属性名的映射关系后,Mybatis通过反射创建对象,同时使用反射给对象的属性逐一赋值并返回,那些找不到映射关系的属性,是无法完成赋值的。

8.Mybatis的Xml映射文件中,不同的Xml映射文件,id是否可以重复?

答:不同的Xml映射文件,如果配置了namespace,那么id可以重复;如果没有配置namespace,那么id不能重复;毕竟namespace不是必须的,只是最佳实践而已。

原因就是namespace+id是作为Map<String, MappedStatement>的key使用的,如果没有namespace,就剩下id,那么,id重复会导致数据互相覆盖。有了namespace,自然id就可以重复,namespace不同,namespace+id自然也就不同。

9.当实体类中的属性名和表中的字段名不一样 
解决办法①: 
通过在查询的sql语句中定义字段名的别名,让字段名的别名和实体类的属性名一致 
<select id=”selectorder” parametertype=”int” resultetype=”me.gacl.domain.order”> 
select order_id id, order_no orderno ,order_price price form orders where order_id=#{id}; 
</select> 
解决办法②: 
通过<resultMap>来映射字段名和实体类属性名的一一对应的关系 
<resultMap type=”me.gacl.domain.order” id=”orderresultmap”> 
<!–用id属性来映射主键字段–> 
<id property=”id” column=”order_id”> 
<!–用result属性来映射非主键字段–> 
<result property = “orderno” column =”order_no”/> 
<result property=”price” column=”order_price” /> 
</reslutMap>

10.IBatis和MyBatis在核心处理类分别叫什么?

IBatis里面的核心处理类交SqlMapClient, MyBatis里面的核心处理类叫做SqlSession 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值