1. 要搭建的项目的项目结构如下(使用的框架为:Spring、SpingMVC、MyBatis):

2.pom.xml中的配置如下(注意,本工程分为几个小的子工程,另外两个工程最终是jar包):

其中pom.xml中的内容如下,其中${ip}为ip地址:

<?xml version="1.0" encoding="UTF-8"?>

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

<groupId>xxx.xxx.xxx</groupId>

<artifactId>xxx</artifactId>

<packaging>pom</packaging>

<version>1.0-SNAPSHOT</version>

<name>App tpl</name>

<modules>

<module>xxx-api</module>

<module>xxx-impl</module>

<module>xxx-webapp</module>

</modules>

<scm>

<connection>scm:svn:http://${ip}/svn/xxx/trunk</connection>

<developerConnection>scm:svn:http://${ip}/svn/xxx/trunk</developerConnection>

<url>http://${ip}/svn/xxx/trunk</url>

</scm>

</project>

其中xxx-xxx-webapp中的pom配置如下:

其中maven项目的目录结构:

xxx-xxx-webapp

--src

--main

---java

---resources

---webapp

--test

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

<groupId>xxx.xxx.xxx</groupId>

<artifactId>xxx-xxx-webapp</artifactId>

<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

<name>xxx-xxx-webapp</name>

<url>http://maven.apache.org</url>

<build>

<plugins>

<plugin>

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

<version>2.0.2</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

<fork>true</fork>

<meminitial>128m</meminitial>

<maxmem>512m</maxmem>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<additionalProjectnatures>

<projectnature>

org.springframework.ide.eclipse.core.springnature

</projectnature>

</additionalProjectnatures>

<additionalBuildcommands>

<buildcommand>

org.springframework.ide.eclipse.core.springbuilder

</buildcommand>

</additionalBuildcommands>

<downloadSources>false</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

<wtpversion>1.5</wtpversion>

</configuration>

</plugin>

<!-- 要加上下面的一句,否则执行:mvn package -Dmaven.test.skip=true的时候会报错 -->

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>2.1.1</version>

</plugin>

</plugins>

</build>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spring.version>4.0.3.RELEASE</spring.version>

</properties>

<dependencies>

<!-- Spring -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-oxm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.2</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.2</version>

</dependency>

<!-- Test -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.7</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>aopalliance</groupId>

<artifactId>aopalliance</artifactId>

<version>1.0</version>

</dependency>

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.3</version>

</dependency>

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.3</version>

</dependency>

<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>1.5.4</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc14</artifactId>

<version>10.2.0.3.0</version>

</dependency>

<dependency>

<groupId>taglibs</groupId>

<artifactId>standard</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.6</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.1</version>

</dependency>

<dependency>

<groupId>net.sf.ezmorph</groupId>

<artifactId>ezmorph</artifactId>

<version>1.0.6</version>

</dependency>

<dependency>

<groupId>commons-httpclient</groupId>

<artifactId>commons-httpclient</artifactId>

<version>3.1</version>

</dependency>

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.3</version>

<classifier>jdk15</classifier>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.7</version>

</dependency>

<dependency>

<groupId>org.jasig.cas.client</groupId>

<artifactId>cas-client-core</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>org.jasig.cas.client</groupId>

<artifactId>ucap-cas-client</artifactId>

<version>3.2.1</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.8</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>transaction</artifactId>

<version>3.1.1</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>web.servlet</artifactId>

<version>3.1.1</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version>

<scope>provided</scope>

</dependency>

<!--CMS引用json -->

<dependency>

<groupId>org.json</groupId>

<artifactId>org.json</artifactId>

<version>chargebee-1.0</version>

</dependency>

<!-- json -->

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.4</version>

<classifier>jdk15</classifier>

</dependency>

<dependency>

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

<artifactId>jackson-core-lgpl</artifactId>

<version>1.9.13</version>

</dependency>

<dependency>

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

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

<version>1.9.13</version>

</dependency>

<dependency>

<groupId>org.json</groupId>

<artifactId>json</artifactId>

<version>20140107</version>

</dependency>

<dependency>

<groupId>com.ucap.webservice</groupId>

<artifactId>uservices-api</artifactId>

<version>1.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>org.mybatis.caches</groupId>

<artifactId>mybatis-ehcache</artifactId>

<version>1.0.2</version>

</dependency>

