一:Mybatis概述
1.Mybatis概念
MyBatis
是一款优秀的
持久层框架
,用于简化
JDBC
开发;
MyBatis
本是
Apache
的一个开源项目
iBatis, 2010
年这个项目由
apache software foundation
迁移到了
google code,并且改名为
MyBatis
。
2013
年
11
月迁移到
Github;
官网:
https://mybatis.org/mybatis-3/zh/index.html
持久层:
负责将数据到保存到数据库的那一层代码。
以后开发我们会将操作数据库的
Java
代码作为持久层。而
Mybatis
就是对
jdbc
代码进行了封装。
JavaEE
三层架构:表现层、业务层、持久层
框架:
框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
在框架的基础之上构建软件编写更加高效、规范、通用、可扩展
2.JDBC缺点
硬编码:
注册驱动、获取连接
代码有很多字符串,而这些是连接数据库的基本信息,以后如果要将
Mysql
数据库换成其他的关系型数据库的话,这些地方都需要修改,如果放在此处就意味着要修改我们的源代码。
SQL
语句
如果表结构发生变化,
SQL
语句就要进行更改。这也不方便后期的维护。
操作繁琐:
手动设置参数
手动封装结果集
3.Mybatis 优化
硬编码可以配置到
配置文件
;
操作繁琐的地方mybatis
都
自动完成
二:MyBatis快速入门
1.步骤
2.解决SQL映射文件的警告提示
产生原因:idea和数据库没有建立连接,不识别表信息
解决方式:在idea中配置MySQL数据库连接
三:Mapper代理开发
四:MyBatis核心配置文件
五:配置文件完成增删改查
1.简单查询
(1)操作
(2)参数占位符
#{}:会将其替换为?,为了防止SQL注入问题
${}:拼sql,会存在SQL注入问题
使用时机:
*参数传递的时候:#{}
*表明或者列名不固定的情况下:${}会存在sql注入问题
(3)parameterType:
用于设置参数类型,该参数可以省略
(4)SQL语句中特殊字符处理
*转义字符
*<!CDATA[内容]]>
2.多条件查询
(1)步骤
(2)编写接口方法
(3)编写SQL语句
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
where status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</select>
3.多条件动态条件查询
if:用于判断参数是否有值,使用test属性进行判断
*存在的问题:第一个条件不需要逻辑运算符
*解决方案:
1)使用恒等式让所有条件格式都一样
2)<where>标签替换where关键字
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test="brandName != null and brandName != '' ">
and brand_name like #{brandName}
</if>
</where>
</select>
4.单条件动态查询
从多个条件中选择一个
chhoose(when,otherwise):选择,类似于java中的switch语句
(1)编写接口方法
/**
* 单条件动态查询
* @param brand
* @return
*/
List<Brand> selectByConditionSingle(Brand brand);
(2)编写SQL语句
在
BrandMapper.xml
映射配置文件中编写
statement
,使用
resultMap
而不是使用
resultType
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose><!--相当于switch-->
<when test="status != null"><!--相当于case-->
status = #{status}
</when>
<when test="companyName != null and companyName != '' "><!--相当于case-->
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != ''"><!--相当于case-->
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
(3)编写测试方法
@Test
public void testSelectByConditionSingle() throws IOException {
//接收参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
// 处理参数
companyName = "%" + companyName + "%";
brandName = "%" + brandName + "%";
//封装对象
Brand brand = new Brand();
//brand.setStatus(status);
brand.setCompanyName(companyName);
//brand.setBrandName(brandName);
//1. 获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 执行方法
List<Brand> brands = brandMapper.selectByConditionSingle(brand);
System.out.println(brands);
//5. 释放资源
sqlSession.close();
}
5.添加
(1)添加
(2)主键返回
6.修改
(1)修改全部字段
(2)修改动态字段
7.删除
(1)删除一个
(2)批量删除
六:参数传递
1.多个参数:
封装为Map集合
以 arg 开头 :第一个参数就叫
arg0
,第二个参数就叫 arg1,以此类推。如:
map.put("arg0",参数值1);
map.put("arg1",参数值2);
以 param 开头 : 第一个参数就叫
param1
,第二个参数就叫 param2,依次类推。如
map.put("param1",参数值1);
map.put("param2",参数值2);
2.单个参数
POJO
类型 :直接使用。要求属性名
和
参数占位符名称
一致
Map
集合类型 :直接使用。要求 map
集合的键名
和
参数占位符名称
一致
Collection
集合类型:Mybatis 会将集合封装到
map
集合中,如下:
map.put("arg0",
collection
集合
);
map.put("collection",collection
集合
;
可以使用 @Param
注解替换
map
集合中默认的
arg
键名。
List
集合类型:Mybatis 会将集合封装到
map
集合中,如下:
map.put("arg0",
list
集合
);
map.put("collection",
list
集合
);
map.put("list",
list
集合
);
可以使用 @Param
注解替换
map
集合中默认的
arg
键名。
Array
类型:Mybatis 会将集合封装到
map
集合中,如下:
map.put("arg0",数组
);
map.put("array",数组
);
可以使用 @Param
注解替换
map
集合中默认的
arg
键名。
其他类型:
比如
int
类型,
参数占位符名称
叫什么都可以。尽量做到见名知意