myBatis-08 与Spring的整合
1、pom.xml配置
多引入:
1)spring-test
2)druid
3)mybatis-spring
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.rui</groupId> <artifactId>04QuickExample</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>7</source> <target>7</target> </configuration> </plugin> </plugins> </build> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.0</version> </dependency> <!--jdbcTemplate--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.0</version> </dependency> <!--spring测试--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.8</version> <scope>test</scope> </dependency> <!--阿里的数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.0</version> </dependency> <!--MySQL连接的依赖包--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <!-- https://mybatis.org/mybatis-3/getting-started.html --> <!-- https://mybatis.org/mybatis-3/zh/index.html --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mybatis Spring对接包 单独引入--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--单元测试--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!--日志输出--> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
2、applicationContext.xml配置
将相关对象交给Spring管理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置开启依赖注入的注解解析器,扫包范围--> <context:component-scan base-package="rui.db"/> <!--加载属性配置文件--> <context:property-placeholder location="classpath:jdbc.Properties"/> <!--配置数据源--> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <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="initialSize" value="${jdbc.pool.initialSize}"/> <!--最小空闲连接数--> <property name="minIdle" value="${jdbc.pool.minIdle}"/> <!--最大并发使用数--> <property name="maxActive" value="${jdbc.pool.maxActive}"/> <!--最大等待时间--> <property name="maxWait" value="${jdbc.pool.maxWait}"/> </bean> <!--配置事务管理器,使用jdbc数据源的事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="druidDataSource"/> </bean> <!--开启基于注解的事务管理方式--> <tx:annotation-driven transaction-manager="transactionManager"/> <!--myBatis和Spring整合,定义sessionFactory,配置所用数据源和主配置文件位置--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="druidDataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--自动生成Dao接口的代理实现类,注入到Spring中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="rui.db.Dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
3、调用测试
//和Spring集成之后的测试,通过Spring直接获取到数据访问接口,无需通过SqlSession @Test public void TestMyBatis_04() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ex_CustomerDao dao = (ex_CustomerDao) context.getBean("ex_CustomerDao"); ex_Customer item = dao.findByKey("C001"); System.out.println(item.toString()); System.out.println("测试完成4"); }