Mybatis 使用備忘錄

自動生成Mapper

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

Mybatis like sql語句

<select id="getUserByNameAndNickName" parameterType="pd"  resultMap="BaseResultMap">
select  
<include refid="Base_Column_List" />
from user where UserName like CONCAT('%',#{keyword},'%') or NickName like CONCAT('%',#{keyword},'%') ORDER BY  regtime DESC limit #{startIndex,jdbcType=INTEGER}, #{itemCountOnPage,jdbcType=INTEGER} 
</select>

mybatis 關聯查詢

http://www.cnblogs.com/xdp-gacl/p/4264440.html

<?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,namespace的值習慣上設置成包名+sql映射文件名,這樣就可以保證namespace的值是惟一的
例如namespace="me.gacl.mapping.classMapper"就是me.gacl.mapping(包名)+classMapper(classMapper.xml文件去除後綴)
 -->
<mapper namespace="me.gacl.mapping.classMapper">

<!-- 
    根據班級id查詢班級信息(帶老師的信息)
    ##1. 聯表查詢
    SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;

    ##2. 執行兩次查詢
    SELECT * FROM class WHERE c_id=1;  //teacher_id=1
    SELECT * FROM teacher WHERE t_id=1;//使用上面獲得的teacher_id
 -->

<!-- 
方式一:嵌套結果:使用嵌套結果映射來處理重複的聯合結果的子集
         封裝聯表查詢的數據(去除重複的數據)
    select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
-->
<select id="getClass" parameterType="int" resultMap="ClassResultMap">
    select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select>
<!-- 使用resultMap映射實體類和字段之間的一一對應關係 -->
<resultMap type="me.gacl.domain.Classes" id="ClassResultMap">
    <id property="id" column="c_id"/>
    <result property="name" column="c_name"/>
    <association property="teacher" javaType="me.gacl.domain.Teacher">
        <id property="id" column="t_id"/>
        <result property="name" column="t_name"/>
    </association>
</resultMap>

<!-- 
方式二:嵌套查詢:經過執行另一個SQL映射語句來返回預期的複雜類型
    SELECT * FROM class WHERE c_id=1;
    SELECT * FROM teacher WHERE t_id=1   //1 是上一個查詢獲得的teacher_id的值
-->
 <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
    select * from class where c_id=#{id}
 </select>
 <!-- 使用resultMap映射實體類和字段之間的一一對應關係 -->
 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2">
    <id property="id" column="c_id"/>
    <result property="name" column="c_name"/>
    <association property="teacher" column="teacher_id" select="getTeacher"/>
 </resultMap>

 <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher">
    SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
 </select>

</mapper>

mysql mybatis 分頁

select * from composition where reviewfinish=#{reviewfinish,jdbcType=INTEGER} ORDER BY createtime DESC limit #{startIndex,jdbcType=INTEGER}, #{itemCountOnPage,jdbcType=INTEGER}

startIndex從0開始

mybatis in 查詢

當查詢的參數有多個時,例如html

findByIds(String name, Long[] ids)

在傳參數時,必定要改用Map方式, 這樣在collection屬性能夠指定名稱java

Map<String, Object> params = new HashMap<String, Object>(2);
     params.put("name", name);
     params.put("ids", ids);
    mapper.findByIdsMap(params);

<select id="findByIdsMap" resultMap="BaseResultMap">  
 select  
 <include refid="Base_Column_List" />  
 from tabs where ID in  
 <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">  
  #{item}  
 </foreach>  
</select>

MyBatis 配置多個數據源

http://zhuchengzzcc.iteye.com/blog/1827633

Mybatis不使用ResultMap的狀況

<mapper namespace="com.rx.rdi.dao.clm_collectionservice" >
	<select id="get_clm_collectionserviceid_by_cachemodelid" resultType="java.util.HashMap"  parameterType="java.lang.String">
		select id from clm_collectionservice where CacheModelId= #{key}	limit 1
	</select>
	<select id="get_clm_collectionserviceparams_by_collectionid" resultType="java.util.HashMap"  parameterType="java.lang.String">
		select name,value from clm_collectionserviceparam where collectionid=#{key}
	</select>
</mapper>

不使用ResultMap,直接使用ResultType,這樣省去編寫map工做。簡單的應用程序推薦這樣使用,即便用了MyBatis框架的穩定性,也不用拘泥於MyBatis的條框。

Mybatis動態傳入表名

使用Statement模式,即不是預編譯模式。
這種模式使用 ${} 
由於#{}是專門用於預編譯模式的,用來替換參數標記符。
${}是替換字符串,容易sql注入。

Mybatis 符號

&lt;                                
                 <
                 小於號                                          
                 &gt;
                 >                                     
                 大於號
                 &amp;
                 &
                 和
                 &apos;
                 ’
                 單引號
                 &quot;
                 "
                 雙引號

mybatis一級緩存和二級緩存

一級緩存爲內存,二級緩存爲ehcache緩存mysql

和spring結合因爲sqlsession關閉一級緩存就清除,因此須要使用事務或二級緩存來解決緩存問題。 具體文章:http://blog.csdn.net/u011403655/article/details/46696065spring

springboot工程掃描不到的問題

開始覺得是打包的問題,後來發現怎樣修改打包插件的配置都很差使,排除此問題。sql

後來調整mybatisplus的mapper-locations: classpath:/mapper/Mapper.xml路徑,不管怎樣調整都沒有反應,仍是很差使緩存

終於想到,springboot配置文件的優先級是先加載application.properties和註解,application.yamld厄優先級不高。springboot

由於再application-mybatis.xml中配置了sqlSessionFactory,裏面有mapper-locations:的property,沒有配置,也就是空,這個空把已配置的給覆蓋了。session

最終解決辦法就是修改appliationmybatis.xml中的:mybatis

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置實體掃描路徑,多個package能夠用分號; 逗號, 分隔, 支持通配符*-->
        <!-- com.a.b.entity;com.a.c.entity;com.d.*.entity-->
        <property name="typeAliasesPackage" value="com.baomidou.mybatisplus.test.h2.entity"/>
        <property name="configuration" ref="mybatisConfig"/>
        <!-- MP 全局配置注入 -->
        <property name="globalConfig" ref="globalConfig"/>
        <property name="plugins">
            <array>
                <!-- 分頁插件配置 -->
                <bean id="paginationInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"/>
                <!-- 樂觀鎖插件 -->
                <bean id="optimisticLockerInterceptor"
                      class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor">
                </bean>
                <!-- 性能攔截器,兼打印sql,不建議生產環境配置-->
                <bean id="performanceInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"/>
            </array>
        </property>
        <property name="mapperLocations">
            <list>
                <value>classpath:/mapper/*Mapper.xml</value>
            </list>
        </property>
    </bean>


加入mapperLocation的配置

轉自: http://www.vmfor.com/p/101336779283.html
相關文章
相關標籤/搜索