程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> maven-sprigmvc-mybatis配置,mybatismaven配置

maven-sprigmvc-mybatis配置,mybatismaven配置

編輯:JAVA綜合教程

maven-sprigmvc-mybatis配置,mybatismaven配置


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

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved