动态代理Dao开发
动态代理模式开发
使用Mapper代理接口开发方式(主流)
面向接口 没有实现类
步骤:
1.创建接口:接口的名字一定要和映射文件的名字完全一样!!!
2.接口中抽象方法的名字一定要和映射文件的id属性的名字完全一样!
3.如果使用了动态代理开发namespace:必须要跟接口的全路径完全一致。
4.让maven工程的java源文件包下面能读取到xml文件
<!--让maven工程的java源文件包下面能读取到xml文件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
5.在核心配置文件中做映射配置
<!--扫描包: 扫描mapper包下面所有的接口文件-->
<package name="cn.kgc.mapper"></package>
核心配置详解
注意:标签一定要按照配置顺序书写!!!
1. 配置文件中的标签和顺序如下:
properties?, 配置属性(学习)
settings?, 全局配置:缓存,延迟加载
typeAliases?, 类型别名(学习)
typeHandlers?, 类型转换(操作)(了解)
objectFactory?, 对象工厂
plugins?, 插件:分页插件
environments?, 环境配置(数据源)
environment (环境子属性对象)
transactionManager (事务管理)
dataSource (数据源)
mappers? 引入映射配置文件(学习)
? : 一个或者零个
| : 任选其一
+ : 最少一个
* : 零个或多个
, : 必须按照此顺序编写
日志
1.可以使用Mybatis自带的日志功能
在sqlMapConfig.xml中配置
<!--mybatis自带日志功能-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
2.使用第三方的日志:log4j
第一步:引入坐标
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
第二步:导入日志文件
第三步:在sqlMapConfig.xml中配置
<!--日志配置:log4j-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
动态sql语句
1.问题
查询条件是可有可无,所以,如果不使用动态sql查询,可能就会报错
mybatis:只关注SQL语句就可以了
2.if语句
只要你满足条件,都可以执行条件中的内容
<!--
where标签:
1.当有查询条件的时候,自动添加where条件
2.当没有查询条件的时候,自动去除where条件
-->
<select id="queryActiveByCondition" parameterType="Condition"
resultType="Computers">
select * from computers
<where>
<if test="cBrand!=null and cBrand!='' ">
and brand = #{cBrand}
</if>
<if test="minPrice!=null and minPrice>=0 ">
and price >= #{minPrice}
</if>
<if test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</if>
<if test="cRunMem!=null and cRunMem>0 ">
and runMem = #{cRunMem}
</if>
<if test="cpuM!=null and cpuM!='' ">
and cpu = #{cpuM}
</if>
<if test="cVideoCard!=null and cVideoCard>0 ">
and videoCard = #{cVideoCard}
</if>
<if test="cStatus!=null and cStatus>0 ">
and `status` = #{cStatus};
</if>
</where>
order by id
3.where语句
where标签
1.当有查询条件的时候,自动添加where条件
2.当没有查询条件的时候,自动去除where条件
4.choose when otherwise语句
类似于java语句的switch语句:判断作用=如果满足条件,就执行那一个case中的代码块,break结束了;只执行一个条件,跳出switch只返回一个结果。
<!--条件查询 = choose-->
<select id="queryActiveByChoose" parameterType="Condition"
resultType="Computers">
select * from computers
<where>
<choose>
<when test="cBrand!=null and cBrand!=''">
and brand = #{cBrand}
</when>
<when test="minPrice!=null and minPrice>=0">
and price >= #{minPrice}
</when>
<when test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</when>
<otherwise>
</otherwise>
</choose>
</where>
order by id
</select>
5.update语句
<!--动态更新-->
<!--
set标签:如果有条件,就去除最后后面的,号
-->
<update id="updateActiveBySet" parameterType="Computers">
update computers
<set>
<if test="brand!=null and brand!=''">
brand = #{brand},
</if>
<if test="price!=null and price>=0">
price = #{price},
</if>
<if test="runMem!=null and runMem>0">
runMem = #{runMem},
</if>
<if test="cpu!=null and cpu!=''">
cpu = #{cpu}
</if>
</set>
where id = #{id}
</update>
6.foreach语句
<!--
查询编号是1,2的电脑信息
foreach标签:循环遍历作用
collection: 集合:list / 数组: 类型[] = Integer[]
item: 集合/数组的数据值
item="接口传递的参数的名字"
面向接口 = 面向百度
-->
<select id="queryBatch" parameterType="list"
resultType="Computers">
select * from computers where id in
<foreach collection="list" open="(" separator=","
close=")" item="cids">
#{cids}
</foreach>
</select>
7.mybatis trim 标签的使用
mybatis的trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语
句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性
插入、更新、删除或者条件查询等操作。
属性 | 描述 |
prefix | 给sql语句拼接的前缀 |
suffix | 给sql语句拼接的后缀 |
prefixOverriders | 去除sql语句前面的关键字或者字符,该关键字或者字符有prefixOverrides属性指定,假设属性指定为“and”,当sql语句开头为“and”,trim标签将会去除该“and” |
suffixOverrides | 去除SQL语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定 |
<!--条件查询 = trim-->
<!--
prefix属性:表示拼接SQL语句的前缀
=== 如果有查询条件,prefix会自动的给语句添加where
prefixOverrides属性:去除SQL语句的前缀
-->
<select id="queryActiveByTrim" parameterType="Condition"
resultType="Computers">
select * from computers
<trim prefix="where" prefixOverrides="and">
<if test="cBrand!=null and cBrand!='' ">
and brand = #{cBrand}
</if>
<if test="minPrice!=null and minPrice>=0 ">
and price >= #{minPrice}
</if>
<if test="maxPrice!=null and maxPrice>=0 ">
and price <= #{maxPrice}
</if>
<if test="cRunMem!=null and cRunMem>0 ">
and runMem = #{cRunMem}
</if>
<if test="cpuM!=null and cpuM!='' ">
and cpu = #{cpuM}
</if>
<if test="cVideoCard!=null and cVideoCard>0 ">
and videoCard = #{cVideoCard}
</if>
<if test="cStatus!=null and cStatus>0 ">
and `status` = #{cStatus};
</if>
</trim>
order by id
</select>
<!--动态更新 : if+trim-->
<!--
suffixOverrides属性: 表示去除掉后缀
=== 表示如果有后缀的,号 去掉。
-->
<update id="updateActiveByTrim" parameterType="Computers">
update computers
<trim prefix="set" suffixOverrides=",">
<if test="brand!=null and brand!=''">
brand = #{brand},
</if>
<if test="price!=null and price>=0">
price = #{price},
</if>
<if test="runMem!=null and runMem>0">
runMem = #{runMem},
</if>
<if test="cpu!=null and cpu!=''">
cpu = #{cpu}
</if>
</trim>
where id = #{id}
</update>
如果错误,请指正,谢谢!!