markdown 在线制作ppt json校验和格式化工具

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 查询

当查询的参数有多个时,例如

findByIds(String name, Long[] ids) 

在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称

     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缓存

和spring结合由于sqlsession关闭一级缓存就清除,所以需要使用事务或二级缓存来解决缓存问题。 具体文章:http://blog.csdn.net/u011403655/article/details/46696065

springboot工程扫描不到的问题

开始以为是打包的问题,后来发现怎样修改打包插件的配置都不好使,排除此问题。

后来调整mybatisplus的mapper-locations: classpath:/mapper/Mapper.xml路径,无论怎样调整都没有反应,还是不好使

终于想到,springboot配置文件的优先级是先加载application.properties和注解,application.yamld厄优先级不高。

因为再application-mybatis.xml中配置了sqlSessionFactory,里面有mapper-locations:的property,没有配置,也就是空,这个空把已配置的给覆盖了。

最终解决办法就是修改appliationmybatis.xml中的:

<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
posted @ 2018-04-03 09:47  GavinHacker  阅读(146)  评论(0编辑  收藏  举报
markdown 在线制作ppt json校验和格式化工具