1、mybatis持久层框架,apache的顶级项目,开始托管到goolecode,后来托管到github,下载地址https://github.com/mybatis/mybatis-3。源码包中的asm包是操作字节码的,比如CGLIB利用它生成代理类,javassist也是操作字节码的类库。
mybatis参考文档:http://www.mybatis.org/mybatis-3/zh/logging.html#
2、sqlMapConfig配置数据源和事务,建立mapper.xml映射文件。sessionFactory,session通过executor(基本执行器和缓存执行器)执行器操作数据库,底层用mapped statement底层封装对象(输入,输出参数)
3、mybatis加载配置文件及创建sqlSession的过程

4、 使用#{}格式的语法在mybatis中使用Preparement语句来安全的设置值,执行sql类似下面的:
PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,id);
这样做的好处是:更安全,更迅速,通常也是首选做法。 不过有时你只是想直接在 SQL 语句中插入一个不改变的字符串。比如,像 ORDER BY,你可以这样来使用:
ORDER BY ${columnName}
5、前提主键是自增的,mybatis获取自增主键的值,
各个属性参考http://limingnihao.iteye.com/blog/781911/
方法1:
<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
方法2:
<insert id="insert" parameterType="Person">
<selectKey keyProperty="id" resultType="long" order="AFTER" >
select LAST_INSERT_ID()
</selectKey>
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
<insert id="addTargetBatch" useGeneratedKeys="true"
parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into bms_target_new
(name,month,sale_target_num,order_target_num,platform,create_user,create_time,update_user,update_time)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.name},#{item.month},#{item.saleTargetNum},#{item.orderTargetNum},#{item.platform},#{item.createUser},now(),#{item.createUser},now())
</foreach>
</insert>
方法3:非自增的方式
<insert id="insert" parameterType="Person">
<selectKey keyProperty="id" resultType="string" order="BEFORE" >
select uuid()
</selectKey>
insert into person(id,name,pswd) values(#{name},#{pswd})
</insert>
6、无任何整合,原始dao开发


7、mapper接口开发,namespace等于接口地址

8、sql片段及引用和foreach



9、都要更新一下统计表,然而要更新统计表,就要根据主键去统计表里面去查询是否已经有这样的一条记录,如果有那么就更新,如果没有那么就插入一条记录,老做法是写三条sql语句:。
select * from player_count where player_id = 1;//查询统计表中是否有记录
insert into player_count(player_id,count) value(1,1);//没有记录就执行insert 操作
update player_count set count = count+1 where player_id = 1;//有记录就执行update操作
这种写法比较麻烦
用on duplicate key update 的做法如下:
insert into player_count(player_id,count) value(1,1) on duplicate key update count=count+1;
这样每次不管插入还是更新都调用这句语句就能达到我们要的效果,省了不少的判断。
insert INTO customer_refund(id,refund_no,order_no,user_id,vip,channel,mobile,status,sku_name,type,tag,description,pay_type,proportion,coupon,refund_fee,total_fee,payment,create_user,create_time)
VALUES(
#{id},#{refundNo},#{orderNo},#{userId},#{vip},#{channel},#{mobile},#{status},#{skuName},#{type},#{tag},#{description},#{payType},#{proportion},#{coupon},#{refundFee},#{totalFee},#{payment},#{createUser},#{createTime})
ON DUPLICATE KEY UPDATE
user_id=#{userId},
vip=#{vip},
channel=#{channel},
sku_name=#{skuName},
type=#{type},
tag=#{tag},
description=#{description},
pay_type=#{payType},
proportion=#{proportion},
refund_fee=#{refundFee},
total_fee=#{totalFee},
payment=#{payment},
status=#{status},
create_time=NOW()
10、日期查询 xml中<,>,&为特殊字符
<if test="endTime != null and endTime != ''">
<![CDATA[and create_time <= #{endTime}]]>
</if>
11、MyBatis提供了一个使用注解来传入多个参数的方式。这种方式需要在接口的参数上添加@Param注解
updateWorkOrderRecordJson(@Param(value = "workOrderId") long workOrderId,@Param(value = "desc") String desc);
12、Mybatis内置的日志工厂提供日志功能
<settings>
<setting name="logImpl" value="SLF4J" />
</settings>
13、在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author 的别名为 author;若有注解,则别名为其注解值。看下面的例子:
<typeAliases>
<package name="com.mryx.ams.domain"/>
</typeAliases>
14、mybatis分页拦截器(代理和反射):http://www.tuicool.com/articles/7bYjUn
Mybatis的默认实现即通过Plugin来管理Interceptor的。分页的原理是:
* 分页拦截器,用于拦截需要进行分页查询的操作,然后对其进行分页处理。 利用拦截器实现Mybatis分页的原理: * 要利用JDBC对数据库进行操作就必须要有一个对应的Statement对象,Mybatis在执行Sql语句前就会产生一个包含Sql语句的Statement对象,而且对应的Sql语句 * 是在Statement之前产生的,所以我们就可以在它生成Statement之前对用来生成Statement的Sql语句下手。在Mybatis中Statement语句是通过RoutingStatementHandler对象的 * prepare方法生成的。所以利用拦截器实现Mybatis分页的一个思路就是拦截StatementHandler接口的prepare方法,然后在拦截器方法中把Sql语句改成对应的分页查询Sql语句,之后再调用 * StatementHandler对象的prepare方法,即调用invocation.proceed()。 * 对于分页而言,在拦截器里面我们还需要做的一个操作就是统计满足当前条件的记录一共有多少,这是通过获取到了原始的Sql语句后,把它改为对应的统计语句再利用Mybatis封装好的参数和设 * 置参数的功能把Sql语句中的参数进行替换,之后再执行查询记录数的Sql语句进行总记录数的统计。
更改Sql语句这个看起来很简单,而事实上来说的话就没那么直观,因为包括sql等其他属性在内的多个属性都没有对应的方法可以直接取到,它们对外部都是封闭的,是对象的私有属性,所以这里就需要引入反射机制来获取或者更改对象的私有属性的值了
15、mybatis动态sql自动去除前缀或者后缀
<trim prefix="WHERE (" suffix=")" prefixOverrides="AND |OR ">
<if test="regions != null and regions != '' ">
or a.region in (${regions})
</if>
<if test="citys != null and citys != '' ">
or a.city in (${citys})
</if>
<if test="groups != null and groups != '' ">
or a.submit_user IN (
SELECT
wg.user_id
FROM
work_order_group g
LEFT JOIN work_order_group_user wg ON wg.group_id = g.id
WHERE
g.id in (${groups})
) OR solve_group in (${groups})
</if>
</trim>

17、模糊查询

18、批量更新


1620

被折叠的 条评论
为什么被折叠?