<!-- 提交兼容MySQL的依赖 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.38</version>

</dependency>

<!-- 阿里巴巴的druid数据源配置 -->
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.7</version>
</dependency>

<!-- 整合redis所需的jar包 -->
        <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <version>1.6.0.RELEASE</version>
</dependency>
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.7.3</version>
</dependency>

</dependencies>

</project>

其中xxx-api工程的pom文件如下:

工程结构:

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

<parent>

<!—和第一个pom文件中的pom.xml相同-->

<groupId>xxx.xxx.xxx</groupId>

<!—和第一个pom文件中的pom.xml相同à

<artifactId>xxx</artifactId>

<version>1.0-SNAPSHOT</version>

</parent>

<groupId>xxx.xxx.xxx</groupId>

<artifactId>xxx-xxx-api</artifactId>

<packaging>jar</packaging>

<name>App tpl Api</name>

<version>1.0-SNAPSHOT</version>

<build>

<plugins>

<plugin>

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

<version>2.0.2</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

<fork>true</fork>

<meminitial>128m</meminitial>

<maxmem>512m</maxmem>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<additionalProjectnatures>

<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

</additionalProjectnatures>

<additionalBuildcommands>

<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

</additionalBuildcommands>

<downloadSources>false</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

<wtpversion>1.5</wtpversion>

</configuration>

</plugin>

</plugins>

</build>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<!—这里放置的是工程中的jar依赖-->

</dependencies>

</project>

其中xxx-xxx.impl工程中的pom文件如下:

工程结构如下:

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

<parent>

<groupId>xxx.xxx.xxx</groupId>

<artifactId>xxx</artifactId>

<version>1.0-SNAPSHOT</version>

</parent>

<groupId>xxx.xxx.xxx</groupId>

<artifactId>xxx-xxx-xxx</artifactId>

<packaging>jar</packaging>

<name>App tpl Api</name>

<version>1.0-SNAPSHOT</version>

<build>

<plugins>

<plugin>

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

<version>2.0.2</version>

<configuration>

<source>1.5</source>

<target>1.5</target>

<fork>true</fork>

<meminitial>128m</meminitial>

<maxmem>512m</maxmem>

<encoding>UTF-8</encoding>

</configuration>

</plugin>

<plugin>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.5.1</version>

<configuration>

<additionalProjectnatures>

<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>

</additionalProjectnatures>

<additionalBuildcommands>

<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>

</additionalBuildcommands>

<downloadSources>false</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

<wtpversion>1.5</wtpversion>

</configuration>

</plugin>

<!-- 要加上下面的一句,否则执行:mvn package -Dmaven.test.skip=true的时候会报错 -->

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>2.1.1</version>

</plugin>

</plugins>

</build>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spring.version>4.0.3.RELEASE</spring.version>

</properties>

<dependencies>

<!--

Spring,使用下面的方式之后能够使用 @RunWith(SpringJUnit4ClassRunner.class)

的方式编写单元测试了

-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-oxm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.2</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.2</version>

</dependency>

<!-- Test -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.7</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>aopalliance</groupId>

<artifactId>aopalliance</artifactId>

<version>1.0</version>

</dependency>

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.3</version>

</dependency>

<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.3</version>

</dependency>

<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>1.5.4</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc14</artifactId>

<version>10.2.0.3.0</version>

</dependency>

<dependency>

<groupId>taglibs</groupId>

<artifactId>standard</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.6</version>

</dependency>

<dependency>

<groupId>commons-lang</groupId>

<artifactId>commons-lang</artifactId>

<version>2.1</version>

</dependency>

<dependency>

<groupId>net.sf.ezmorph</groupId>

<artifactId>ezmorph</artifactId>

<version>1.0.6</version>

</dependency>

<dependency>

<groupId>commons-httpclient</groupId>

<artifactId>commons-httpclient</artifactId>

<version>3.1</version>

</dependency>

<dependency>

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

<artifactId>httpclient</artifactId>

<version>4.3.1</version>

</dependency>

<dependency>

<groupId>org.apache</groupId>

<artifactId>httpmime</artifactId>

<version>4.3</version>

</dependency>

<dependency>

<groupId>net.sf.json-lib</groupId>

<artifactId>json-lib</artifactId>

<version>2.3</version>

<classifier>jdk15</classifier>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.7</version>

</dependency>

<dependency>

<groupId>com.ucap.utils</groupId>

