zoukankan      html  css  js  c++  java
  • mybatis源码简单分析

    • mybatis入门介绍

    /**
    * 1. 接口式编程
    * 原生: Dao =====> DaoImpl
    * mybatis : Mapper =====> xxxMapper
    * 2. SqlSession 代表数据库的一次会话, 用完需要关闭会话
    * 3. SqlSession Connection 一样都是线程不安全的, 所以不能放在堆区(类成员对象), 所以每次创建都应该是新的对象
    * 4. mapper接口没有实现类, 但是mybatis会为其创建代理对象
    * 将接口和xml进行绑定
    * EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
    *
    */
    public class MybatisTest {

    /**
    * 旧方式
    */
    @Test
    public void test() {
    SqlSession sqlSession = null;
    String resource = "SqlMapConfig.xml";
    try (
    InputStream inputStream = Resources.getResourceAsStream(resource);
    ) {
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    sqlSession = sqlSessionFactory.openSession();
    Employee employee = sqlSession.selectOne("com.zhazha.mybatis.bean.Employee.selectBlog", 1);
    System.out.println(employee);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (null != sqlSession) {
    sqlSession.close();
    }
    }
    }

    /**
    * 新方式
    * @throws IOException
    */
    @Test
    public void test1() throws IOException {
    InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = factory.openSession();
    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
    Employee employee = mapper.get(1);
    System.out.println(employee);
    sqlSession.close();
    }
    }

    旧方式xml

    <?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">
    <mapper namespace="com.zhazha.mybatis.bean.Employee">
    <select id="selectBlog" resultType="com.zhazha.mybatis.bean.Employee">
    SELECT te.id, te.last_name AS lastName, te.email, te.gender FROM tbl_employee te WHERE te.id = #{id}
    </select>
    </mapper>

    新方式xml

    <?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">
    <!-- Mapper接口 -->
    <mapper namespace="com.zhazha.mapper.EmployeeMapper">
    <!-- get mapper方法的函数名称 -->
    <select id="get" resultType="com.zhazha.mybatis.bean.Employee">
    SELECT te.id, te.last_name AS lastName, te.email, te.gender FROM tbl_employee te WHERE te.id = #{id}
    </select>
    </mapper>

    mybatis的sql映射文件

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC" />
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="EmployeeMapper.xml" />
    <mapper resource="Employee.xml" />
    </mappers>
    </configuration>

    log4Jxml配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <param name="Encoding" value="UTF-8" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) " />
    </layout>
    </appender>
    <logger name="java.sql">
    <level value="debug" />
    </logger>
    <logger name="org.apache.ibatis">
    <level value="info" />
    </logger>
    <root>
    <level value="debug" />
    <appender-ref ref="STDOUT" />
    </root>
    </log4j:configuration>

    • mybatis初始化过程源码理解

    • mybatis调用过程


    上面就是sql语句存放位置和方法签名存放位置数据结构









  • 相关阅读:
    Jquery操作select
    Session在类库中的使用
    从一个表取数据更新另一个表的信息
    判断数据库表中是否存在某个字段
    .net项目中上传的图片或者文件太大 无法上传
    计算机中丢失 msvcr110.dlll
    Ubuntu搭建FTP server
    Linux常用命令集
    系统清理篇
    ubuntu安装 ssh server
  • 原文地址:https://www.cnblogs.com/bangiao/p/12418389.html
Copyright © 2011-2022 走看看