龙空技术网

mybatis整合spring

爱学习的小菜鸟 85

前言:

现时同学们对“mybatis与spring整合课件北大青鸟”大体比较看重,姐妹们都想要知道一些“mybatis与spring整合课件北大青鸟”的相关内容。那么小编同时在网摘上网罗了一些有关“mybatis与spring整合课件北大青鸟””的相关文章,希望姐妹们能喜欢,朋友们一起来了解一下吧!

mybatis整合spring

当前spring是最热门的框架之一,很多框架都需要和spring整合,将组件交给spring的IOC容器来管理,下面来带大家来简单地配置一下mybatis和spring的整合

添加依赖

<dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis</artifactId>  <version>3.5.5</version></dependency><dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.48</version></dependency><!-- mybatis整合spring --><dependency>  <groupId>org.mybatis</groupId>  <artifactId>mybatis-spring</artifactId>  <version>1.3.3</version></dependency><!-- spring 相关依赖 --><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-core</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-aop</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-beans</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-aspects</artifactId>  <version>4.3.29.RELEASE</version></dependency><dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-tx</artifactId>  <version>4.3.29.RELEASE</version></dependency>
mybatis配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        ";><configuration>    <settings>        <!-- 开启二级缓存 -->        <setting name="cacheEnabled" value="true"/>        <setting name="logImpl" value="STDOUT_LOGGING"/>        <!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 -->        <setting name="lazyLoadingEnabled" value="true"/>        <!-- 开启时,任一方法的调用都会加载该对象的所有延迟加载属性。 否则,每个延迟加载属性会按需加载 -->        <setting name="aggressiveLazyLoading" value="false"/>    </settings>    <typeAliases>        <!-- typeAlias为某个类起别名 -->        <typeAlias type="com.zhanghe.study.mybatis.model.User" alias="User"/>        <!-- 为某个包下所有类批量起别名            默认值为类名首字母小写         -->        <package name="com.zhanghe.study.mybatis.model"/>    </typeAliases>    <!-- 数据库厂商标识 -->    <databaseIdProvider type="DB_VENDOR">        <!-- name为使用DatabaseMetaData#getDatabaseProductName()返回的厂商名称             value为所设置的别名,使用databaseId时使用别名即可 -->        <property name="MySQL" value="mysql"/>        <property name="Oracle" value="oracle"/>    </databaseIdProvider></configuration>
spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=";       xmlns:xsi=";       xmlns:context=";       xmlns:mybatis=";       xmlns:tx=";       xsi:schemaLocation="         ;>    <context:component-scan base-package="com.zhanghe.study.mybatis"/>    <!-- 引入数据库配置文件 -->    <context:property-placeholder location="jdbc.properties"/>    <!-- 数据源 -->    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="${jdbc.driver}"/>        <property name="url" value="${jdbc.url}"/>        <property name="username" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}"/>    </bean>    <!-- 事务管理器 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="datasource"/>    </bean>    <!-- 创建sqlSessionFactory -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="datasource"/>        <!-- mybatis全局配置文件的位置 也可以不使用mybatis全局配置 所有信息都可以在该bean中使用property来配置 -->        <property name="configLocation" value="mybatis-config1.xml"/>        <!-- mapper映射文件的位置 -->        <property name="mapperLocations" value="classpath*:mapper/**/*.xml"/>    </bean>        <!-- 亲测两种方式都可以 -->    <!-- 方式一:扫描所有的mapper接口,使得接口自动注入 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.zhanghe.study.mybatis.mapper"/>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>    </bean>  <!-- 方式二:扫描所有的mapper接口,使得接口自动注入 --><!--    <mybatis:scan base-package="com.zhanghe.study.mybatis.mapper"/>-->    <!-- 事务管理器 -->    <tx:annotation-driven transaction-manager="transactionManager"/></beans>
单测结果
/** * mybatis、spring整合测试 * @author zh * @date 2021/1/15 16:08 */public class MybatisSpringTest {    private ApplicationContext context;    private SqlSessionFactory sqlSessionFactory;    @Before    public void beanFactory(){        context = new ClassPathXmlApplicationContext("applicationContext.xml");        sqlSessionFactory = context.getBean(SqlSessionFactory.class);    }    @Test    public void testbeanAutoWired(){        UserMapper userMapper = context.getBean(UserMapper.class);        System.out.println(userMapper);        UserService userService = context.getBean(UserService.class);        userService.print();    }    @Test    public void testTwoLevelCache(){        SqlSession session = sqlSessionFactory.openSession();        // mybatis为接口创建代理对象        UserMapper userMapper = session.getMapper(UserMapper.class);        User user = userMapper.selectUser(8);        System.out.println(user);        userMapper.updateUser(user);        session.close();        SqlSession session1 = sqlSessionFactory.openSession();        UserMapper userMapper1 = session1.getMapper(UserMapper.class);        User user1 = userMapper1.selectUser(8);        System.out.println(user1);        System.out.println(user == user1);        session1.close();    }} 

参考地址

标签: #mybatis与spring整合课件北大青鸟