MyBatis系列第1篇:MyBatis简介

前言

MyBatis3以前2.0版本是ibatis

一、简介

以前是从JDBC–>DBUtils(QueryRunner)–>spring的JdbcTemplate:工具
Hibernate:全自动的ORM(Object Relation Mapping 将JavaBean对象对应数据库每一条记录)框架
简介:
1.MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。
2.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。
3.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
MyBatis是一个半自动化的持久化层框架。
sql和java编码分开,功能边界清晰,一个专注业务、一个专注数据。

二、入门案例

MyBatis下载
解压mybatis-3.4.1.zip有个mybatis.pdf文档、目录3. Getting Started .案例快速开始

1.创建全局配置文件mybtais-config.xml

<?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:3307/mybatis"/>
                <property name="username" value="liming"/>
                <property name="password" value="liming"/>
            </dataSource>
        </environment>
    </environments>
    <!--写好的sql映射文件已经要注册到全局配置文件中-->
    <mappers>
        <mapper resource="EmployeeMapper.xml"/>
    </mappers>
</configuration>

2.创建SqlSessionFactory对象

案例步骤:
1、根据xml配置文件(全局配置文件mybatis-config.xml)创建一个SqlsessionFactory对象,mybatis-config.xml有数据源一些环境信息
2、sql映射文件EmployeeMapper.xml配置了每一个sql,以及sql的封装规则等。
3、将sql映射文件注册在全局配置文件中
4、写代码:
1.根据全局配置文件得到sqlsessionFactory
2.使用SqlSession工程进行crud,sqlseesion就代表和数据库进行会话,用完close
3.使用sql标识告知mybatis来执行哪个sql,sql都是保存在sql映射文件中
注意:数据库字段last_name和pojo字段lastName没有映射成功,可以修改sql语句起别名的方式
EmployeeMapper.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.ming.po.EmployeeMapper">
    <!--
    namespace名称空间
    id:唯一标识
    resultType:返回值类型
     #{id}:从传递过来参数中取出id值
    -->
    <select id="selectEmp" resultType="com.ming.po.Employee">
        select id,last_name AS lastName,gender,email from tbl_employee where id = #{id}
    </select>
</mapper>

测试类

package com.ming.test;
import com.ming.po.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
/**
 * 测试mybatis-config.xml
 */
public class MyBatisTest {
    /**
     * 1、根据xml配置文件(全局配置文件mybatis-config.xml)创建一个SqlsessionFactory对象,mybatis-config.xml有数据源一些环境信息
     * 2、sql映射文件EmployeeMapper.xml配置了每一个sql,以及sql的封装规则等。
     * 3、将sql映射文件注册在全局配置文件中
     * 4、写代码:
     * 4.1.根据全局配置文件得到sqlsessionFactory
     * 4.2.使用SqlSession工程进行crud,sqlseesion就代表和数据库进行会话,用完close
     * 4.3.使用sql标识告知mybatis来执行哪个sql,sql都是保存在sql映射文件中
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取SqlSession实例,能直接执行已经映射了的sql语句,selectOne:sql唯一标识,执行sql要用到的参数
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            Employee employee= openSession.selectOne("com.ming.po.EmployeeMapper.selectEmp",1);
            System.out.println(employee);
        }finally {
            openSession.close();
        }
    }
}


3.接口式编程

1.原生:Dao ===>DaoImpl
mybatis: Mapper -->**Mapper.xml
2.SqlSession代表和数据库的一次会话,用完close()
3.SqlSession和connection一样都是非线程安全,每次使用都应该获取新的对象
4.mapper接口没有实现类,mybatis会为这个接口生成一个代理对象。IEmployeeMapper employeeMapper = sqlSession.getMapper(IEmployeeMapper.class);将接口和xml进行绑定,会为接口自动创建一个对象,代理对象去执行增删改查的方法。
5.两个重要配置文件:
mybatis的全局配置文件(包括数据库连接池,事物管理信息等)系统运行环境信息
sql映射文件:sql语句的映射信息

    @Test
    public void testInterface() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            IEmployeeMapper employeeMapper = sqlSession.getMapper(IEmployeeMapper.class);
            Employee employee = employeeMapper.getEmpByID(1);
            System.out.println(employee);
        }finally {
            sqlSession.close();
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值