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 符号
<
<
小于号
>
>
大于号
&
&
和
'
’
单引号
"
"
双引号
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
Mybatis 使用备忘录的更多相关文章
- mybatis+mysql批量插入和批量更新、存在及更新
mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- Java MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- [原创]mybatis中整合ehcache缓存框架的使用
mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- mybatis plugins实现项目【全局】读写分离
在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...
- MyBatis基础入门--知识点总结
对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MementoPattern(备忘录模式)
/** * 备忘录模式 * @author TMAC-J * 用于存储bean的状态 */ public class MementoPattern { public class Memento{ pr ...
随机推荐
- 图解Golang的GC算法
虽然Golang的GC自打一开始,就被人所诟病,但是经过这么多年的发展,Golang的GC已经改善了非常多,变得非常优秀了. 以下是Golang GC算法的里程碑: v1.1 STW v1.3 Mar ...
- instanceof简单用法
语法: 对象 instanceof 类: 含义:如果这个对象时这个类或者这个类的子类的实例化,那么结果及时ture, 否则 false. 常常用来判断一个类是否是某个类的子类,以此判断A类是否继承或者 ...
- java 多线程中的wait方法的详解
java多线程中的实现方式存在两种: 方式一:使用继承方式 例如: PersonTest extends Thread{ String name; public PersonTest(String n ...
- 桌面小部件Wight父类AppWidgetProvider的三个方法
onUpdate()这个方法会在每次更新App Widget的时候调用,数据更新的逻辑都写在这个方法里边.而且要注意的是:在用户添加小部件的时候,会首先调用这个方法,应该在这个方法里进行初始化操作,比 ...
- centos6.6安装Elasticsearch
1. 安装jar8 yum list java-1.8* sudo yum install java-1.8.0-openjdk* -y java --version 2. 安装elasticsear ...
- CommonsChunkPlugin VS SplitChunksPlugin
等了好久终于等到你, webpack团队人员卧薪尝胆五个多月的时间终于带来的webpack4.0,个人觉得webpack4带来的最大优化便是对于懒加载块拆分的优化,删除了CommonsChunkPlu ...
- JSP·随笔
1.简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Servlet - Servlet擅长调用Java程序和后台进 ...
- 论Activity的转换
论Activity的互相转换 这次任务是实现 1.在主屏幕输入自己的姓名,单击评估按钮 2.进入第二个界面,并将主屏幕输入的姓名传递给第二个界面 3.在第二个界面进行问题回答: 4.第二个界面的回答结 ...
- 解决.Net Core跨域问题
什么是跨域?浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 跨域的几种情况 1.端口和协议的不同,只能通过后台来解决 2.localhost和127.0.0.1虽然 ...
- shell遍历文件夹
遍历目录下的所有文件 假如有一个文件夹路径为dir,遍历文件 for file in /path/dir/* do if test -f $file then echo $file arrary=($ ...