mybatis(Mybatis与hibernate的不同、原始Dao开发、Mapper动态代理开发)

1、Mybatis与hibernate的不同

(1)Mybatis和hibernate不同,它不完全是一个ORM框架,因为MyBatis需要程序员自己编写Sql语句。mybatis可以通过XML或注解方式灵活配置要运行的sql语句,并将java对象和sql语句映射生成最终执行的sql,最后将sql执行的结果再映射生成java对象。

mybatis需要在配置文件中书写sql语句:

    <delete id="deleteStudentById" parameterType="Integer">
        delete from student
        where studentno = #{value}
    </delete>

(2)Mybatis学习门槛低,简单易学,程序员直接编写原生态sql,可严格控制sql执行性能,灵活度高,非常适合对关系数据模型要求不高的软件开发,例如互联网软件、企业运营类软件等,因为这类软件需求变化频繁,一但需求变化要求成果输出迅速。但是灵活的前提是mybatis无法做到数据库无关性,如果需要实现支持多种数据库的软件则需要自定义多套sql映射文件,工作量大。

(3)Hibernate对象/关系映射能力强,数据库无关性好,对于关系模型要求高的软件(例如需求固定的定制化软件)如果用hibernate开发可以节省很多代码,提高效率。但是Hibernate的学习门槛高,要精通门槛更高,而且怎么设计O/R映射,在性能和对象模型之间如何权衡,以及怎样用好Hibernate需要具有很强的经验和能力才行。总之,按照用户的需求在有限的资源环境下只要能做出维护性、扩展性良好的软件架构都是好架构,所以框架只有适合才是最好。

2、原始Dao开发

(1)创建接口:

public interface StudentDao {
    public Student selectStudentById(Integer id);
}

(2)创建接口的实现类:

public class StudentDaoImp implements StudentDao{
    private SqlSessionFactory sqlSessionFactory;

    public StudentDaoImp(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public Student selectStudentById(Integer studentno){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession.selectOne("test.findStudentById", studentno);
    }
}

(3)创建测试类:

public class Test1 {
    public SqlSessionFactory sqlSessionFactory;

    public void before() throws Exception {
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }

    public void testDao() throws Exception {
        StudentDao studentDao = new StudentDaoImp(sqlSessionFactory);
        Student student=studentDao.selectStudentById(201811);
        System.out.println(student);
    }

    public static void main(String args []){
        Test1 test1=new Test1();
        try {
            test1.before();
            test1.testDao();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
DEBUG [main] - Logging initialized using ‘class org.apache.ibatis.logging.slf4j.Slf4jImpl‘ adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1750905143.
DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]]
DEBUG [main] - ==>  Preparing: select * from student where studentno = ?
DEBUG [main] - ==> Parameters: 201811(Integer)
DEBUG [main] - <==      Total: 1
Student [studentno=201811, sname=zhai, sex=男, birthday=1998-11-11, classno=tx171, point=890, phone=1234567890, email=null]

3、mapper动态代理开发

(1)接口:

public interface StudentMapper {
     Student findStudentById(Integer studentno);
}

Mapper接口方法名和xml中定义的每个statement的id相同

Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同

Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同

(2)配置文件(注意命名空间):

<mapper namespace="pers.zhb.mapper.StudentMapper">
    <select id="findStudentById" parameterType="Integer" resultType="pers.zhb.pojo.Student">
        select * from student where studentno = #{v}
    </select>

注意:xml文件中的namespace与mapper接口的类路径相同

(3)测试:

public class StudentMapperTest {
    public void testMapper() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSEssion帮我生成一个实现类  (给接口)
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        Student student = studentMapper.findStudentById(201811);
        System.out.println(student);
    }
DEBUG [main] - Logging initialized using ‘class org.apache.ibatis.logging.slf4j.Slf4jImpl‘ adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1291286504.
DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]]
DEBUG [main] - ==>  Preparing: select * from student where studentno = ?
DEBUG [main] - ==> Parameters: 201811(Integer)
DEBUG [main] - <==      Total: 1
Student [studentno=201811, sname=zhai, sex=男, birthday=1998-11-11, classno=tx171, point=890, phone=1234567890, email=null]

原文地址:https://www.cnblogs.com/zhai1997/p/12529996.html

时间: 2024-08-29 02:47:45

mybatis(Mybatis与hibernate的不同、原始Dao开发、Mapper动态代理开发)的相关文章

MyBatis开发Dao的原始Dao开发和Mapper动态代理开发

摘自:https://www.cnblogs.com/yichunguo/p/11990961.html 目录 咳咳...初学者看文字(Mapper接口开发四个规范)属实有点费劲,博主我就废了点劲做了如下图,方便理解: 原始Dao开发方式 1. 编写映射文件 3.编写Dao实现类 4.编写Dao测试 Mapper动态代理方式 1.定义Mapper.xml(映射文件) 2.编写UserMapper.xml配置文件内容: 3.编写UserMapper(接口文件) 4.加载UserMapper.xml

Mybatis框架三:DAO层开发、Mapper动态代理开发

这里是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html 接下来做到了简单的增删改查:http://www.cnblogs.com/xuyiqing/p/8601506.html 但是发现代码重复过多等问题 接下来整合并实现DAO开发: 一:原始DAO开发: package dao; import pojo.User; public interface UserDao { public User selectUserById(Integer

MyBatis - Mapper动态代理开发

采用Mapper动态代理方法只需要编写相应的Mapper接口(相当于Dao接口),那么Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接口实现类方法. - Mapper接口开发需要遵循以下规范: ① Mapper.xml文件中的namespace与mapper接口的全类名相同. ② Mapper接口方法名和Mapper.xml中定义的statement的id相同. ③ Mapper接口方法的输入参数类型和mapper.xml中定义的statement的paramet

MyBatis开发Dao层的两种方式(Mapper动态代理方式)

MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上一篇博客中Dao接口实现类方法. Mapper接口开发需要遵循以下规范: (1)Mapper.xml文件中的namespace与mapper接口的类路

02.MyBatis在DAO层开发使用的Mapper动态代理方式

在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体等于Dao接口实现类方法. 1.编写Mapper.xml映射文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP

Mybatis Mapper动态代理方式

目录结构及配置文件与原始dao方法相比更简便 只需一个UserMapper的接口,放在一起的配置文件,配置文件中namespace的地址确定jdk动态代理的对象 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybati

mybatis基础_动态代理开发

1.使用原始dao开发需要编写mapper的接口和实现类 1.编写接口 public interface UserDao { User getUserById(int userId); } 2.编写实现类 public class UserDaoImpl implements UserDao { private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory)

mybatis(一) mapper动态代理

因为dao开发,会每次创建实体类对象,会传入id等固定查询值,存在硬编码问题,所以采用mapper动态代理(不用创建实体类对象,只需要接口,由mapper自动生成) 与之前mybatis(一)步骤一样,但是需要将mapper.xml文件作出修改: namespace:必须是接口类的全路径 (<mapper namespace="">) id:必须是接口的方法名(<select id=""/>) parameterType:必须是接口方法里面的

Mybatis实现Mapper动态代理方式

一.实现原理 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法. Mapper接口开发需要遵循以下规范:                1. Mapper.xml文件中的namespace与mapper接口的类路径相同.                2.  Mapper接口方法名和Mapper.xml中定义的每个statement的id相同