mybatis與Spring集成,那麼就須要spring對應的jar包
java
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java-version>1.7</java-version> <org.springframework-version>3.1.1.RELEASE</org.springframework-version> <org.aspectj-version>1.6.10</org.aspectj-version> <org.slf4j-version>1.6.6</org.slf4j-version> </properties>
mybatis與Spring集成,須要一箇中間包:mysql
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>
由於咱們使用了數據源,那麼咱們須要添加alibaba的數據源jar包
web
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> </beans>
<!-- 第一步定義數據源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 數據源驅動類可不寫,Druid默認會自動根據URL識別DriverClass --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <!-- 基本屬性 url、user、password --> <!-- ?useUnicode=true&characterEncoding=utf-8 --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/bbs" /> <property name="username" value="userName" /> <property name="password" value="password" /> <!-- 配置監控統計攔截的filters --> <property name="filters" value="stat" /> </bean>
<!-- 配置SqlSessionFactoryBean --> <!-- 2. mybatis 的sqlSession的工廠SqlSessionFactory dataSource: 數據源 typeAliasesPackage:指定實體類的包名,自動將包名下得實體類映射爲簡單的類名 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.mscncn.bbs.core.model"></property> </bean>
<!-- 3. mybatis自動掃描加載Sql映射文件/接口:MapperScannerConfigurer basePackage:指定sql映射文件/接口所在的包,自動掃描(這兒須要注意BindException,若是出現,請看 mybatis異常收集篇) sqlSessionFactory:引用上面定義好得sqlSessionFactory --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mscncn.bbs.mybatis.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
<!-- 4. 事物管理DataSourceTransactionManager dataSource:引用上面定義好得數據源 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
<!-- 5. 使用聲明式事物管理 transaction-manager: 引用上面定義的事物管理器 --> <tx:annotation-driven transaction-manager="txManager" />
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/root-context.xml") public class UserMapperTest { @Autowired private UserMapper userMapper; @Test public void testQuery(){ System.out.println(userMapper.queryArticleByUser(1)); } }