<artifactId>utils</artifactId>

<version>1.4-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>com.ucap.base</groupId>

<artifactId>base</artifactId>

<version>1.6-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>com.ucap.wcm</groupId>

<artifactId>website-api</artifactId>

<version>1.6-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>xxx.xxx.xxx</groupId>

<artifactId>app-tpl-api</artifactId>

<version>1.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>org.jasig.cas.client</groupId>

<artifactId>cas-client-core</artifactId>

<version>3.2.0</version>

</dependency>

<dependency>

<groupId>org.jasig.cas.client</groupId>

<artifactId>ucap-cas-client</artifactId>

<version>3.2.1</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.8</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>transaction</artifactId>

<version>3.1.1</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>web.servlet</artifactId>

<version>3.1.1</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

</dependency>

<dependency>

<groupId>com.ucap.rpc</groupId>

<artifactId>rpc</artifactId>

<version>1.1-SNAPSHOT</version>

</dependency>

</dependencies>

</project>

3.在web.xml中配置使用的Spring配置的文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring_Modle</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

<filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping> 
    
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context-dispatcher.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<!-- druid -->
<servlet>
   <servlet-name>DruidStatView</servlet-name>
   <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>DruidStatView</servlet-name>
   <url-pattern>/druid/*</url-pattern>
</servlet-mapping>

<session-config>
   <!-- 设置过期时间为一年,相当于是一年都不过期 -->
<session-timeout>525600</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

4、接着配置spring的applicationContext.xml,、基于以上applicationContext.xml,我们知道,若想兼容不同的数据库、需要以下类:

注意:上图的sqlmaps下的目录名称和jdbc.properties中的数据库名称相同

其中:DataSourceInstances.java的代码如下:

package xxx.xxx.xxx.mapper.base.impl;

/**

* DataSourceInstances.java

* @attention -9-11

* @note begin modify by -9-11 -9-11

* @note begin modify by -9-11 -9-11

* @note begin modify by -9-11 、其它常用工具:

公共的mapper类

package xxx.xxx.xxx.mapper.base.impl;

import java.io.Serializable;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.lang.reflect.ParameterizedType;

import java.lang.reflect.Type;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.log4j.Logger;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.Cacheable;

import xxx.xxx.xxx.mapper.base.BaseMapper;

import com.ucap.utils.UUIDGenerator;

@SuppressWarnings("unchecked")

public class BaseMapperImpl<T, PK extends Serializable> extends

SqlSessionDaoSupport implements BaseMapper<T, PK> {

public static Logger logger = Logger.getLogger(BaseMapperImpl.class);

private Class<T> entityClass = null;

/**

* 创建默认构造方法,以取得真正的泛型类型

*/

public BaseMapperImpl() {

Class<?> c = getClass();

Type type = c.getGenericSuperclass();

if (type instanceof ParameterizedType) {

Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();

entityClass = (Class<T>) parameterizedType[0];

}

}

@Resource(name = "sqlSessionFactory")

public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {

super.setSqlSessionFactory(sqlSessionFactory);

}

// 保存实体对象

@CacheEvict(value = "ehcache", allEntries = true)

public T insert(T entity) {

try {

Field[] fileds = entity.getClass().getDeclaredFields();

for (Field field : fileds) {

if (field.getName().equals("id")) {

Method setIdMethod = entity.getClass().getDeclaredMethod(

"setId", String.class);

setIdMethod.invoke(entity, UUIDGenerator.generate());

}

}

} catch (Exception e) {

e.printStackTrace();

}

getSqlSession().insert(entity.getClass().getName() + "Mapper.insert",entity);

return entity;

}

// 更新

@CacheEvict(value = "ehcache", allEntries = true)

public void update(T entity) {

getSqlSession().update(entity.getClass().getName() + "Mapper.update",entity);

}

// 根据id删除某个对象

@CacheEvict(value = "ehcache", allEntries = true)

public void deleteById(PK id) {

getSqlSession().delete(entityClass.getName() + "Mapper.deleteById", id);

}

//根据id,逻辑上删除某个对象(更行状态值)

public void deleteByUpdateState(PK id) {

getSqlSession().delete(entityClass.getName() + "Mapper.deleteByUpdateState", id);

}

// 根据id加载某个对象

@Cacheable(value = "ehcache", key = "#id")

