springmvc+spring+mybatis+mysql框架整合(maven模块化分层)03

上面两章说到springmybatis工程集成开发所需要开发jar包,demoutil工程主要是放平时工程中用到的工具类,

demoparent工程,集成了三个工程,便于以后发布测试。


现在我们来建最后一个工程demoproject,此工程是真正的模块开发工程。所有业务都在此工程实现。


这次建的是maven的web工程

pom.xml如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  	<!-- 指定父模块,使用相对路径,父模块中必须有pom.xml文件 -->
	<parent>
		<relativePath>../demoparent</relativePath>
		<groupId>demo</groupId>
		<artifactId>demoparent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<!-- 继承了父模块,所以groupId,version都不再需要 -->
  
<!--   <groupId>demo</groupId> -->
  <artifactId>demoproject</artifactId>
  <packaging>war</packaging>
<!--   <version>0.0.1-SNAPSHOT</version> -->
  <name>demoproject Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
</project>


就是这么简单,按maven分层工程。

接着,我们形如配置基本的xml文件了。


配置文件也就这些几个了。

1.messages.properties这个主要是存发异常代码和信息的文件。

2.SqlMapConfig.xml这个mybatis的,里面什么都没有

<?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>
	<!-- setting配置 -->
	
	<!-- 配置mapper.xml文件 -->
	<!-- <mappers>
	  
	</mappers> -->

</configuration>

3.applicationContext.xml这个是spring容器主要是配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.1.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<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="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
	</bean>

	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="insert*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 切面 -->
	<aop:config proxy-target-class="true">
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* yycg.*.service.impl.*.*(..))" />
	</aop:config>

</beans>

4.applicationContext-base-dao.xml连接mysql的

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.1.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<!-- 配置SqlSessionFactory
从spring和mybatis的整合包中获取
 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 加载数据源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 配置SqlMapConfig.xml -->
    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>

<!-- 使用MapperFactoryBean 生成mapper的代理对象
在mybatis和spring的整合包中
-->
<!-- 
<bean id="sysuserCustomMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="yycg.base.dao.mapper.SysuserCustomerMapper"/>
  <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->

	<!--配置 mapper自动扫描器 bean名称就是mapper类型(首字母小写)
	 配置扫描包路径 ,如果扫描多个包路径,中间使用半角逗号分隔 
		配置SqlSessionFactory -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="demo.**.dao.mapper"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean> 


</beans>


5.applicationContext-base-service.xml属于业务开发的xml,管理对象

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.1.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<!-- serviceImpl对象管理 -->
</beans>


6.springmvc.xml controller层,

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.1.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

	<!-- 组件扫描 扫描所有标记@Controller类,由于使用自动扫描所以action类不用在spring配置文件中配置 -->
	<context:component-scan base-package="demo.**.action" />

	<!-- 处理器映射器和适配器,可以使用mvc注解驱动 -->
	<mvc:annotation-driven />


	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 将jstl的jar包加入工程,默认支持jstl -->
		<!-- 前缀和后缀可以去掉的,为了方便开发才加的 -->
		<property name="prefix" value="/WEB-INF/jsp" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- json转换器 -->
	<bean id="jsonMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	</bean>

	<!-- 统一异常处理类 -->
	<bean id="handlerExceptionResolver" class="yycg.base.process.exception.ExceptionResolverCustom">
		<!-- 注入一个json转换器 -->
		<property name="jsonMessageConverter" ref="jsonMessageConverter" />
	</bean>

	<!-- 文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置上传文件的最大尺寸为5MB -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>


	<!-- 拦截器 -->
	<mvc:interceptors>
		<!-- 多个拦截器,顺序执行 -->
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="自定义拦截器全路径"></bean>
		</mvc:interceptor>
	</mvc:interceptors>



</beans>


就这些了。可以部署下。没出错就ok了。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值