mybatis-2

mybatis原生用法

    sqlSession.selectOne(sqlId,參數);
    sqlSession.update(sqlId,參數);
    sqlSession.insert(sqlId,參數);
    sqlSession.delete(sqlId,參數);

mybatis接口用法

1 namespace與接口全限定名一致
2 id和抽象函數保持一致
3 參數類型與返回類型保持一致
4 java類名與xml文件名保持一致

    public interface GoodsMapper {
    Goods selectGoodsById();
}
    <select id="selectGoodsById" resultType="com.study.entity.Goods">
        select * from goods where gid=3
    </select>
    GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
    Goods goods = mapper.selectGoodsById();

mybatis-config.xml配置

properties

db.properties

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql:///taobao?useUnicode=true&characterEncoding=utf-8

jdbc.user=root

jdbc.password=123456

    <!-- 加載配置文件 -->
    <properties resource="db.properties"></properties>
    <!-- 通過${key}來使用 -->
    <property name="driver" value="${jdbc.driverClass}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.user}"/>
    <property name="password" value="${jdbc.password}"/>

settings配置

下面是配置log4j日志

    <settings>
        <!-- 日志的實現類,可以不寫,會自動匹配 -->
        <setting name="logImpl" value="LOG4J"/>
        <!-- 將數據庫字段的下劃線自動轉為駝峰命名 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 自動映射,FULL表示無論是否關聯都進行自動映射 -->
        <setting name="autoMappingBehavior" value="FULL"/>
    </settings>
    # Global logging configuration
    log4j.rootLogger=ERROR, stdout
    # MyBatis logging configuration...
    log4j.logger.com.gxy.mapper=TRACE   //這里寫你自己的包
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

別名

    <!-- 指定包下面的所有實體類都會將類名作為別名 -->
    <typeAliases>
        <package name="com.study.entity"/>
    </typeAliases>
    <!-- 使用的使用直接使用別名Goods,不用寫全限定名 -->
    <select id="selectGoodsById" resultType="Goods">
        select * from goods where gid=3
    </select>

關聯

關聯結果resultMap與resultMap嵌套

    public class Goods {
        private GoodType goodsType;//屬性是pojo 普通的java對象
    }

一對一

    <select id="selectGoodsById" resultMap="goodsResultMap">
        select * from goods g inner join data_dictionary d on g.good_type=d.value_id where d.type_code='good_type' and gid=3
    </select>

    <resultMap id="goodsResultMap" type="Goods">
        <!-- 建議主鍵寫上,可以提高mybatis的效率 -->
        <id property="gid" column="gid"/>
        <!--關聯結果 association,只能關聯一個bean javaType:返回的結果類型 -->
        <association property="goodsType" resultMap="goodTypeResultMap"/>
    </resultMap>
    <!-- 注意:這里要配置好自動映射autoMappingBehavior為FULL,配制后駝峰命名mapUnderscoreToCamelCase為true -->
    <resultMap id="goodTypeResultMap" type="GoodType"></resultMap>
關聯resultMap相互嵌套

一對多

<select id="selectGorderByUid" resultMap="gorderResultMap">
    SELECT * from gorder g inner join detail d on g.oid=d.oid where uid=#{value}
</select>

<resultMap id="gorderResultMap" type="Gorder">
    <id property="oid" column="oid"/>
    <collection property="detailList" ofType="Detail" >
        <id property="did" column="did"/>
    </collection>
</resultMap>

關聯查詢resultMap與select嵌套

一對一

    <resultMap id="goodsResultMap" type="Goods">
            <id property="gid" column="gid"/>
            <association property="goodsType" select="selectGoodType" column="good_type" javaType="GoodsType"/>
        </resultMap>

        <select id="selectGoodsById" resultMap="goodsResultMap">
            select * from goods where gid=3
        </select>

        <select id="selectGoodType" resultType="GoodsType">
            select * from data_dictionary where type_code="good_type" and value_id=#{value}
        </select>
關聯resultMap與select相互嵌套

一對多

    <select id="selectGorderByUid" resultMap="gorderResultMap">
        select * from gorder where uid=#{value}
    </select>

    <resultMap id="gorderResultMap" type="Gorder">
        <id property="oid" column="oid"/>
        <!-- select:調用另外一個查詢語句  column:傳遞給另外一個查詢語句使用的字段-->
        <collection property="detailList" select="com.study.mapper.DetailMapper.selectDetailByOid" column="oid"/>
    </resultMap>

動態sql

    <select id="searchGoods" resultMap="goodsResultMap" parameterType="Goods">
        select * from goods
        <!-- where會自動去掉第一個and -->
        <where>
            <if test="gname!=null">
                gname=#{gname}
            </if>
            <if test="goodsType.valueId!=null">
                and good_type=#{goodsType.valueId}
            </if>
        </where>
    </select>

set與if組合

    update goods
    <set>
        <if test="gname!=null and gname!=''">
            gname=#{gname}
        </if>
        <if test="goodsType.valueId!=null">
            , good_type=#{goodsType.valueId}
        </if>
    </set>
    where gid=#{gid}

trim與if的組合,可以代替where和set

    update goods
    <trim prefix="set" suffixOverrides=",">
        <if test="gname!=null and gname!=''">
            gname=#{gname},
        </if>
        <if test="goodsType.valueId!=null">
            good_type=#{goodsType.valueId},
        </if>
    </trim>

    select * from goods
    <trim prefix="where" prefixOverrides="and | or">
        <if test="gname!=null">
            and gname=#{gname}
        </if>
        <if test="goodsType.valueId!=null">
            and good_type=#{goodsType.valueId}
        </if>
    </trim>

foreach,一般用在in語句

    select * from goods where gid in
    <foreach collection="list" item="goods" open="(" close=")" separator=",">
        #{goods}
    </foreach>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. 簡介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的...
    笨鳥慢飛閱讀 5,678評論 0 4
  • Java數據持久化之mybatis 一. mybatis簡介 1.1 原始的JDBC操作: Java 通過 Jav...
    小Q逛逛閱讀 4,968評論 0 16
  • 1、概述 MyBatis中在查詢進行select映射的時候,返回類型可以用resultType,也可以用resul...
    crocodile_b閱讀 731評論 0 4
  • 我曾待過幾年的地方 如今我在家鄉還過著深秋 那兒已下起了雪 每年春來都到了五月里 我沒去以前的那些年不常下雨 下雨...
    無問色空閱讀 175評論 7 7
  • 我,就是一個獨孤者,實實在在的孤獨者。越是孤獨,我越是敢于去挑戰身邊那些讓我十分厭倦的人和物,盡管那些人被奉為“上...
    修改不了昵稱閱讀 278評論 0 1