一、mybatis的简介和helloworld

一、mybatis的简介:

 1.数据库交互方式:工具、框架

 工具:有jdbc ,DbUtils,jdbcTemplate;

  工具的发展历程:jdbc->DbUtils(QueryRunner)-->jdbcTemplate; 

   交互过程:编写SQL →预编译→  设置参数 → 执行SQL →封装结果

  功能简单:SQL语句编写在java代码里面,硬编码高耦合的方式、不易维护;

框架:整体解决方案;常用框架有Hibernate:全自动orm、mybatis...

  Hibernate:全自动orm(Object Relation Mapping:即bean对象和 数据库关系的映射; )框架;

  缺点:

    长难复杂SQL,对于Hibernate而言处理也不容易;

    内部自动生产的SQL,不容易做特殊优化。

    基于全映射的全自动框架,大量字段的POJO进行部分映射时比较困难。 导致数据库性能下降。

  mybatis:sql语句交给开发人员,以配置方式的方式配置,其他部分框架自动完成;半自动框架;轻量级框架

2.mybatis项目地址

https://github.com/mybatis

https://github.com/mybatis/mybatis-3

3.下载地址:

https://github.com/mybatis/mybatis-3/releases

 

二、helloworld

1.准备工作:

  • 数据库创建一张:
CREATE TABLE `tbl_employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `d_id` int(11) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)
  •  创建一个java项目 项目名称为 Helloworld;

    创建两个包,名称分别为 entity 和 test;entity中存放 Employee实体; test中存放 Junit测试类;

    创建config(与src为同级目录)文件夹,右键buildpath 点击 use as source folder;

  • Helloworld 中加入3个lib;分别为log4j-1.2.17.jar      mybatis-3.5.9.jar   mysql-connector-java-8.0.27.jar  

    

2.mybatis 的helloworld 的简单实现

步骤:

  • 编写全局配置文件,包含了数据源信息
  • sql映射文件:配置了每一个SQL以及SQL的封装规则等;
  • 将SQL映射文件注册在全局配置文件中;
  • 编写代码:

     a.根据全局配置文件得到sqlSessionFactory

        b.使用sqlSessionFactory工厂,获取到sqlSession对象使用他来执行增删该查动作,一个sqlSession代表一次会话,用完关闭连接

       c.使用SQL唯一标识(即id)来告诉mybatis执行哪个SQL;

a.在config下 创建mybatis全局配置文件:mybatis-config.xml;

MyBatis 的全局配置文件包含了影响 MyBatis 行为甚深 的设置(settings)和属性(properties)信息、如数据 库连接池信息等。指导着MyBatis进行工作。我们可以 参照官方文件的配置示例。 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6   <environments default="development">
 7     <environment id="development">
 8       <transactionManager type="JDBC"/>
 9       <dataSource type="POOLED">
10         <property name="driver" value="com.mysql.jdbc.Driver"/>
11         <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
12         <property name="username" value="root"/>
13         <property name="password" value=""/>
14       </dataSource>
15     </environment>
16   </environments>
17   <!-- 将我们写好的SQL映射文件一定哟啊注册到全局配置文件中 -->
18   <mappers>
19     <mapper resource="EmployeeMapper.xml"/>
20   </mappers>
21 </configuration>
View Code

b.创建SQL 映射文件:EmployeeMapper.xml;

映射文件的作用就相当于是定义Dao接口的实现类如何 工作。这也是我们使用MyBatis时编写的最多的文件 

 将我们写好的SQL映射文件一定注册到全局配置文件中

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="aaa">
 6 <!--
 7 namespace:命名空间;此处可以任意指定;
 8 id:唯一标识;
 9 resultType:返回值类型;
10 #{id}:从传递过来的参数中去除ID值
11  -->
12     <select id="getById" resultType="entity.Employee">
13         select LAST_NAME AS
14         lastName,gender as gender,email as email from
15         tbl_employee where id =#{id}
16     </select>
17 </mapper>
View Code

c.测试:

 1 package test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 import org.junit.jupiter.api.Test;
11 
12 import entity.Employee;
13 
14 public class MybatisTest {
15     @Test
16     public void test01() throws IOException {
17         String resource = "mybatis-config.xml";
18         InputStream inputStream = Resources.getResourceAsStream(resource);
19         // 1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象
20         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
21         // 获取SqlSessionFactory 实例
22         SqlSession openSession = sqlSessionFactory.openSession();
23         try {
24             // 使用SQL唯一标识(即id)来告诉mybatis执行哪个SQL 第一参数是 namespace+"."+id
25             Employee emmployee = openSession.selectOne("aaa.getById", 1);
26             System.out.println(emmployee);
27         } finally {
28             openSession.close();
29         }
30 
31     }
32 }

 

 运行结果:Employee [id=0, lastName=JoyLi, email=1602211058@qq.com, gender=女];

 

 3.Helloworld的接口式编程:

说明:

1.原生的数据库交互: Dao ====> DaoImpl
 mybatis: Mapper ====> xxMapper.xml
2、SqlSession代表和数据库的一次会话;用完必须关闭;
3、SqlSession和connection一样她都是非线程安全。每次使用都应该去获取新的对象。
4、mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。

  (将接口和xml进行绑定)
  EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
5、两个重要的配置文件:
mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息
sql映射文件:保存了每一个sql语句的映射信息:将sql抽取出来。

当接口和xml文件动态绑定时,MyBatis会为接口自动创建一个代理对象,通过代理对象去调用相应的方法。好处:解耦和,更安全的类型检查,明确的返回值

接口编程的步骤:

  • 在config下 创建mybatis配置文件:mybatis-config.xml;
  • 创建接口文件:EmployeeMapper.java ;(mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象(将接口和XML进行绑定))
  • 创建SQL 映射文件:EmployeeMapper.xml;注意namcespace 为接口的全类名;<select id="getById" resultType="entity.Employee">中的id为接口的方法名 
  • 测试类中通过接口去访问对应的方法;

把上边的例子转换成接口式编程:

a.创建接口:

1 package dao;
2 
3 import entity.Employee;
4 
5 public interface EmployeeMapper {
6 
7     Employee getById(int id);
8 }

b.在EmployeeMapper.xml中修改namespace。namespace为接口的全类名;

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="dao.EmployeeMapper">
 6 <!--
 7 namespace:命名空间;指定为接口的全类名
 8 id:唯一标识;
 9 resultType:返回值类型;
10 #{id}:从传递过来的参数中去除ID值
11  -->
12     <select id="getById" resultType="entity.Employee">
13         select LAST_NAME AS
14         lastName,gender as gender,email as email from
15         tbl_employee where id =#{id}
16     </select>
17 </mapper>

c.测试类:

 1    @Test
 2     public void test01() throws IOException {
 3         String resource = "mybatis-config.xml";
 4         InputStream inputStream = Resources.getResourceAsStream(resource);
 5         //1.获取SqlSessionFactory对象
 6         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 7         // 获取SqlSessionFactory 实例,能直接运行已经执行的sql语句
 8         SqlSession openSession = sqlSessionFactory.openSession();
 9         try {
10           //3.获取接口的实现类对象,当接口和xml文件动态绑定时,MyBatis会为接口自动创建一个代理对象,通过代理对象去调用相应的方法
11             EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
12             System.out.println(mapper.getById(1));
13         } finally {
14             openSession.close();
15         }
16 
17     }

 

 运行结果:Employee [id=0, lastName=JoyLi, email=1602211058@qq.com, gender=女];

 

目录接口如下:

 

 

posted @ 2022-02-08 10:58  啄木鸟伍迪  阅读(113)  评论(0编辑  收藏  举报
//火箭 GenerateContentList();