spring + springmvc+mybatis+maven配置

今天由于项目需要,简单的配置了一下ssm框架,maven配置所需包,可直接粘贴复制

maven依赖

 <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>com.alibaba</groupId>

      <artifactId>fastjson</artifactId>

      <version>1.2.29</version>

    </dependency>

    <dependency>

      <groupId>commons-fileupload</groupId>

      <artifactId>commons-fileupload</artifactId>

      <version>1.3.2</version>

    </dependency>

    <dependency>

      <groupId>org.codehaus.jackson</groupId>

      <artifactId>jackson-mapper-lgpl</artifactId>

      <version>1.9.13</version>

    </dependency>

    <dependency>

      <groupId>jstl</groupId>

      <artifactId>jstl</artifactId>

      <version>1.2</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-core</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-beans</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aspects</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-webmvc</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aop</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-context</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-tx</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-jdbc</artifactId>

      <version>3.2.18.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.mybatis</groupId>

      <artifactId>mybatis</artifactId>

      <version>3.4.2</version>

    </dependency>

    <dependency>

      <groupId>org.mybatis</groupId>

      <artifactId>mybatis-spring</artifactId>

      <version>1.3.1</version>

    </dependency>

    <dependency>

      <groupId>com.alibaba</groupId>

      <artifactId>druid</artifactId>

      <version>1.0.28</version>

    </dependency>

    <dependency>

      <groupId>org.slf4j</groupId>

      <artifactId>slf4j-log4j13</artifactId>

      <version>1.0.1</version>

    </dependency>

    <dependency>

      <groupId>org.apache.struts.xwork</groupId>

      <artifactId>xwork-core</artifactId>

      <version>2.3.31</version>

    </dependency>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.apache.commons</groupId>

      <artifactId>commons-dbcp2</artifactId>

      <version>2.1.1</version>

    </dependency>

    <dependency>

      <groupId>jfreechart</groupId>

      <artifactId>jfreechart</artifactId>

      <version>1.0.0</version>

    </dependency>

    <dependency>

      <groupId>jcommon</groupId>

      <artifactId>jcommon</artifactId>

      <version>0.9.5</version>

    </dependency>

    <dependency>

      <groupId>com.google.code.gson</groupId>

      <artifactId>gson</artifactId>

      <version>2.3.1</version>

    </dependency>

    <dependency>

      <groupId>commons-beanutils</groupId>

      <artifactId>commons-beanutils</artifactId>

      <version>1.4.1</version>

    </dependency>

    <dependency>

      <groupId>commons-codec</groupId>

      <artifactId>commons-codec</artifactId>

      <version>1.5</version>

    </dependency>

    <dependency>

      <groupId>commons-collections</groupId>

      <artifactId>commons-collections</artifactId>

      <version>3.2</version>

    </dependency>

    <dependency>

      <groupId>commons-lang</groupId>

      <artifactId>commons-lang</artifactId>

      <version>2.3</version>

    </dependency>

    <dependency>

      <groupId>commons-lang</groupId>

      <artifactId>commons-lang</artifactId>

      <version>2.2</version>

    </dependency>

  <dependency>

      <groupId>org.apache.httpcomponents</groupId>

      <artifactId>httpclient</artifactId>

      <version>4.1.2</version>

    </dependency>

    <dependency>

      <groupId>postgresql</groupId>

      <artifactId>postgresql</artifactId>

      <version>8.3-603.jdbc4</version>

    </dependency>

    <!– jsp –>

    <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>javax.servlet-api</artifactId>

      <version>3.0.1</version>

      <scope>provided</scope>

    </dependency>

    <dependency>

      <groupId>javax.servlet.jsp</groupId>

      <artifactId>jsp-api</artifactId>

      <version>2.1</version>

      <scope>provided</scope>

    </dependency>

    <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>jstl</artifactId>

      <version>1.2</version>

    </dependency>

    <!– jsp –>

  </dependencies>

