Web基础(六):Mybatis
目录
一、作用
代替jdbc,解决jdbc的硬编码、繁杂代码问题
1. 硬编码(注册驱动,获取连接) -> mybatis-conf.xml
2. 繁杂代码(定义sql语句) -> XxxMapper.xml
二、环境准备
(一)三步走
1. maven配置
创建maven项目
设置 - 构建工具 - maven主路径、配置路径
2. pom.xml导入依赖
mybatis、mysql、junit、slf4j-api、logback-classic、logback-core
3. resource配置文件
mybatis核心配置、sql映射、logback配置
(二)mybatis核心配置文件
注意:核心配置文件标签要遵循MyBatis官网的结构顺序
1. 加载数据源,配置驱动信息、数据库连接信息
<environments default="默认数据源id">
<environment id="数据源id">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/数据库名?useSSL=false"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
</dataSource>
</environment>
</environments>
2. 加载sql映射
(1)逐个加载映射:<mapper>resource属性,指定sql映射路径
(2)批量加载映射:<package>name属性,指定sql映射包全名
<!--1. 逐个加载映射-->
<mappers>
<mapper resource="com/liu/mybatis/mapper/XxxMapper.xml"/>
</mappers>
<!--2. 批量加载映射-->
<mappers>
<package name="com.liu.mybatis.mapper"/>
</mappers>
3. 起类别名
<typeAliases>
<typeAlias type="类全限定名" alias="类别名"/>
</typeAliases>
(三)sql映射
1. 映射根配置:namespace 属性
作用:区分不同映射
(1) XML 无接口:自定义唯一名称,区分映射
(2) Mapper 接口:填写接口全类名,绑定接口
<!--1. XML无接口开发:自定义-->
<mapper namespace="XxxMapper">
</mapper>
<!--2. Mapper接口开发:接口全限定名-->
<mapper namespace="com.liu.mybatis.mapper.XxxMapper">
</mapper>
2. sql声明标签
(1)标签类型:select/insert/update/delete
(2)属性:id,resultType / resultMap
注意:resultType和resultMap - type,填实体类的全限定名/别名
<!--1. resultType-->
<select id="sqlID" resultType="结果集要封装成的目标实体类">
select * from tb_user
</select>
<!--2. resultMap-->
<resultMap id="resultMapID" type="结果集要封装成的目标实体类">
<result column="字段名" property="字段别名"/>
</resultMap>
<select id="sqlID" resultMap="resultMapID">
</select>
3. sql内容编写
(1)参数占位符
① #{}:预编译占位符,防 SQL 注入,自动加引号
② ${}:字符串直接拼接,有注入风险,不加引号
(2)特殊字符处理
①转义字符:< > &
②CDATA 区:<![CDATA[ 内容 ]]>
(3)模糊查询
①like关键字
②处理参数:%通配符
(4)动态 SQL 标签
①where:剔除开头多余 and/or;无查询条件时,不生成where语句
②if:条件成立,拼接 SQL
③choose:多条件单选,类似if-else if-else
<!--1. if标签-->
select *
from tb_brand
<where>
<if test="条件"></if>
</where>
<!--2. choose标签-->
select *
from tb_brand
<where>
<choose>
<when test="条件"></when>
<otherwise></otherwise>
</choose>
</where>
三、XML无接口开发
(一)准备pojo,起类别名
(二)核心配置文件
(三)sql映射
(四)测试执行sql
1. 加载配置文件,获取SqlSessionFactory对象
(1)InputStream inputStream = Resources.getResourceAsStream(mybatis核心配置文件路径);
(2)SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
2. 获取SqlSession对象,用于执行sql
sqlSessionFactory.openSession()
3. 执行sql
sqlSession.selectList("namespace.sqlId")
4. 释放资源
sqlSession.close()
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--XML无接口开发:namespace为自定义-->
<mapper namespace="UserMapper">
<select id="selectAll" resultType="com.liu.mybatis.pojo.User">
select * from tb_user
</select>
</mapper>
public class selectAll {
public static void main(String[] args) throws IOException {
//1. 加载核心配置文件,获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象,用于执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 执行sql
List<User> sl = sqlSession.selectList("UserMapper.selectAll");
System.out.println(sl);
//4. 释放资源
sqlSession.close();
}
}
四、Mapper接口开发
(一)准备pojo,起类别名
(二)Mapper接口
(三)sql映射
(四)执行sql
1. 加载配置文件,获取SqlSessionFactory对象
2. 获取SqlSession对象
3. 获取Mapper代理对象,用于执行sql
sqlSession.getMapper(XxxMapper.class)
4. 执行sql
xxxMapper.接口方法()
5. 释放资源
public interface BrandMapper {
//返回值类型与sql封装类型对应:MyBatis根据接口返回值类型,自动封装sql查询结果。
//方法名与sql声明ID一致
List<Brand> selectAll();
}
<!--Mapper接口开发:namespace为接口全限定名-->
<mapper namespace="com.liu.mybatis.mapper.BrandMapper">
<resultMap id="brandResultMap" type="Brand">
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select id,brand_name,company_name,ordered,description,status from tb_brand
</select>
</mapper>
@Test
public void testSelectBrand() throws IOException {
//1. 加载核心配置文件,获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象,用于执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 获取UserMapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 执行sql
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
//5. 释放资源
sqlSession.close();
}
(五)同名规则
1. 接口与映射的名字、目录相同
2. 接口方法名与sql声明ID相同
3. 接口方法返回值类型与sql结果集要封装成的目标实体类对应
4. 传递参数:
(1)散装参数:散装参数名与sql占位符名称相同
(2)实体类参数:实体类属性名与sql占位符名称相同
(3)Map集合参数:键名与sql占位符名称相同
五、查询
1. 查询所有
2. 通过id查询
3. 多条件查询
4. 多条件动态查询
5. 单条件动态查询
更多推荐




所有评论(0)