首页 > 其他分享 >Mybatis整合Spring

Mybatis整合Spring

时间:2022-10-30 17:14:50浏览次数:41  
标签:jdbc Spring jar mybatis 整合 spring Mybatis 2.2 log4j

一、整合思路。

1、SqlSessionFactory对象交给Spring容器管理。

2、传统dao的开发方式中应该从Spring容器中获取sqlSession对象。

3、Mapper代理形式中,应该从Spring容器中直接获得mapper的代理对象。

4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

 

二、需要整合的jar包。

1、spring的jar包。

2、mybatis的jar包。

3、spring+mybatis的整合包。

4、mysql的数据库驱动jar包。

5、数据库连接池的jar包。

 

 三、整合步骤。

1、创建工程,导入jar包。

2、加入配置文件。

  2.1 sqlMapConfig配置文件。

    2.1.1 数据库连接及连接池。(放到applicationContext.xml配置文件中)

    2.1.2 事务管理。

    2.1.3 sqlSessionFactory对象,配置到spring容器中。

    2.1.4 mapper代理对象或者是dao实现类配置到spring容器中。

    配置代码如下:

<?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>
    <!-- 1.设置别名 -->
    <typeAliases>
        <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
        <package name="com.sfwu15.pojo"/>
    </typeAliases>
    
    <!-- 加载Mapper -->
    <mappers>
        <!-- 加载User.xml配置文件 -->
        <mapper resource="SqlMap/User.xml"/>
    </mappers>
</configuration>

  2.2 applicationContext.xml的配置文件。

  2.2.1 因为SqlSessionFactoryBean属于mybatis-spring这个jar包,对于spring来说,mybatis是另外一个架构,需要整合jar包,所以在项目中加入mybatis-spring-1.2.2.jar的源码。

  2.2.2 加载db.properties配置文件。

  2.2.3 配置数据库连接池。

  2.2.4 配置SqlSessionFactory对象。 

  2.2.5 配置Dao。

  2.2.6 配置Mapper代理。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">

    <!-- 加载数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 数据库连接池 -->
    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="clone">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>
    
    <!-- 配置SqlSessionFactory对象 -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置sqlMapConfig配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 配置Dao -->
    <bean name="userDao" class="com.sfwu15.dao.impl.UserDaoImpl">
        <!-- 配置sqlSessionFactory -->
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    <!-- Mapper代理的方式开发方式一,配置Mapper代理对象 -->
    <!-- <bean name="userMappe" class="org.mybatis.spring.mapper.MapperFactoryBean">
        配置Mapper接口
        <property name="mapperInterface" value="com.sfwu15.mapper.UserMapper"/>
        配置sqlSessionFactory
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean> -->
    
    <!-- Mapper代理的方式开发方式二,扫描包方式配置代理 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置Mapper接口 -->
        <property name="basePackage" value="com.sfwu15.mappercom.sfwu15.mapper"/>
    </bean>
</beans>

   2.3 db.properties配置如下。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

  2.4 log4j.properties配置如下:

# Global logging configuration
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

搭建完成后的文件如下图所示:

db.properties

标签:jdbc,Spring,jar,mybatis,整合,spring,Mybatis,2.2,log4j
From: https://www.cnblogs.com/sfwu/p/16841524.html

相关文章

  • springboot源码剖析(四) 上下文启动
    概念      springboot在启动流程中最重要的事情便是加载启动spring组件,比如加载IOC容器,启动springMVC等。实现原理  使用AnnotationConfig......
  • springboot源码剖析(三) 环境配置
    概念      springboot在启动流程中会把环境配置都加载进应用当中。实现原理     使用环境配置器用来加载和解析所有配置文件。配置文件......
  • springboot2
    学习文档:https://blog.csdn.net/ttxbgjj/article/details/122881011---尚硅谷课程笔记https://www.yuque.com/atguigu/springboot  ---尚硅谷官网文档学习视频:https:......
  • springboot源码剖析(二) 事件发布
    概念      springboot在启动流程中会发布一些事件通知依赖组件进行主动更新。      原理是springboot使用到的一种设计模式:观察者模式。优......
  • java spring项目中使用设计模式和函数式编程的思想去除业务逻辑中的if else判断
    如果你开发项目时对项目之后的发展很清晰但仍陷入了为什么要用设计模式替换ifelse的疑问时就说明你项目的体量不需要用设计模式答案只在问题提出之后有意义策略和状......
  • Spring源码-context:component-scan解析
    调用AbstractApplicationContext.refresh()刷新容器,会调用obtainFreshBeanFactory()获取ConfigurableListableBeanFactory。会去调用loadBeanDefinitions()方法解析xml文件......
  • springboot源码剖析(一) 总体启动流程
    前言  之前阅读STL(C++)源码的时候,有所感悟:大佬的代码总会实践到部分设计模式、新型语法特性,亦或是精巧的算法和数据结构。     读源码的技巧:大......
  • 找到多个名为spring_web的片段。这是不合法的相对排序。有关详细信息,请参阅Servlet规
    问题描述:解决办法:1:检查pom.xml中是否包含多个spring-web字段;2:删除掉多余的spring-web.jar,保留一个即可;......
  • Spring事务回滚的两种方法
    ##方法一1.使用@Transaction来配置自动回滚,可以配置在类上,也可以配置在方法上(作用域不同),但对final或private修饰的方法无效,且该类必须是受spring所管控的,也就是被已经被注......
  • Spring源码分析之AOP
    AOP是什么面向切面的程序设计(Aspect-orientedprogramming,AOP,又译作面向方面的程序设计、剖面导向程序设计),是计算机科学中的一种程序设计思想,旨在将横切关注点与业务主体进......