Spring、Springmvc、Mybatis整合

首先所有的XML配置文件如下:

mybatis-config.xml:mybatis全局配置,稍后交给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>
    <!-- 配置全局属性 -->
    <settings>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />
        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />
        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
       <!--  <setting name="mapUnderscoreToCamelCase" value="true" /> -->
    </settings>
</configuration>
spring-aspect.xml :Spring的代理模式

<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="   
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
	<!-- 开启spring的注解扫描 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<context:component-scan base-package="cn.pzh.bank.aspect"></context:component-scan>	
	<!-- 用注解配置AOP -->
	<!-- 1 -->
	<!-- 2.记得开启spring的注解扫描 -->
	
	<!-- 面向切面(AOP) -->
	
<!-- 	<bean id="serviceAspect" class="aop.MyAspect"></bean>
	用XML方式实现AOP
	<aop:config >
		<aop:pointcut expression="execution(public * *(..))" id="pointcut"/>
		<aop:aspect ref="serviceAspect">
			<aop:before method="beforeAdvice" pointcut-ref="pointcut"/>
			<aop:after-returning method="afterAdvice" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config> -->
	
</beans>  


spring-dao.xml:自动生成dao层的实现类,注入spring的Bean池中

<?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"
    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">
    <!-- 配置整合mybatis过程 -->
    <!-- 1加载外部文件 -->
    <context:property-placeholder location="classpath:db.properties" />
	
    <!-- 2配置数据源,class代表哪个池(Spring支持默认池,c3p0,dbcp,durid) -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${mysql.driver}" />
        <property name="jdbcUrl" value="${mysql.url}" />
        <property name="user" value="${mysql.username}" />
        <property name="password" value="${mysql.password}" />
        <property name="maxPoolSize" value="100" />
        <property name="minPoolSize" value="10" />
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false" />
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000" />
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2" />
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="cn.pzh.bank.entity" />
        <!-- 扫描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocations" value="classpath:cn/pzh/bank/mapper/*.xml" />
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,实现类对象注入到spring容器中,默认实现类对象名字类名首字母小写 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="cn.pzh.bank.dao" />
    </bean>
</beans>
spring-service.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	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
          http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
">
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	
	
	<!-- 扫描service包下所有使用注解的类型 -->
    <context:component-scan base-package="cn.pzh.bank.service"></context:component-scan>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置基于注解的声明式事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	

</beans>
spring-web.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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	<!-- 处理器(控制层)注解手法 -->
	<!-- 支持处理静态文件标签 -->
	<mvc:default-servlet-handler />
	<!-- 支持注解手法标签 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="cn.pzh.bank.web"></context:component-scan>
	<!-- HandlerMapping:处理器映射器 --><!-- 默认加载 -->
	<bean
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
	<!-- HandlerAdapter:处理器适配器 -->

	<!-- Handler:处理器(XML方式实现) -->
	<bean id="/first.action" class="cn.pzh.bank.web.FirstControl"></bean>
	<bean id="/second.action" class="cn.pzh.bank.web.SecondControl"></bean>
	<!-- ViewResolver:视图解析器 -->
<!-- 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  		<property name="prefix" value="/WEB-INF/jsp/"></property>
  		<property name="suffix" value=".jsp"></property>
	</bean> -->



</beans>
web.xml :web工程自带的配置文件,加载Spring的XML配置,配置springmvc前端控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>springmvc</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- springmvc前端控制器 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>  <!-- 随便取个名字 -->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 这个servlet的类路径 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 加载spring配置的xml文件 -->
			<param-value>classpath:spring-*.xml</param-value>
		</init-param>
		<!-- 启动服务器时就初始化servlet -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>  <!-- 上面那个名字 -->
		<!-- servlet的/和/*的区别 -->
		<url-pattern>/</url-pattern>	   <!-- 映射路径 -->
	</servlet-mapping>
</web-app>
具体每个类的注解手法参照之前的博客!!!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值