public T fetch(PK id) {

return (T)getSqlSession().selectOne(entityClass.getName() + "Mapper.fetch", id);

}

// 查找所有的对象

public List<T> findAll() {

return (List<T>)getSqlSession().selectList(entityClass.getName() + "Mapper.findAll", null);

}

//设置是否共享,如果SHARESTATE = 1 表示的是共享,SHARESTATE = 0:表示专享

public void updateShareState(T entity) {

getSqlSession().update(entityClass.getName() + "Mapper.updateShareState", entity);

}

/**

* 查询所有对象:分页查询

* @param paramMap

* @return 返回某1页的数据

* @attention 方法的使用注意事项  【必带参数:page】

* @author YangWeiQiang

* @date 2015-11-12

*/

public List<T> findAllByPage(Map paramMap) {

return (List<T>)getSqlSession().selectList(entityClass.getName() + "Mapper.queryListByPage", paramMap);

}

// 根据查询参数,当前页数,每页显示的数目得到分页列表

/*

@Cacheable(value = "ehcache", key = "'queryPage-'+#condition+'-'+#currentPage+'-'+#pageSize")

public Pager<T> queryPage(Map<String, Object> condition,

Integer currentPage, Integer pageSize) {

Pager<T> pager = new Pager<T>(pageSize, count(condition), currentPage);

try {

if (condition == null) {

condition = new HashMap<String, Object>();

}

condition.put("beginRow", (pager.getCurrentPage() - 1)

* pager.getPageSize());

condition.put("pageSize", pager.getPageSize());

List<T> dataList = this.getSqlSession().selectList(

entityClass.getName() + "Mapper.queryList", condition);

pager.setDataList(dataList);

return pager;

} catch (RuntimeException re) {

logger.error("findList " + entityClass.getName() + "failed :{}",

re);

re.printStackTrace();

}

return null;

}*/

/**

* 通过条件查询

*/

public int count(Map<String, Object> condition) {

int count = (Integer)getSqlSession().selectOne(entityClass.getName() + "Mapper.count", condition);

return count;

}

@Cacheable(value = "ehcache", key = "'queryList-'+#condition+'-orderBy-'+#orderBy+'-sortBy-'+#sortBy")

public List<T> queryList(Map<String, Object> condition, String orderBy,

String sortBy) {

if (condition == null) {

condition = new HashMap<String, Object>();

condition.put("orderBy", orderBy);

condition.put("sortBy", sortBy);

}

return (List<T>)getSqlSession().selectList(

entityClass.getName() + "Mapper.queryList", condition);

}

/**

* 通过条件查询一条

*/

@Cacheable(value = "ehcache", key = "'queryOne-'+#condition")

public T queryOne(Map<String, Object> condition) {

return (T)getSqlSession().selectOne(

entityClass.getName() + "Mapper.queryOne", condition);

}

/**

* 更新或保存

*/

@CacheEvict(value = "ehcache", allEntries = true)

public T updateOrSave(T t, PK id) {

if (null != fetch(id)) {

update(t);

} else {

return insert(t);

}

return t;

}

@Cacheable(value = "ehcache", key = "'findOne-'+#property+'-'+#value")

public T findOne(String property, Object value) {

Map<String, Object> condition = new HashMap<String, Object>();

condition.put(property, value);

return (T)getSqlSession().selectOne(

entityClass.getName() + "Mapper.findOne", condition);

}

@Cacheable(value = "ehcache", key = "'findList-'+#property+'-'+#value")

public List<T> findList(String property, Object value) {

Map<String, Object> condition = new HashMap<String, Object>();

condition.put(property, value);

return getSqlSession().selectList(

entityClass.getName() + "Mapper.findList", condition);

}

public Class<T> getEntityClass() {

return entityClass;

}

public Integer selectMaxId() {

return getSqlSession().selectOne(

entityClass.getName() + "Mapper.selectMaxId");

}

@CacheEvict(value = "ehcache", allEntries = true)

public void deleteByCondition(Map<String, Object> condition) {

getSqlSession().delete(

entityClass.getName() + "Mapper.deleteByCondition", condition);

}

@CacheEvict(value = "ehcache", allEntries = true)

public void deleteByProperty(String property, Object value) {

Map<String, Object> condition = new HashMap<String, Object>();

condition.put(property, value);

deleteByCondition(condition);

}

@CacheEvict(value = "ehcache", allEntries = true)

