Mybatis笔记

1.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"
>
<!--mybatis的主要配置文件-->
<configuration>

<properties resource=""></properties>
//设置运行中的一些行为 如日志实现
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
//设置别名
<typeAliases>
<package name="cn.jbit.entrty"/>

</typeAliases>
<!--配置环境--> // 多套运行环境 将sql运行到多个不同的数据库
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<!--配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源-->
<dataSource type="POOLED">
<!--连接数据库的四个基本信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/easybuy"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
<!-- <mappers>
<mapper resource="cn/jbit/dao/EasybuyUserDaoMapper.xml" />
</mappers>-->

<mappers>
<mapper class="cn.jbit.dao.EasybuyUserDaoMapper" />
<mapper class="cn.jbit.dao.EasyBuyProductDaoMapper"/>

</mappers>

</configuration>

 

1.2 sql映射文件

<?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">
//  映射文件的根元素节点   namespace 用于区分不同的mapper 全局唯一
<mapper namespace="cn.jbit.dao.EasybuyUserDao">
    <!--配置查询所有-->
    <!--此id为dao层接口类的方法名字 此resultType用于方法返回的类型-->
    <select id="list"  resultType="cn.jbit.entrty.EasybuyUser">
        select  * from easybuy_user;
    </select>
</mapper>

 

Map传递参数,直接在sql中取出key即可

 

 

 

复杂查询resultMap

<!--按查询嵌套处理-->
<select id="sel1" resultMap="ss" parameterType="map">
     SELECT * FROM smbms_bill
     where productName like "%"#{name}"%" and isPayment=#{is}
​
</select><resultMap id="ss" type="SmbmsBill">
    <!--  association关联属性  property属性名 javaType属性类型 column在多的一方的表中的列名
​
      这里传递过来的id,只有一个属性的时候,下面可以写任何值
        association中column多参数配置:
            column="{key=value,key=value}"
            其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
​
            column 的参数就是传递给下一个sql的条件 SELECT * FROM smbms_provider where id=#{id}
            -->
    <association property="providerName" column="providerId" javaType="SmbmsProvider" select="stu"/>
</resultMap>
    <select id="stu" resultType="SmbmsProvider">
     SELECT * FROM smbms_provider where id=#{id}
</select>
​
​
<!--按查询结果嵌套处理-->
<resultMap id="smbmsmap" type="SmbmsBill">
    <result property="id" column="id"/>
    <result property="productName" column="productName"/>
    <result property="isPayment" column="isPayment"/>
<association property="providerName" javaType="SmbmsProvider">
    <result property="proName" column="proName"/>
</association>
</resultMap>
    <select id="sel"  resultMap="smbmsmap" parameterType="map">
        SELECT s.id,s.productName,s.isPayment,p.proName FROM smbms_bill s, smbms_provider p
        WHERE s.providerId=p.id AND s.productName LIKE "%"#{name}"%" AND s.isPayment=#{is}
   </select>

 

动态sql 标签

 

if

<select id="sel" parameterType="map" resultType="smbmsProvider">
       select  * from smbms_provider where 1=1
       <if test="s != null">
        and proName = #{s}
        </if>
   </select>

 

 

where

这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。【这是我们使用的最多的案例】

<select id="queryBlogIf" parameterType="map" resultType="blog">
    select * from blog 
    <where>
        <if test="title != null">
            title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </where>
</select>

 

 

set

<!--注意set是用的逗号隔开-->
<update id="updateBlog" parameterType="map">
    update blog
      <set>
          <if test="title != null">
              title = #{title},
          </if>
          <if test="author != null">
              author = #{author}
          </if>
      </set>
    where id = #{id};
</update>
 

 

foreach

<select id="queryBlogForeach" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <!--
        collection:指定输入对象中的集合属性
        item:每次遍历生成的对象
        open:开始遍历时的拼接字符串
        close:结束时拼接的字符串
        separator:遍历对象之间需要拼接的字符串
        select * from blog where 1=1 and (id=1 or id=2 or id=3)
      -->
        <foreach collection="ids"  item="id" open="and (" close=")" separator="or">
            id=#{id}
        </foreach>
    </where>
</select>
 

 

choose,when ,otherwise 相当于 switch

 

<select id="queryBlogChoose" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <choose>
            <when test="title != null">
                 title = #{title}
            </when>
            <when test="author != null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

 

 

 

sql片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

<sql id="if-title-author">
    <if test="title != null">
        title = #{title}
    </if>
    <if test="author != null">
        and author = #{author}
    </if>
</sql>
引用SQL片段:
​
<select id="queryBlogIf" parameterType="map" resultType="blog">
    select * from blog
    <where>
        <!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace -->
        <include refid="if-title-author"></include>
        <!-- 在这里还可以引用其他的 sql 片段 -->
    </where>
</select>

 

 

 

posted @ 2020-03-07 20:45  陈鸿锋  阅读(121)  评论(1)    收藏  举报