用法
1.依赖mysql-connector-java、mybatis、junit 扫描资源防止出错
2.mybatis-config.xml核心配置文件
配置环境(连接池)以及mapper(Dao层的xml文件绑定–映射器)
3.写mybatis工具包(用于处理sqlSessionFactory以及连接mybatis-config.xml文件)
4.Dao层接口,以及xml(命名空间等CRUD)
5.实体类、数据库
6.调用:得到sqlSession.getMapper(Dao层接口.class).Dao层里面的方法
最后sqlSession.close();关闭
CRUD
CRUD–namespace [id,resultType,parameterType]
parameterType(多个传入类型可以写map)(对象可以写Objecet)
模糊查询
模糊查询:1.传入%xx% 2.sql里写死’’%’’ #{} ‘’%’’
核心配置文件
mybatis-config.xml核心配置文件有顺序,mybatis默认事务器是JDBC连接池POOLED
使用properties要配置:
<properties resources="xxx.propertis"/>
<environments ...
在环境里配置 driver、url、username、password
对应的value ${driver}、${url}、${username}、${password}
别名:给实体类的包起别名,在Dao层的xml里resultType就可以直接使用了
1.起别名
<typeAliases>
<package name="实体类包"/>别名通常大小写都可以用
</typeAliases>
2.直接在实体类上@Alias(“别名”)
映射器:
1.<mapper resource /方式
2.<mapper class .方式
3.< package name=""/>包方式
生命周期作用域
sqlSessionFactoryBulder(创建了就不需要了)->sqlSessionFactory(一直运行)->sqlSession(用完就销毁)
实体类数据库不一致
1.起别名
2.在Dao xml里使用结果集映射resultMap
(写一个resultMap然后里面放希望对应的名字,使用的时候resultMap=“我们写的结果集id”)
日志
在核心配置文件settings里配置日志
使用日志工厂:
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
使用Log4j:
1.添加依赖
2.创建log4j.properties文件
3.核心配置文件
<settings>
<setting name="logImpl" value="LOG4j"/>
</settings>
4.定义
static Logger logger=Logger.getLogger(我们当前的类.class);
5.使用
logger.info
logger.debug
logger.error
分页
1.sql里使用limit
2.使用mybatis自带RowBouunds分页
RowBounds rowBounds=new RowBounds(开始,大小)
3.使用分页插件pageHelper(注意:编写时sql最后不要加;号)
使用注解开发
在mybatis里使用注解开发可以不写xxMapper.xml,但依旧要绑定核心配置文件里的
简单的使用注解、复杂的使用xml
增删改需要事务提交,我们可以在mybatis工具类里改为true就不需要我们收到再提交了: sqlSessionFactory.openSession(true)
@Param()注解—建议使用,可以防止参数名写错
复杂查询环境
1.多对一:关联association
在xxMapper.xml里
方式一:使用结果嵌套查询
方式二;按结果查询
2.一对多:集合collection
方法一:使用结果嵌套查询
方法二:不建议使用,比较复杂
总结:
多对一:关联association
一对多:集合collection
javaType—指定实体类中属性的类型
ofType-----指定映射到List或集合中的pojo类型
动态SQL
if
choose(when otherwise)
trim(where set)
foreach
1.if例子:
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog where
<if test="title != null"> 如果不为空查title
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
2.where例子:比较智能
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
3.set例子:
<!--注意set是用的逗号隔开-->
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title}, #注意,号
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id};
</update>
4.when例子:和if(只要满足都执行)差不多但when(只要满足一个后面就不执行)
<select id="queryBlogChoose" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<choose>
<when test="title !=null">
title=#{title}
</when>
<when test="author !=null">
and author=#{author}
</when>
<otherwise>
and views={views}
</otherwise>
</choose>
</select>
5.choose例子:像switch
<select id="queryBlogChoose" parameterType="map"
resultType="blog">
select * from blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
6.foreach
7.SQL片段
注意:使用where 时where会自动优化,有时就不能达到我们需要的效果了,即最好里面不使用到where
缓存
使用缓存提高查询效率
1.一级缓存(本地缓存–默认使用本地缓存,无法关闭)
一级缓存失效:查询不同东西、增删改、查询不同Mapper.xml、手动清理缓存
手动清理:sqlSession.clearCache();
2.二级缓存(全部遍历–一级缓存失效走二级缓存)
存数据:一级缓存—>失效---->二级缓存
不同的mapper数据放在自己的缓存(map)中
取数据:二级缓存—>一级缓存—>数据库
mybatis里使用二级缓存:
1.核心配置文件
<setting name="cacheEnabled" value="true"/>
2.xxxMapper.xml使用
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
或使用但可能会报错
<cache/>
3.实体类对象implements Serializable
4.测试:
SqlSession session2 = MybatisUtils.getSession();
UserMapper mapper2 = session2.getMapper(UserMapper.class);
User user2 = mapper2.queryUserById(1);
System.out.println(user2);
System.out.println(user==user2);
session2.close();