public void updateNull(T entity) {

getSqlSession().update(entityClass.getName() + "Mapper.updateNull",entity);

}

@Cacheable(value = "ehcache", key = "'selectOne-'+#mapperId+'-'+#obj")

public T selectOne(String mapperId, Object obj) {

return (T)getSqlSession().selectOne(

entityClass.getName() + "Mapper." + mapperId, obj);

}

@Cacheable(value = "ehcache", key = "'selectList-'+#mapperId+'-'+#obj")

public List<T> selectList(String mapperId, Object obj) {

return getSqlSession().selectList(

entityClass.getName() + "Mapper." + mapperId, obj);

}

@CacheEvict(value = "ehcache", allEntries = true)

public List<T> insertList(List<T> entities) {

return getSqlSession().selectList(

entityClass.getName() + "Mapper.insertList", entities);

}

/*

public DetailsPager<T> queryDetailsPage(Map<String, Object> condition, PK id) {

T currentObj = fetch(id);

if (currentObj != null) {

List<PK> ids = getSqlSession().selectList(

entityClass.getName() + "Mapper.findIds", condition);

int currentObjIndex = ids.indexOf(id);

DetailsPager<T> page = new DetailsPager<T>(currentObj);

if (currentObjIndex > 0)

page.setPreObj(fetch(ids.get(currentObjIndex - 1)));

if (currentObjIndex < ids.size() - 1)

page.setNextObj(fetch(ids.get(currentObjIndex + 1)));

return page;

}

return null;

}*/

public List<T> like(String property, Object value) {

Map<String, Object> condition = new HashMap<String, Object>();

condition.put(property, value);

return getSqlSession().selectList(

entityClass.getName() + "Mapper.like", condition);

}

}

在xxx-xxx-webapp中的单元测试写法:

package xxx.xxx.xxx;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import xxx.xxx.xxx.entity.Component;

import xxx.xxx.xxx.service.component.ComponentService;

/**

* ComponentTestCase.java 单元测试案例

* @attention

* @author toto

* @date 2016-8-24

* @note begin modify by 涂作权 2016-8-24 原始创建

*/

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {

"classpath:context-dispatcher.xml",

"classpath:applicationContext.xml"})

public class ComponentTestCase {

private static final Logger logger = Logger.getLogger(ComponentTestCase.class);

@Resource(name = "componentService")

private ComponentService componentService;

@Test

public void testHello() {

Component component = componentService.findComponent("98a77fe0a737400b80c354b216773119");

logger.info("-----------------------");

logger.info(component.getComponentName());

logger.info("-----------------------");

}

}

httpClient调用http请求。其中接口是一个action,直接返回的是json数据。

String url = ExtendedServerConfig.getInstance().getStringProperty("MANUSCRIPTS_APPENDIXS_URL");

String result = "";

HttpPost httpRequest = new HttpPost(url);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("params", params));

httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));

HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);

//释放post请求

//httpRequest.abort();

if (httpResponse.getStatusLine().getStatusCode() == 200) {

HttpEntity httpEntity = httpResponse.getEntity();

result = EntityUtils.toString(httpEntity);

}

maven常用命令:

mvn -Pall eclipse:eclipse

mvn clean

mvn compile -Dmaven.test.skip=true
mvn install -Dmaven.test.skip=true
mvn package -Dmaven.test.skip=true

