SpringMVC+Mybatis+Maven框架配置文件介绍

果学网 -专注IT在线www.prismcollege.com

主要为几个配置文件:spring.xml,spring-bean.xml,spring-mvc.xml,mybatis-config.xml,generatorConfig.xml

spring.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.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-3.0.xsd
     	http://www.springframework.org/schema/tx 
      	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     	http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<import resource="spring-bean.xml" />
	<!-- 引入数据库属性文件 -->
	<context:property-placeholder location="classpath:properties/db.properties" />
	<!-- 配置datasource数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
		<!-- <property name="poolPreparedStatements" value="true" /> <property 
			name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
		<!-- <property name="validationQuery" value="${validationQuery}" /> -->
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />
		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />
		<!-- 监控数据库 -->
		<!-- <property name="filters" value="stat" /> -->
		<property name="filters" value="mergeStat" />
	</bean>

	<!-- 配置mybatis -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
	</bean>

	<!-- 配置事物管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- 配置druid监控spring jdbc -->
	<!-- <bean id="druid-stat-interceptor" -->
	<!-- class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean> -->
	<!-- <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" -->
	<!-- scope="prototype"> -->
	<!-- <property name="patterns"> -->
	<!-- <list> -->
	<!-- <value>com.fafa.service.*</value> -->
	<!-- </list> -->
	<!-- </property> -->
	<!-- </bean> -->
	<!-- <aop:config> -->
	<!-- <aop:advisor advice-ref="druid-stat-interceptor" -->
	<!-- pointcut-ref="druid-stat-pointcut" /> -->
	<!-- </aop:config> -->
</beans>


根据配置,事物管理只需要在service层的方法前加上注解

@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = { RuntimeException.class, Exception.class })

即可利用spring的事物管理进行完整性事物控制,包括回滚、重做等。


spring-baen.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"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 自动扫描(自动注入) -->
	<context:component-scan base-package="com.fafa.service" />
	
	<!-- 扫描mapper接口,自动生成代理类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xxxx.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
</beans>

spring-mvc的文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- 自动扫描controller包下所有的类 -->
	<context:component-scan base-package="com.xxxx.controller" />

	<!-- 开启注解 -->
	<mvc:annotation-driven />


	<!-- springMVC注解映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				<bean
					class="org.springframework.http.converter.ResourceHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
				<bean
					class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
			</list>
		</property>
	</bean>
<span style="white-space:pre">	</span><!-- Spring-mvc.xml 添加文件上载支持 
<span style="white-space:pre">	</span>如果使用这个组件必须导入fileupload包!
<span style="white-space:pre">	</span>ID 必须为 multipartResolver-->
<span style="white-space:pre">	</span><bean id="multipartResolver"
<span style="white-space:pre">		</span>class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<span style="white-space:pre">		</span><!-- 限制上载文件的大小是 102400 Byte
<span style="white-space:pre">		</span>也就是 100K Byte-->
<span style="white-space:pre">		</span><property name="maxUploadSize"
<span style="white-space:pre">			</span>value="102400"/>
<span style="white-space:pre">		</span><!-- 将异常推迟到 控制器中处理 -->
<span style="white-space:pre">		</span><property name="resolveLazily"
<span style="white-space:pre">			</span>value="true"/>
<span style="white-space:pre">	</span></bean>
	<!-- 前缀与后缀设置 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 静态资源访问 -->
	<mvc:resources location="/fafa/" mapping="/fafa/**" />
	<mvc:resources location="/css/" mapping="/css/**" />
	<mvc:resources location="/js/" mapping="/js/**" />
	<mvc:resources location="/images/" mapping="/images/**" />
<span style="white-space:pre">	</span><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
<span style="white-space:pre">	</span><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> 
<span style="white-space:pre">	</span><mvc:default-servlet-handler />  
</beans>

mybatis-config.xml文件如下:

<?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> -->
		<!-- <setting name="cacheEnabled" value="true" /> -->
		<!-- <setting name="lazyLoadingEnabled" value="true" /> -->
		<!-- <setting name="multipleResultSetsEnabled" value="true" /> -->
		<!-- <setting name="useColumnLabel" value="true" /> -->
		<!-- <setting name="useGeneratedKeys" value="false" /> -->
		<!-- <setting name="enhancementEnabled" value="false" /> -->
		<!-- <setting name="defaultExecutorType" value="SIMPLE" /> -->
<!-- 	</settings> -->
	<mappers>
		<mapper resource="com/xxxx/mapper/AdvertisementMapper.xml" />
		<mapper resource="com/xxxx/mapper/RecordShareMapper.xml" />
		<mapper resource="com/xxxx/mapper/SchoolMapper.xml" />

		<mapper resource="com/xxxx/mapper/VersionMapper.xml" />
		<mapper resource="com/xxxx/mapper/WorkExperienceMapper.xml" />
		<!-- <mapper resource="com/xxxx/mapper/*Mapper.xml" /> -->
	</mappers>
</configuration>

generatorConfig.xml(暂未使用)文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
	<classPathEntry
		location="D:\warehouse\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />
	<context id="context1"> 
		<!-- 注释 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
			<property name="suppressDate" value="true" />
		</commentGenerator>
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/xxxxxxxx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull"
			userId="root" password="xxxxx" />
		<javaModelGenerator targetPackage="com.xxxx.entity"
			targetProject="xxxxxxxx" />
		<sqlMapGenerator targetPackage="com.xxxx.mapper"
			targetProject="xxxxxxxxx" />
		<javaClientGenerator targetPackage="com.xxxx.mapper"
			targetProject="xxxxxxxxx" type="XMLMAPPER" />

		<table tableName="XXXXX_XXXXX" enableCountByExample="false"
			enableDeleteByExample="false" enableDeleteByPrimaryKey="false"
			enableSelectByExample="false" enableUpdateByExample="false"></table>



	</context>
</generatorConfiguration>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值