maven-sprigmvc-mybatis配置

时间:2023-03-09 03:08:21
maven-sprigmvc-mybatis配置
pom.xml配置
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zx</groupId>
<artifactId>qyxx</artifactId>
<version>1.0.0-snapshot</version>
</parent>
<artifactId>qyxx-web</artifactId>
<name>qyxx-web</name>
<url>http://maven.apache.org</url>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <dependency>
<groupId>com.zx</groupId>
<artifactId>qyxx-core</artifactId>
<version>${project.version}</version>
</dependency>
<!-- sitemesh开始 -->
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>
<!-- sitemesh结束 --> <!-- junit -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- QQ快捷登陆jar包 -->
<!-- <dependency>
<groupId>com.tencent</groupId>
<artifactId>qq.connect</artifactId>
<version>2.0</version>
</dependency> --> </dependencies> <profiles>
<!-- 开发环境 -->
<profile>
<id>local</id>
<properties>
<env.type>local</env.type>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境 -->
<profile>
<id>dev</id>
<properties>
<env.type>dev</env.type>
</properties>
</profile>
<!-- 生产正式环境 -->
<profile>
<id>pro</id>
<properties>
<env.type>pro</env.type>
</properties>
</profile>
</profiles>
<build>
<finalName>qyxx</finalName>
<!-- 使用指定的配置 -->
<filters>
<filter>src/main/resources/config/${env.type}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/*.properties</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- compiler插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 自动部署 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<url>http://10.60.10.63/manager/text</url>
<username>tomcat</username>
<password>zxkj8888</password>
<update>true</update>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>

applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" 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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="true"> <description>Spring公共配置 </description> <util:properties id="common" location="classpath:common.properties" />
<!-- <util:properties id="qqconfig" location="classpath:qqconnectconfig.properties" /> --> <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.zx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 支持异步方法执行 -->
<task:annotation-driven />
<!-- 缓存配置 -->
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="default" />
</set>
</property>
</bean>
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
<property name="driverClassName" value="#{common['jdbc.driver']}" />
<!-- 基本属性 url、user、password -->
<property name="url" value="#{common['jdbc.url']}" />
<property name="username" value="#{common['jdbc.username']}" />
<property name="password" value="#{common['jdbc.password']}" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="#{common['jdbc.minIdle']}" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="#{common['jdbc.minIdle']}" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="#{common['jdbc.maxActive']}" />
<!-- 获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="select 'x'" />
<!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 以下正常要注释掉,排查问题用 -->
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle:true mysql:false) -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
</bean> <!--事务管理DataSourceTransactionManager -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" /> <!-- Spring iBatis SqlMapClient -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations">
<value>classpath:mapper/**/*Mapper.xml</value>
</property>
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean> <!-- 自动扫描mapper配置和dao映射,无需写mybatis-config.xml -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zx.qyxx.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
</bean> <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring
bean对象 -->
<bean id="springContextHolder" class="com.zx.qyxx.utils.SpringContextHolder"
lazy-init="false" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
</bean>
</beans>

spring-mvc.xml配置<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance    xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用CoC,惯例优先原则(convention over configuration)的方式来配置SpringMVC可以帮我们声明Controller的时候省下很多功夫。
只要我们的Controller是以XXXController的方式命名的话就可以使用到CoC带来的好处。 例如,对于普通的Controller(非MultiActionController),CoC帮我们做以下的映射:
HomeController映射到/home*请求URL -->
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- 使用annocation创建Controller的bean -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 自动扫描 -->
<context:component-scan base-package="com.zx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan> <!-- 注解映射的支持 默认 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <!-- 对静态资源文件的访问方案 默认 -->
<mvc:default-servlet-handler />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login/**" />
<mvc:exclude-mapping path="/isLogin" />
<mvc:exclude-mapping path="/logOut" />
<mvc:exclude-mapping path="/forget" />
<mvc:exclude-mapping path="/verify/**" />
<mvc:exclude-mapping path="/callBack/**" /> <!-- <mvc:exclude-mapping path="/oauth" /> -->
<bean class="com.zx.qyxx.intercepter.SignInterceptor" />
</mvc:interceptor>
</mvc:interceptors> <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10485760" />
<property name="maxInMemorySize" value="4096" />
<property name="resolveLazily" value="true" />
</bean> <!-- 定义首页 -->
<mvc:view-controller path="/" view-name="redirect:/index.html" /> <!-- 定义JSP -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- end --> <!-- 将Controller抛出的异常转到特定View, 保持SiteMesh的装饰效果 -->
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings"> <props> <prop key="java.lang.Throwable">error/500</prop>
</props> </property> </bean> -->
<!-- 支持跨域 -->
<mvc:cors>
<mvc:mapping path="/**" allowed-origins="*"
allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
allow-credentials="true" />
</mvc:cors> <!-- 0/86400 * * * * ?定时任务配置 0 0 1 * * ? -->
    <task:executor id="executor" pool-size="1" />     <task:scheduler id="scheduler" pool-size="1" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
      <task:scheduled-tasks>
      <task:scheduled ref="listenerService" method="logout" cron="0 0 0/2 * * ?" />
      <!-- <task:scheduled ref="listenerService" method="buildIp" cron="0 0/5 * * * ?" />
      <task:scheduled ref="listenerService" method="getIps" cron="0 0 0/1 * * ?" /> -->
   </task:scheduled-tasks>
</beans>

commen.properties配置

#数据库信息
#jdbc.driver=${jdbc.driver}
jdbc.driver=net.sf.log4jdbc.DriverSpy
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.minIdle=10
jdbc.maxActive=30 #微信开放平台
AppID=${AppID}
AppSecret=${AppSecret}
WechatRedirectUrl=${WechatRedirectUrl} #QQ互联
QQAppID=${QQAppID}
QQAppKey=${QQAppKey}
QQRedirectUrl=${QQRedirectUrl}
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
getOpenIDURL = https://graph.qq.com/oauth2.0/me
#Redis配置信息
redis.Host=10.211.55.3
redis.Port=6379
redis.Password=hujinhu

log4j.properties配置

# Output pattern : date [thread] priority category - message
#log4j.rootLogger=warn, Console, RollingFile,Cat
log4j.rootLogger=warn, Console, RollingFile,ErrorFile #Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n #RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=logs/log.log
log4j.appender.RollingFile.DatePattern=yyyy-MM-dd-HH'.log'
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.RollingFile.Threshold=INFO #ErrorFile
log4j.appender.ErrorFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorFile.File=logs/error.log
log4j.appender.ErrorFile.DatePattern=yyyy-MM-dd-HH'.log'
log4j.appender.ErrorFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorFile.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.ErrorFile.Threshold=ERROR #Project defalult level
log4j.logger.com.zx=INFO #log4jdbc
#log4j.logger.jdbc.sqltiming=INFO

config路径下的local.properties配置

#线下地址
#数据库信息
jdbc.url=jdbc:log4jdbc:mysql://xx.xx.xx.xx:xxxx/enterprise_info?characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true
jdbc.username=xxxx
jdbc.password=xxxx #微信开放平台
AppID=wx3e90b218c6d22e17
AppSecret=xxxxxxxx
WechatRedirectUrl=http://xxxxx/callBack/wechat #QQ互联
QQAppID=101357725
QQAppKey=xxxxxxxxx
QQRedirectUrl=http://xxxxxxxx/callBack/qq