您的位置:首页 > 编程语言 > Java开发

Mybatis3.2和Spring3.x整合----Myb…

2015-09-09 10:03 555 查看




1. 工程主要文件





有了spring我们就不需要MybatisUtil.java了。SqlSessionFactory和SqlSession直接由spring提供。



2. 主要jar包

spring-core-3.0.0.RELEASE.jar

spring-context-3.0.0.RELEASE.jar

spring-beans-3.0.0.RELEASE.jar

spring-asm-3.0.0.RELEASE.jar

log4j-1.2.14.jar

commons-logging-1.1.1.jar

spring-expression-3.0.0.RELEASE.jar

persistence.jar

spring-jdbc-3.0.0.RELEASE.jar

spring-orm-3.0.0.RELEASE.jar

spring-tx-3.0.0.RELEASE.jar

c3p0-0.9.1.2.jar

commons-dbcp.jar

commons-pool.jar

jotm.jar

xapool.jar

spring-aop-3.0.0.RELEASE.jar

aopalliance-1.0.jar

aspectjweaver-1.6.2.jar

aspectjlib-1.6.2.jar

aspectjrt-1.6.2.jar

cglib-nodep-2.2.jar

spring-aspects-3.0.0.RELEASE.jar

spring-instrument-3.0.0.RELEASE.jar

spring-instrument-tomcat-3.0.0.RELEASE.jar

mybatis-3.2.0.jar

mybatis-spring-1.1.1.jar

mysql-connector-java-3.1.13-bin.jar

log4j-1.2.11.jar

除了后面的四个jar包之外其他所有jar都是通过编译器添加的。



3. 主要文件 applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config
/>

<context:component-scan
base-package="com.edu.mybatis_spring"
/>

<tx:jta-transaction-manager
/>

<!-- 读取数据库连接信息配置文件 -->

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<!--

下面的name属性的值是固定的,可以在spring的源码里看到,

在propertyPlaceholdConfigurer的间接父类

org.springframework.core.io.support.PropertiesLoaderSupport

里有locations的getter和setter方法,location是一个Resource数组。

如果是用其他名字回报异常:

Error creating bean with name

'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0'

defined in class path resource ......

-->

<property
name="locations">

<value>classpath:resources/jdbc.properties</value>

</property>

</bean>

<!-- 配置数据源 -->

<bean id="dataSource"
destroy-method="close"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}"
/>

<property name="username" value="${jdbc.username}"
/>

<property name="password" value="${jdbc.password}"
/>

</bean>

<!-- 配置Mybatis使用的数据源 -->

<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"
/>

<!--
指定mybatis主配置文件的位置并读取使用,可以不配置不是使用这个文件,

就像SSH整合时不需要hibernate.cfg.xml文件一样那些配置都可以在spring配置文件里配置
-->

<!--

该属性用来指定MyBatis的XML配置文件路径,跟Spring整合时,编写MyBatis映射文件的目的无非是配置一下typeAlias、setting之类的

元素。不用在其中指定数据源,或者事务处理方式。就算配置了也会被忽略。因为这些都是使用Spring中的配置。

当然如果你不打算添加typeAlias 之类的设置的话,你连MyBatis的配置文件都不用写,更不用配置这个属性了

但是个人觉得还用着好啊!因为在对应的mapper文件里的 parameterType="User"

属性就不用全限定名("com.edu.mybatis_spring.mapper.UserMapper")了省事啊。

-->

<!--<property name="configLocation"
value="resources/mybatis-config.xml"></property>

-->

<property name="configLocation"
value="resources/mybatis-config.xml"></property>

</bean>

<!-- 用spring的AOP给代码添加事务管理
-->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>

<property name="dataSource" ref="dataSource"
/>

</bean>

<!-- 用spring AOP给service层的方法添加事务管理
-->

<aop:config>

<aop:pointcut id="bussinessService"

expression="execution(public * com.edu.mybatis_spring.dao.*.*(..))"
/>

<aop:advisor
pointcut-ref="bussinessService"

advice-ref="txAdvice"
/>

</aop:config>

<tx:advice id="txAdvice"
transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get*" read-only="true"
/>

<tx:method name="add*"
propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

<!-- 前面的配置除了某些文件的路径外基本是固定的,

下面的配置为mapper映射文件交给spring管理,

并且为每个mapper都注入了SqlSession的实例 -->

<!-- 注册mybatis的mapper方式 1
-->

<bean id="userMapper"

class="org.mybatis.spring.mapper.MapperFactoryBean">

<!--

mapperInterface固定名字,换其他的名字不行,

因为在 org.mybatis.spring.mapper.MapperFactoryBean类里

有 mapperInterface 的getter和setter方法,

而不可能有你自定义其他名字和对应的getter和setter方法,

没有setter方法spring就无法注入,可以在mybatis-spring-1.1.1.jar里看到

-->

<property name="mapperInterface"
value="com.edu.mybatis_spring.mapper.UserMapper"
/>

<property name="sqlSessionFactory"
ref="sqlSessionFactory" />

</bean>

<!--

注册mybatis的mapper方式 2

不指定mapper,使用自动扫描方式注册各个Mapper ,

但是暂时不可用可能是spring和mybatis的版本问题

-->

<!--<bean
class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property
name="basePackage">

<value>com.edu.mybatis_spring.mapper</value>

</property>

<property name="sqlSessionFactory"
ref="sqlSessionFactory" />

</bean>

-->

</beans>



4. Mybatis-config.xml

为了配置简洁还是使用了这个配置文件,当然这些配置都是可以全都放在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>

<typeAliases>

<!-- 导入此包下的所有类,相当于注册。。
-->

<package
name="com.edu.mybatis_spring.model"/>

</typeAliases>

</configuration>



5. UserDaoImpl.xml

package com.edu.mybatis_spring.dao.impl;

import org.springframework.context.ApplicationContext;

import
org.springframework.context.support.ClassPathXmlApplicationContext;

import com.edu.mybatis_spring.mapper.UserMapper;

import com.edu.mybatis_spring.model.User;

import com.edu.mybatis_spring.util.MybatisUtil;

public class UserDaoImpl implements UserMapper {

@Override

public void addUser(User user) {

//UserMapper userMapper = (UserMapper)
MybatisUtil.getBean(UserMapper.class);

ApplicationContext ctx = new

ClassPathXmlApplicationContext("resources/applicationContext.xml");

UserMapper userMapper = ctx.getBean(UserMapper.class);

userMapper.addUser(user);

}

@Override

public User getUserByName(String username) {

return ((UserMapper)
MybatisUtil.getBean(UserMapper.class)).getUserByName(username);

}

}



6. 结果

可以看到这两个方式测试通过了,我们在看数据库。





在数据库中看到了我们测试添加的数据。可以说整个过程是成功的。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: