Spring整合MyBatis【方式一】

思路:
SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD
发现,MyBatis最终是通过SqlSessionFactory来操作数据库的,
Spring整合MyBatis 其实就是将 MyBatis的SqlSessionFactory交给Spring。

整合步骤:

1.添加jar包

        mybatis-spring.jar
        spring-tx.jar
        spring-expression.jar
        spring-context-support.jar
        spring-core.jar
        spring-context.jar
        spring-beans.jar
        spring-aop.jar
        spring-web.jar
        commons-logging.jar
        commons-dbcp.jar
        ojdbc.jar
        mybatis.jar
        log4.jar
        commons-pool.jar
        mysql-connector-java.jar

2.创建 类-表

 实体类-数据库表
            注:
                在实现DAO接口的实现类中,还需要继承SqlSessionDaoSupport父类,才能获取SqlSessionFacotry核心对象。

3.创建配置文件

            a.MyBatis配置文件conf.xml
            b.Spring配置文件application.xml

4.MyBatis映射操作:

		 通过XxxMapper.xml将 类、表建立映射关系,
         并在MyBatis的主配置文件conf.xml中加载XxxMapper.xml。

5.获得SqlSessionFacotry核心对象:

	之前:
            在MyBatis中:
                conf.xml -> SqlSessionFacotry

    现在:
            需要通过Spring管理SqlSessionFacotry:
                applicationContext.xml -> SqlSessionFacotry
                (直接在IOC容器中创建SqlSessionFactory对象)

            因此产生SqlSessionFacotry所需要的数据库信息不放在conf.xml,而需要放入spring配置文件中。
            如:
                 <!--在SpringIOC容器中 创建MyBatis和核心类SqlSessionFactory-->
                  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                          <!--获取数据源(连接数据库信息)-->
                          <property name="dataSource" ref="dataSource"></property>
                          <!--加载 mybatis 配置文件-->
                          <property name="configLocation" value="classpath:conf.xml"></property>
                  </bean>

6.使用SqlSessionFacotry对象:

 【Spring创建对象需要在IOC容器中】
        在applicationContext.xml中创建 某DAO实现类时,(即使类中没有属性)必须
        为继承的父类SqlSessionDaoSupport中的sqlSessionFactory属性赋值后,
        在DAO实现类中才能获取SqlSessionFacotry对象。
            如:
                <!--学生DAO实现对象-->
                    <bean id="studentDaoImpl" class="org.neowang.Dao.Impl.StudentDaoImpl">
                        <!--将Spring配置的SqlSessionFactory对象交给mapper(即DAO)-->
                            <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
                    </bean>

7.使用Spring-MyBatis整合后来开发程序。

 目标:通过spring产生mybatis最终操作需要的 动态mapper对象(StudentMapper对象)。

            a.DAO层实现类 继承SqlSessionDaoSupport类
            b.applicationContext.xml中要创建SqlSessionFactory对象,
                  并为DAO层实现类的父类注入属性值后,
                     DAO层实现类中才能调用super.getSqlSession()来获取SqlSession类。

注:

a.在配置数据源,即在配置连接数据库信息时,若是使用外部配置文件获取连接信息时,注意"巨坑"!!!

b.在applicationContext.xml中也可以直接加载XxxMapper.xml文件,不再需要加载conf.xml
如:

 <!--在IOC容器中加载XxxMapper.xml映射文件-->
                  <property name="mapperLocations" value="org/neowang/mapper/*.xml"></property>

使用位置在上述加载conf.xml位置相同(替换加载conf.xml语句)。

c.该方式一整合方式还是很麻烦的。


附注:其他简单方式 点击查看


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值