由于ide的集成环境不同, intellij IDEA需要加入以下的build,不然他不会解析xml文件,IDEA的一个小bug。elipse没有

<build>

    <finalName>kaiYou</finalName>

      <plugins>

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-compiler-plugin</artifactId>

          <configuration>

            <source>1.6</source>

            <target>1.6</target>

          </configuration>

        </plugin>

      </plugins>

      <!– 解决Maven项目编译后classes文件中没有.xml问题 –>

      <resources>

        <resource>

          <directory>src/main/java</directory>

          <includes>

            <include>**/*.xml</include>

          </includes>

          <filtering>true</filtering>

        </resource>

      </resources>

    </build>

Spring + SpringMVC + mybatis配置,可以直接粘贴复制使用用,只需要修改其中的文件夹路径和数据库语句即可

config.properties

#hibernate.dialect=org.hibernate.dialect.OracleDialect

#driverClassName=oracle.jdbc.driver.OracleDriver

#validationQuery=SELECT 1 FROM DUAL

#jdbc_url=jdbc:oracle:thin:@localhost:1521:orcl

#jdbc_username=root

#jdbc_password=root

#hibernate.dialect=org.hibernate.dialect.MySQLDialect

#driverClassName=com.mysql.jdbc.Driver

validationQuery=SELECT 1

jdbc_url=jdbc:postgresql://192.168.3.101:5432/opengame?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

jdbc_username=postgres

jdbc_password=yjkj8888

#hibernate.dialect=org.hibernate.dialect.SQLServerDialect

#driverClassName=net.sourceforge.jtds.jdbc.Driver

#validationQuery=SELECT 1

#jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/test

#jdbc_username=sa

#jdbc_password=123456

#hibernate.dialect=org.hibernate.dialect.DerbyDialect

#driverClassName=org.apache.derby.jdbc.EmbeddedDriver

#validationQuery=SELECT 1

#jdbc_url=jdbc:derby:test;create=true

#jdbc_username=root

#jdbc_password=root

#jndiName=java:comp/env/dataSourceName

hibernate.hbm2ddl.auto=update

hibernate.show_sql=false

hibernate.format_sql=true

sessionInfoName=sessionInfo

uploadFieldName=filedata

uploadFileMaxSize=20971520

uploadFileExts=txt,rar,zip,doc,docx,xls,xlsx,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid

uploadDirectory=attached

log4j.properties

log4j.rootLogger=DEBUG,Console,File

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c]%m%n

log4j.appender.File=org.apache.log4j.RollingFileAppender 

log4j.appender.File.File=mybatis.log

log4j.appender.File.MaxFileSize=10MB

log4j.appender.File.Threshold=ALL

log4j.appender.File.layout=org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n

spring.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:property-placeholder location=”classpath:config.properties” />

<!– 自动扫描(自动注入) –>

<context:component-scan base-package=”com.yjkj.service” />

</beans>

spring-mvc.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:p=”http://www.springframework.org/schema/p”

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.xsd”>

<!– 自动扫描controller包下的所有类,使其认为spring mvc的控制器 –>

<context:component-scan base-package=”com.yjkj.controller” />

<mvc:annotation-driven />

<!– 让springmvc可以访问静态资源 –>

<mvc:resources mapping=”/resources/**” location=”/resources/” />

<!– 避免IE执行AJAX时,返回JSON出现下载文件 –>

<bean id=”mappingJacksonHttpMessageConverter”

class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”>

<property name=”supportedMediaTypes”>

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

<!– 对模型视图名称的解析,即在模型视图名称添加前后缀 –>

<bean

class=”org.springframework.web.servlet.view.InternalResourceViewResolver”

p:prefix=”/WEB-INF/jsp/” p:suffix=”.jsp” />

<bean id=”multipartResolver”

class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”>

<property name=”defaultEncoding”>

<value>UTF-8</value>

</property>

<property name=”maxUploadSize”>

<value>32505856</value><!– 上传文件大小限制为31M,31*1024*1024 –>

</property>

<property name=”maxInMemorySize”>

<value>4096</value>

</property>

</bean>

<!– 异常处理 –>

<!– <bean id=”exceptionResolver”

class=”org.springframework.web.servlet.handler.SimpleMappingExceptionResolver”>

<property name=”defaultErrorView”>

<value>error</value>

</property>

<property name=”exceptionMappings”>

<props>

<prop key=”java.lang.NullPointerException”>nullException</prop>

<prop key=”java.lang.RuntimeException”>userException</prop>

<prop key=”org.springframework.web.multipart.MaxUploadSizeExceededException”>uploadException</prop>

</props>

</property>

</bean> –>

</beans>

spring-mybatis.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: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-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”>

<!– JNDI方式配置数据源 –>

<!– <bean id=”dataSource” class=”org.springframework.jndi.JndiObjectFactoryBean”> 

<property name=”jndiName” value=”${jndiName}”></property> </bean> –>

<!– 配置数据源 –>

<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” />

<!– 自动扫描entity目录, 省掉Configuration.xml里的手工配置 –>

<property name=”mapperLocations” value=”classpath:com/yjkj/mapping/*.xml” />

<!– 自动带上包的前缀,由于mapper是自动生成的,所以直接省略了 –>

<!–<property name=”typeAliasesPackage” value=”com.yjkj.model” />–>

</bean>

<bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>

<property name=”basePackage” value=”com.yjkj.dao” />

<property name=”sqlSessionFactoryBeanName” value=”sqlSessionFactory” />

</bean>

<!– 配置事务管理器 –>

<bean id=”transactionManager”

class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<property name=”dataSource” ref=”dataSource” />

</bean>

<!– 注解方式配置事物 –>

<!– <tx:annotation-driven transaction-manager=”transactionManager” /> –>

<!– 拦截器方式配置事物 –>

<tx:advice id=”transactionAdvice” transaction-manager=”transactionManager”>

<tx:attributes>

<tx:method name=”add*” propagation=”REQUIRED” />

<tx:method name=”append*” propagation=”REQUIRED” />

<tx:method name=”insert*” propagation=”REQUIRED” />

<tx:method name=”save*” propagation=”REQUIRED” />

<tx:method name=”update*” propagation=”REQUIRED” />

<tx:method name=”modify*” propagation=”REQUIRED” />

<tx:method name=”edit*” propagation=”REQUIRED” />

<tx:method name=”delete*” propagation=”REQUIRED” />

<tx:method name=”remove*” propagation=”REQUIRED” />

<tx:method name=”repair” propagation=”REQUIRED” />

<tx:method name=”delAndRepair” propagation=”REQUIRED” />

<tx:method name=”get*” propagation=”SUPPORTS” />

<!– <tx:method name=”find*” propagation=”SUPPORTS” /> –>

<tx:method name=”load*” propagation=”SUPPORTS” />

<tx:method name=”search*” propagation=”SUPPORTS” />

<tx:method name=”datagrid*” propagation=”SUPPORTS” />

<tx:method name=”*” propagation=”SUPPORTS” />

</tx:attributes>

</tx:advice>

<aop:config>

<aop:pointcut id=”transactionPointcut”

expression=”execution(* com.yjkj.service..*Impl.*(..))” />

<aop:advisor pointcut-ref=”transactionPointcut”

advice-ref=”transactionAdvice” />

</aop:config>

<!– 配置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.yjkj.service.*</value>

</list>

</property>

</bean>

<aop:config>

<aop:advisor advice-ref=”druid-stat-interceptor”

pointcut-ref=”druid-stat-pointcut” />

</aop:config>

</beans>

    原文作者:胡GaQue
    原文地址: https://www.jianshu.com/p/4722b1c26842
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