先看項目目錄結構java
很清爽了mysql
最重要的Spring的核心配置文件,看一下spring
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:property-placeholder location="classpath:db.properties" /> <!-- 數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="com.itheima.mybatis.pojo"></property> </bean> <!-- 單個動態代理用下面方法 --> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> --> <!-- Mapper動態代理批量掃面的方式,掃描後會每一個代理對象的id就是接口類名(首字母小寫) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注意:從上下文找到sqlSessionFactory對象 --> <property name="basePackage" value="com.itheima.mybatis.mapper"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
只要數據源和動態代理及事務就能夠了sql
動態代理對象的名字是默認的類名首字母小寫數據庫
測試類看一下使用方法apache
package com.itheima.mybatis.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.itheima.mybatis.mapper.UserMapper; public class TestUserMapper { private ApplicationContext ctx; @Test public void testQueryUserById() { this.ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = (UserMapper) this.ctx.getBean("userMapper"); System.out.println(userMapper.queryUserById(1)); } }
使用代理對象就能夠了mybatis
最後,使用的maven座標app
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>com.itheima.mybatis</groupId> <artifactId>mybatis02_spring</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis02_spring</name> <description>mybatis02_spring</description> <!-- mybatis單獨玩的時候有的包應該有 --> <!-- spring相關包 --> <!-- mybatis與spring整合包 --> <!-- 數據庫鏈接池jar包 --> <properties> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <mysql.version>5.1.46</mysql.version> <mybatis.version>3.2.7</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <spring.version>4.1.3.RELEASE</spring.version> </properties> <dependencies> <!-- junit測試包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- log4j日誌包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--mysql依賴包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--mybatis依賴包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--Mybatis+Spring整合 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- 數據庫鏈接池 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- Spring:依賴注入IoC與DI的最基本實現 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring:Bean工廠與bean的裝配 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring:spring的context上下文即IoC容器 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring:jdbc的支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring:test的支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- spring end --> </dependencies> </project>