早期MyBatis开发与接口式Mybatis开发的简介

早期MyBatis开发与接口式Mybatis开发的简介

一、早期版本的myBatis使用  

         导jar包
            1、配置mybatis.xml的配置文件
                1)、需要加载数据库配置文档           

    <properties resource="db.properties" />

                2)、配置数据源,数据库连接池、处理事务方式

 <environments default="development">
     <environment id="development">
           <transactionManager type="JDBC"/>
           <!-- 链接数据库 的数据池 -->
           <dataSource type="POOLED">
                 <property name="username" value="${jdbc.username}" />
                 <property name="password" value="${jdbc.password}" />
                 <property name="url" value="${jdbc.url}" />
                 <property name="driver" value="${jdbc.driver}" />
            </dataSource>
      </environment>
</environments>

                3)、配置映射文件 即xml中配置的sql语句文件 文件位置需要使用/ 不能使用 .

 <mapper url="file:///var/mappers/AuthorMapper.xml"/>
 <mapper resource="org/mybatis/builder/PostMapper.xml"/>
 <mappers>
      <mapper resource="com/da/wei/mapper.xml"/>
 </mappers>

            2、配置映射mapper.xml
             

  <mapper namespace="com.cn.mybatis.mapper.EmployeeMapper">
      <!-- 命名空间使用将要映射执行方法的接口文件  在此处,原始的方法中,命名空间的作用并不明显-->
      <!-- 查询所以字段信息 -->
      <select id="selectAllEmployee" resultType="com.cn.mybatis.entity.Employee">   
         SELECT     emp_id AS empId,emp_name AS empName,emp_gender AS empGender,emp_email AS empEmail FROM employee
      </select>
      <!-- 通过id号查询员工信息 -->
      <select id="selectEmployeeById" resultType="com.cn.mybatis.entity.Employee" parameterType="java.lang.Integer">
         SELECT     emp_id AS empId,emp_name AS empName,emp_gender AS empGender,emp_email AS empEmail
          FROM employee WHERE emp_id = #{empId}
      </select>
  </mapper>

            3、执行sql操作
                执行步骤:
                    1、配置资源文件
                    2、设置输入流
                    3、通过输入流创建会话工厂
                         new SqlSessionFactoryBuilder().build(iS);
                    4、通过会话工厂创建会话
                        sqlSessionFactory.openSession();
                    5、通过会话执行sql
                        sqlSession.selectOne("com.cn.mybatis.mapper.EmployeeMapper.selectEmployeeById",1);
                        其中第一个参数是映射过去的参数,即需要执行的方法体的全限定名
                        第二个是参数,需要与设置的值类型(方法体的参数类型统一)
                        这里可以直接强转到想获得的类型,默认为Object                       
                        sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee",Employee.class);
                        这里查询返回list 可以通过使用类类型设置返回值的类型,可是不能使用参数                

              6、关闭会话
                  sqlSession.close();

 1   private static SqlSessionFactory getSqlSessionFactory(){
 2       //1、配置资源文件
 3       String source = "mybatis.xml";
 4       //2、设置输入流
 5       InputStream iS = null;
 6       //3、创建会话工厂
 7       SqlSessionFactory sqlSessionFactory = null;
 8       try {
 9             //4、将配置文件以流的方式读入
10             iS = Resources.getResourceAsStream(source);
11             //5、根据配置文件创建会话工场
12            sqlSessionFactory = new SqlSessionFactoryBuilder().build(iS);
13            return sqlSessionFactory;
14              } catch (IOException e) {
15               // TODO Auto-generated catch block
16                e.printStackTrace();
17             }
18       return null;
19 }
20 public static void test01(){
21     SqlSessionFactory sqlSessionFactory = null;
22     SqlSession sqlSession = null;
23     //获取会话工厂
24     sqlSessionFactory = getSqlSessionFactory();
25     if(sqlSessionFactory!=null){
26         //6、由会话工程创建会话
27         sqlSession = sqlSessionFactory.openSession();
28         //7、由会话执行sql
29        Employee employee = sqlSession.selectOne("com.cn.mybatis.mapper.EmployeeMapper.selectEmployeeById",1);
30        System.out.println(employee);
31        System.out.println("==============标准割==============");
32        List<Employee> listEmployee = sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee",Employee.class);
33         //    System.out.println(listEmployee);
34         for(Employee emp : listEmployee){
35                 System.out.println(employee);
36           }           //sqlSession.selectList("com.cn.mybatis.mapper.EmployeeMapper.selectAllEmployee", rowBounds)
37          sqlSession.close(); 
38    }
39 }

        早期版本的myBatis其执行的过程为,    
            1、在使用时,通过引用资源mybatis.xml,创建会话工厂
            2、业务执行时,加载mybatis的配置文件
                在mybatis中加载数据库的配置文件db.properties文件,进行数据库数据源配置,同时进行数据连接池,数据库事务管理的配置。
            3、在mybatis中加载sql映射文件mapper.xml
            4、会话工厂创建会话,通过创建出来的会话进行功能调用
                调用时,其中的statement参数需要执行,执行方法的全限定名。
            5、使用过后进行会话的关闭。                      

 二、接口式版本开发        

      接口版本配置基本一致          

    1、mybatis.xml配置
                其中映射使用的mapper.xml需要指定到对应的包下
            2、mapper.xml配置
                按照开发规范,这里需要将mapper的名字配置与接口方法同名且在同一包下。
                    且命名空间此时用有效需要设置,其值是对应的mapper.xml的接口文件全限定名。
            3、使用

      在调用时不再是以前的那种statement的配置方式了
                通过创建出来的会话,通过获取映射的方式,获取mapper方法接口的代理对象,使用其代理对象进行方法体的直接调用
                //由会话工程创建会话
                sqlSession = sqlSessionFactory.openSession();
                //通过会话获取方法->代理对象
                EmployeeMapper employeeMapper = sqlSession.getMapper(com.cn.mybatis.mapper.EmployeeMapper.class);
                List<Employee> listEmployee = employeeMapper.selectAllEmployee();
                Employee employee = employeeMapper.selectEmployeeById(4);    

 以上两种方式的比较:            

  1、接口方式的sql语句mapper.xml的需要按照规范,与接口方法放在同包下,且保持同名。
                    早期方法只需要在mybatis下指定对应的地址即可
                接口的方式更加规范和易读,可以提高开发效率。    
            2、接口方法在调用时,接口采用通过会话工厂创建代理对象的方式,使用代理对象调用接口中的方法使之实现。
                    早期方法使用会话直接使用,使用会话的方法指定对应的statement(方法的全限定名)进行sql操作。
                接口方式由于直接调用方法,因此可以对参数进行检验验证。

原文地址:https://www.cnblogs.com/Mr-Dawei/p/7469025.html