SpringMVC,MyBatis项目中兼容Oracle和MySql的解决方案及其项目环境搭建配置、web项目中的单元测试写法、HttpClient调用post请求等案例的更多相关文章

  1. Jenkins+PowerShell持续集成环境搭建(三)Web项目

    1. 新建一个名字为HelloWorld.Web的Freesyle项目: 2. 配置源码管理: 3. 编译配置: 版本:选择MSBuild4 文件:D:\CI\Config\HelloWorld.We ...

  2. 关于软件开发中兼容win7注册表的解决方案

    关于软件开发中兼容win7注册表的解决方案   编写人:CC阿爸 2014-3-14 l  近来在开发一winform程序时,发现在xp 系统访问注册表一切正常.可偏这个时候,微软又提醒大家.Xp今年 ...

  3. 在Eclipse中使用Struts和Hibernate框架搭建Maven Web项目

    前言 学习使用Java还是2012年的事情,刚开始学习的Java的时候,使用的是MyEclipse工具和SSH框架.初学者适合使用MyEclipse,因为他将struts.Spring和Hiberna ...

  4. 02 eclipse中配置Web项目(含eclipse基本配置和Tomcat的配置)

    eclipse搭建web项目 一.Eclipse基本配置 找到首选项: (一)配置编码 (二)配置字体 (三)配置jdk (四)配置Tomcat 二.Tomcat配置 三.切换视图,检查Tomcat ...

  5. Eclipse 搭建 Maven Web项目

    第一步:安装JDK: 第二步:安装Eclipse: 第三步:安装tomcat7: 第四步:安装maven插件: 4.1 下载maven:http://maven.apache.org/download ...

  6. 使用MyEclipse搭建java Web项目开发

    转自:http://blog.csdn.net/jiuqiyuliang/article/details/36875217 首先,在开始搭建MyEclipse的开发环境之前,还有三步工具的安装需要完成 ...

  7. Cocos2d-x v3.0正式版尝鲜体验【1】 环境搭建和新建项目

    Cocos2d-x v3.0在前天最终公布正式版了,等了大半年最终出来了.一直没去碰之前的3.0各种beta,rc版本号,就想等正式版出来再尝试. 昨天也參加了触控科技在成都举办的沙龙活动.看到作者王 ...

  8. 从零开始学Xamarin.Forms(二) 环境搭建、创建项目

    原文:从零开始学Xamarin.Forms(二) 环境搭建.创建项目 一.环境搭建 Windows下环境搭建:     1.下载并安装jdk.Android SDK和NDK,当然还需要 VS2013 ...

  9. 【Java框架型项目从入门到装逼】第三节 - 如何用Tomcat发布web项目?

    啥叫Tomcat?有道词典是这么说的. 这个我们姑且不管,实际上呢,Tomcat是一种Web服务器,我们自己做好了一个Web项目,就可以通过Tomcat来发布.服务器呢,又分为硬件服务器和软件服务器. ...

随机推荐

  1. POJ1222熄灯问题

    千年老题,以前用枚举做,现在用高斯消元做 自由元直接做成0即可 #include<cstdio> #include<cstdlib> #include<algorithm ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    运气好,分到的房里我最先开始Hack C题,Hack了12个,听说F题沙雕莫队但我不会,最后剩不到15分钟想出E题做法打了一波结果挂了,最后虽然上分了但总有点不甘心. 最后A掉ABCD Hack+12 ...

  3. ●BZOJ 2500 幸福的道路

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2500 题解: DFS,单调队列 首先有一个结论,距离树上某一个点最远的点一定是树的直径的一个 ...

  4. APIO 2016

    我好菜啊都不会 T1.boats 题目大意:给你N段区间,按顺序决定每段区间可以选一个数或不选,若选则选的这个数必须大于所有在这之前选的数,求有多少种方案.(N<=500,区间在[1,1e9]范 ...

  5. 例10-5 uva12716

    题意:gcd(a,b) = a^b,( 1≤ a , b ≤ n) 思路: ① a^b = c, 所以 a^c = b,而且c是a的约数,枚举a,c,再gcd判断 ② 打表可知 a-b = c,而且a ...

  6. 习题7-1 uva 208(剪枝)

    题意:按最小字典序输出a到b 的所有路径. 思路:先处理出个点到目标点b的情况(是否能到达),搜索即可. 最开始我只判了a能否到b,然后给我的是WA,然后看了半天感觉思路没什么问题,然后把所有点都处理 ...

  7. FFT模板(BZOJ2179)

    实现了两个长度为n的大数相乘. #include <cstdio> #include <cmath> #include <complex> using namesp ...

  8. VLAN的划分

    VLAN划分是指逻辑上把网络资源和网络用户按照一定的原则进行划分,把一个物理上实际的网络划分成多个小的逻辑网络.设计VLAN的最初目的是隔离局域网的广播,不让它去影响网络带宽.VLAN与传统的LAN相 ...

  9. Chrome的First Paint

    前言 First paint 直译过来的意思就是浏览器第一次渲染(paint),在First paint之前是白屏,在这个时间点之后用户就能看到(部分)页面内容. 所以研究这个First Paint的 ...

  10. SQL语句删除字段,改变字段长度

    1.改变字段长度 ALTER TABLE T_MSG_SEND_R_ACC MODIFY reply_content VARCHAR(512); 2.删除字段ALTER TABLE MSG_TX_BA ...