Mybatis入门

1、建立mybatis配置文件SqlMapConfig.xml(名称不唯一),它是mybatis的全局配置文件:

--properties属性:将数据库连接参数放到db.properties中;

--settings全局参数配置:参数会影响mybatis的运行行为;

--typeAliases(别名):可以针对parameterType或resultType指定的类型定义一些别名,方便开发;

--typeHandlers(类型处理器):通过typeHandlers完成jdbc类型和java类型的转换,mybatis提供的类型处理器满足日常需求,一般不需自定义;

--mappers(映射配置):

--SqlMapConfig.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>
    <!-- 加载属性文件 -->
    <properties resource="db.properties"></properties>
    <!-- 全局配置参数 -->
    <!-- <settings></settings> -->
    <!-- 别名定义 -->
    <typeAliases>
        <!-- 定义单个别名 type:原类型   alias:别名 -->
        <typeAlias type="com.tt.po.Customer" alias="customer"/>
        <!-- 批量定义别名:指定包名,mybatis自动扫描包中的po类,自动定义别名,别名为类型 -->
        <package name="com.tt.po"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 加载映射文件 -->
    <mappers>
        <!-- 加载单个映射文件:通过resource加载 -->
        <mapper resource="sqlmap/Customer.xml"/>
        <!-- 通过mapper接口加载:通过class加载
            需要遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名保持一致,且在一个目录中
            前提:使用mapper代理方法 -->
        <!-- <mapper class="com.tt.mybatis.mapper.CustomerMapper"/> -->
        
        <!-- 批量加载mapper(推荐使用)
        指定mapper接口的包名,mybatis自动加载,同样也需要遵循上述规范-->
        <package name="com.tt.mybatis.mapper"/>
    </mappers>
</configuration>

--映射文件Customer.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">

<!-- 命名空间:其作用是对sql进行分类管理 -->
<mapper namespace="test">

    <!-- 
        配置sql语句
        通过select执行数据库查询:
            id标识映射文件中的sql
            parameterType为输入参数的类型;
            resultType为输出参数的Java对象类型 
    -->
    <select id="findCustomerById" parameterType="int" resultType="com.tt.po.Customer">
        <!-- #{value}表示一个占位符,其中的value可以为任何名称 -->
        select * from customers where id=#{id}
    </select>
    
    <select id="findCustomerByName" parameterType="java.lang.String" resultType="com.tt.po.Customer">
        <!-- ${value}表示字符串拼接,{}中的变量只能为value,存在sql注入风险,不建议使用 -->
        select * from customers where name like '%${value}%';
    </select>
</mapper>

2、创建会话工厂:SqlSessionFactory;

3、通过会话工厂产生SqlSession: new SqlSessionFactoryBuilder().build(inputStream);

--SqlSession内部通过执行器Executor操作数据库

--底层封装对象: mapped statement,对数据库存储封装,包括sql语句、输入参数、输出参数;

public void mybatisTest(){
    
    //mybatis的配置文件
    String resource = "SqlMapConfig.xml";
    
    SqlSession sqlSession = null;
    try {
        InputStream inputStream = Resources.getResourceAsStream(resource);
        
        //通过工厂创建SqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            
        //创建会话工厂
        sqlSession = sqlSessionFactory.openSession();
            
        //通过SqlSession操作数据库
        Customer customer = sqlSession.selectOne("test.findCustomerById", 1);
        System.out.println(customer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        //释放资源,关闭SqlSession
        sqlSession.close();
    }
}

4、mybatis工程结构:

原文地址:https://www.cnblogs.com/tengtao93/p/5105714.html