初识MyBatis

01_原始JDBC存在的问题

原始JDBC代码
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
String sql = "insert into tb_user(username,password) values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString( 1 ,"zhangsan");
statement.setString( 2 ,"zhangsan");
statement.executeUpdate();
connection.close();
statement.close();
原始jdbc开发存在的问题如下:
  • 数据库连接创建、释放频繁造成系统资源浪费从而影响系统性能
  • sql 语句在代码中硬编码,造成代码不易维护,实际应用 sql 变化的可能较大,sql 变动需要改变java代码。
  • 查询操作时,需要手动将结果集中的数据手动封装到实体中。插入操作时,需要手动将实体的数据设置到sql语句的占位符位置
应对上述问题给出的解决方案:
  • 使用数据库连接池初始化连接资源
  • 将sql语句抽取到xml配置文件中
  • 使用反射、内省等底层技术,自动将实体与表进行属性与字段的自动映射

02_MyBatis基本概念

A.概述
  • mybatis是一个优秀的基于 java 的持久层框架,它内部封装了 jdbc,使开发者只需要关注sql 语句本身,而不需要花费精力去处理加载驱动、创建连接、创建statement 等繁杂的过程。
  • mybatis通过xml配置或注解的方式将要执行的各种 statement 配置起来,并通过java 对象和 statement 中sql 的动态参数进行映射生成最终执行的 sql 语句.
  • 最后mybatis框架执行sql并将结果映射为java对象并返回。采用ORM思想解决了实体和数
    据库映射的问题,对jdbc 进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc
    api 打交道,就可以完成对数据库的持久化操作。
B.MyBatis结构

初识MyBatis_第1张图片

  • SqlMapConfig.xml, 此文件作为mybatis的核心配置文件,配置了mybatis的运行环境等信息。
  • Mapper.xml, 即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。
  • SqlSessionFactory, 通过mybatis配置信息创建,即会话工厂对象
  • SqlSession, 由SqlSessionFactory获取,用来操作数据库。
  • Executor, mybatis底层自定的接口,用来操作数据库。
  • MappedStatement, mybatis的底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个MappedStatement对象。
  • 输入映射,包括HashMap、基本类型、pojo,Executor通过MappedStatement在执行sql前将输入的java对象映射至sql中,输入映射相当于JDBC编程中对preparedStatement设置参数。
  • 输出映射,包括HashMap、基本类型、pojo,Executor通过MappedStatement在执行sql后将输出结果映射至java对象中,输出映射相当于JDBC编程中对结果的解析处理过程。

03_MyBatis资源下载

  • https://mybatis.org/mybatis-3/zh/index.html
    初识MyBatis_第2张图片

04_Mybatis入门程序

需求:查询所有记录
a.加入jar包

在这里插入图片描述

b.创建mapper映射文件


<mapper namespace="test">
<!‐‐配置查询所有‐‐>
<select id="selectUserList" resultType="com.aaa.bean.User">
        select * from user
select>
mapper>
  • namespace :命名空间,用于隔离sql语句,后面会讲另一层非常重要的作用。
c.创建SqlMapConfig.xml文件,并加载mapper映射文件


<configuration>
<!‐‐ 和spring整合后 environments配置将废除‐‐>
<environments default="development">
<environment id="development">
<!‐‐ 使用jdbc事务管理‐‐>
<transactionManager type="JDBC" />

<!‐‐ 数据库连接池‐‐>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?
serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="mapper01.xml">mapper>
mappers>
configuration>
d.代码测试
@Override
public List<User> selectUserList() throws Exception {
    SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlMapperConfig.xml")
);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<User> userList = sqlSession.selectList("selectUserList");
    sqlSession.close();
    return userList;
}

05_mapper映射文件说明

初识MyBatis_第3张图片

06_根据id查询用户信息

A.映射文件:
  • 在mapper01.xml中添加
<select id="selectUserById" parameterType="int" resultType="cn.aaa.bean.User">
select * from user where id = #{id}
select>
B.测试代码

07_根据用户名查询用户信息(模糊查询)

A.方式一:使用#{username}
  • a.配置mapper01.xml
<select id="selectUserListLikeName1" parameterType="String"
resultType="com.aaa.bean.User">
select * from user where username like #{username}
select>
<select id="selectUserListLikeName2" parameterType="java.lang.String"
resultType="com.aaa.bean.User">
select * from user where username like "%"#{username}"%" 
select>
  • b.dao层
@Override
public List<User> selectUserListLikeName1(String name) throws Exception {
    SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<User> userList =
sqlSession.selectList("selectUserListLikeName1","%"+name+"%");
    sqlSession.close();
    return userList;
}
@Override
public List<User> selectUserListLikeName2(String name) throws Exception {
    SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
    SqlSession sqlSession = sqlSessionFactory.openSession();
    List<User> userList = sqlSession.selectList("selectUserListLikeName2",name);
    sqlSession.close();
    return userList;
}
B.方式二:使用${value}
  • a.配置mapper01.xml