我的mybatis学习笔记

用法

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

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WsEtEeBa-1610353339241)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210111161038091.png)]

在这里插入图片描述

7.SQL片段

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rj1u3usJ-1610353339244)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210111161144980.png)]

注意:使用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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值