学习:MyBatis学习笔记

官方文档:http://www.mybatis.org/mybatis-3/zh/index.html

什么是MyBatis

半自动化ORM框架

  • Hibernate在POJO于数据库表之间建立映射,程序只需要对POJO进行操作,就达到操作数据库持久层的目的。
  • 相对于Hibernate等“一站式”ORM框架,MyBatis是一种“半自动化”的ORM框架。也就是说,ORM概念中,MyBatis只强调了OR部分的内容,而将M(mapping)部分概念进行了淡化。将sql语句的操作权,最终交给了程序员。
  • MyBatis在POJO与sql之间建立映射,具体的sql语句需要手动编写,然后通过配置文件,将sql语句所需的参数,获得的返回结果字段映射到指定的POJO。
  • 使用MyBatis提供的ORM机制,对于业务逻辑层实现人员来讲,操作的仍是Java对象,对于具体的数据库操作,需要程序员手动编写sql语句。
  • MyBatis通过在数据库移植性与SQL开发工作量部分做出让步,换来了系统设计更多的灵活性和自由空间。
  • 为什么要使用MyBatis,而不是Hibernate?

    在开发中,可能会碰到以下问题:

  • 系统的部分或全部数据都来源于现有数据库库,处于安全考虑,客户只提供一些sql或存储过程来获取数据,具体的表结构不会公开。(常见于金融、银行行业)
  • 开发过程中要求,所有涉及业务逻辑部分的数据库表的操作,必须通过存储过程进行实现。
  • 系统数据处理量巨大,对性能要求非常高。
  • 此时,使用Hibernate和JDBC都不够合适。

    MyBatis结构图

    这里写图片描述

    jar

    基础jar只需要两个:
    mybatis-..*.jar
    ojdbc*.jar

    mybatis-config.xml

    <configuration>
    <!--引用资源文件-->
        <properties resource="jdbc.properties"></properties>
        <typeAliases>
        <!--为实体类定义别名-->
            <typeAlias type="com.it.mybatis.entity.Person" alias="person"/>
        </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>
            <mapper resource="com/it/mybatis/entity/PersonMapper.xml" />
        </mappers>

    jdbc.properties

    jdbc.driver=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
    jdbc.username=scott
    jdbc.password=tiger

    实体映射文件

    <mapper namespace="com.it.mybatis.entity">
        <select id="queryById" parameterType="int" resultType="person">
            select * from person where pid = #{pid}
        </select>
        <insert id="add" parameterType="person" >
            insert into person values(se_per.nextval,#{pname},#{pwd})
        </insert>
        <update id="update" parameterType="person">
            update person set pname=#{pname}, pwd=#{pwd} where pid=#{pid}
        </update>
        <delete id="delete" parameterType="int">
            delete from person where pid=#{pid}
        </delete>
        <select id="login" parameterType="person" resultType="person">
            select * from person where pid=#{pid} and pwd=#{pwd}
        </select>
        <!--多条件查询-->
        <select id="queryAll" parameterType="person" resultType="person">
            select * from person where 1=1 
            <if test="pname!=''and pname!=null">
              and pname like '%${pname}%'
            </if>
            <if test="pid!=null and pid!=''">
              and pid=#{pid}
            </if>
        </select>
    
    </mapper>

    Util

    public class MyBatisUtil {
        static SqlSessionFactory ssf=null;
        static{
            try {
                Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
                ssf=new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        public static SqlSession getSession(){
            return ssf.openSession();
        }
    
    }
    

    dao

    public Person queryById(int pid){
            SqlSession session=MyBatisUtil.getSession();
            Person person=(Person)session.selectOne("queryById", pid);
            session.close();
            return person;      
        }
    public List<Person> queryAll(Person person) {
            SqlSession session=MyBatisUtil.getSession();
            List<Person> list=session.selectList("queryAll",person);
            session.close();
            return list;
        }

    猜你喜欢

    转载自blog.csdn.net/qq_35206244/article/details/81557319