1.什么是Mybatis
- Mybatis是一款优秀的持久化框架
- MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
Mybatis的优点
- 优点
- sql和代码的分离,提高了可维护性
- 提供映射标签,支持对象与数据库的orm字段关系映射
- 提供对象关系映射标签,支持对象关系组建维护
- 提供xml标签,支持编写动态sql。
2.Mybatis的流程
思路–> 创建Maven项目 --> 在pom.xml中导入需要的jar包和需要的工具类–> 创建核心配置文件mybatis-config.xml -->书写接口和Mapper.xml --> 在核心配置文件中注册驱动 --> 测试
CRUD
2.1、namespace
namespace中的包名要和 Dao/mapper 接口的包名一致!
2.2、select
选择,查询语句;
- id : 就是对应的namespace中的方法名;
- resultType:Sql语句执行的返回值!
- parameterType : 参数类型!
2.3、Insert
<!--对象中的属性,可以直接取出来-->
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
2.4、update
<update id="updateUser" parameterType="com.kuang.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id} ;
</update>
2.5、Delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id};
</delete>
遇到复杂的属性,或者字段过多,优先用map,实际业务中,我们也经常使用map,只需要设置key,和value即可
3.配置解析
3.1、核心配置文件
- mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration核心配置文件-->
<configuration>
<!--configuration(配置)-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
- MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。
properties(属性):引入外部properties文件(数据库)<!--引入外部配置文件--> <properties resource="db.properties"> </properties>
settings(设置):这个是日志文件的配置,自带的有:STDOUT_LOGGING,使用前需要导包
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
typeAliases(类型别名):给类型起别名,在resultType就可以直接写
<!--可以给实体类起别名-->
<typeAliases>
<!--package默认为类名的首字母小写-->
<package name="com.wmx.pojo"/>
</typeAliases>
<!--可以给实体类起别名-->
<typeAliases>
<typeAlias type="com.wmx.pojo.User" alias="User"/>
</typeAliases>
mappers(映射器):注册绑定我们的Mapper文件;
-每一个Mapper.XML都需要在Mybatis核心配置文件中注册!
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<!--方式一: 【推荐使用】-->
<mappers>
<mapper resource="com/wmx/dao/UserMapper.xml"/>
</mappers>
<!--方式二:使用class文件绑定注册-->
<mappers>
<mapper class="com.wmx.dao.UserMapper"/>
</mappers>
<!--方式三:使用扫描包进行注入绑定-->
<mappers>
<package name="com.kuang.dao"/>
</mappers>
接口和他的Mapper配置文件必须同名!
接口和他的Mapper配置文件必须在同一个包下
解决数据库字段名与实体类对应的属性名不一致
当数据库字段名与实体类对应的属性名不一致时,有两种解决方式:
1.在xml文件中指定resultMap,指定id,下面需要的直接引用id就可以;
2.使用注解开发时,通过注解@Results来指定对应关系
resultMap标签
resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
使用:
<resultMap type="com.wmx.domain.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
</resultMap>
其中:
id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称
resultMap
元素是 MyBatis 中最重要最强大的元素- ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。
ResultMap
最优秀的地方在于,虽然你已经对它相当了解了,但是根本就不需要显式地用到他们。- 如果世界总是这么简单就好了。
2.使用注解开发
可以直接在接口上面使用注解开发,但实际开发环境不建议这样做!
关于@Param() 注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,但是建议大家都加上!
- 我们在SQL中引用的就是我们这里的 @Param() 中设定的属性名!
动态SQL(重要)
什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句
IF
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
choose (when, otherwise)
<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>
</where>
</select>
trim (where,set)
select * from mybatis.blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id}
</update>
where 和 set 标签在实际执行sql过程中,会自动帮忙去掉逗号或者是and
比较好用!
还有一级缓存和二级缓存就不写了!
最后在实际开发环境中
我们经常会使用map当参数来进行传参进行一系列的CRUD工作
类似
BlogMapper.java
public interface BlogMapper {
//插入数据
int addBlog(Blog blog);
//更新博客
int updateBlog(Map map);
//查询博客
List<Blog> queryBlogIF(Map map);
List<Blog> queryBlogChoose(Map map);
//根据Foreach遍历Blog
List<Blog> queryBlogForeach(Map map);
}
BlogMapper.xml文件
<sql id="if-title-author">
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<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}
</where>
</update>