Mybatis

作者: 她与星河皆遗憾 | 来源:发表于2019-05-06 19:48 被阅读0次

1,Mybatis是什么?

Mybatis是一个持久层的架构,是apache下的顶级项目。

Mybatis原先是托管在googlecode下,再后来是托管在Github上。

Mybatis让程序员将主要的精力放在sql上,通过Mybatis提供的映射方式,自由灵活生成(半自动,大部分需要程序员编写sql)满足需要sql语句。

Mybatis可以将向preparedStatement中的输入参数自动进行输入映射,将查询结果集灵活的映射成java对象。(输出映射

2,Mybatis框架

image

单例模式的SqlSessionFactory

//调用mapper
        //1.构建session工厂
        InputStream inputStream = null;
        SqlSessionFactory sqlSessionFactory = null;
        SqlSession sqlSession = null;
        try {
            //1.得到配置文件流
            inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.创建会话工厂,传入Mybatis的配置文件信息
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //3.利用工厂对象,创建SqlSession对象
            sqlSession = sqlSessionFactory.openSession();
            //4.代理模式
            UserinfoMapper userinfoMapper = sqlSession.getMapper(UserinfoMapper.class);
}catch (IOException e) {
            e.printStackTrace();
        }

注解:

  • SqlMapConfig.xml(Mybatis的全局配置文件,名称不定)配置了数据源,事务等Mybatis运行环境

  • Mapper.xml映射文件(配置sql语句)

  • SqlSessionFactory(会话工厂)根据配置文件配置工厂,创建SqlSession

  • SqlSession(会话)面向用户的接口,操作数据库(发出sql增仓改查)

  • Executor(执行器)是一个接口(基本执行器,缓存执行器),SqlSession内部通过执行器操作数据库

  • Mapped Statement(底层封装对象)对操作数据库存储封装,包括sql语句,输入参数,输出结果类型

3.log4j.properties

在类路径下创建log4j.properties如下:
# Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4.SqlMapConfig.xml

配置Mybatis的运行环境,数据源,事务等

<?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>
    <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>
   <!-- mapper映射器(mapper.xml文件所在包) -->
    <mappers>
        <package name="com.mapper"></package>
    </mappers>
</configuration>

动态 SQL

通过mybatis提供的各种标签方法实现动态拼接sql。

    <select id="findUserList2" parameterType="User" resultType="User">
        select * from user
        <!--where可以自动的去掉条件中的第一个and-->
        <where>
                <if test="sex != null and sex != ''">
                    and user.sex = #{sex}
                </if>
                <if test="username != null">
                    and user.username like  #{username}
                </if>
        </where>
    </select>

测试代码:因为设置了动态的sql,如果不设置某个值,那么条件就不会拼接在sql上
添加的配置为 archetypeCatalog=internal

相关文章

网友评论

      本文标题:Mybatis

      本文链接:https://www.haomeiwen.com/subject/pctjoqtx.html