Idea中Spring+Mybatis Web项目整合
业务背景
工作中的一个项目,应用系统需要从第三方应用系统同步数据,第三方提供了数据库实时数据库。
存在一个问题,应用系统使用JDK1.8开发,实时数据库目前采用ODBC桥接取数,仅JDK1.7支持,JDK1.8移除了ODBC桥接驱动。如果取数逻辑在应用系统库中进行开发,则应用系统JDK需降级至1.7,影响和改动较大。
经考虑,最后采用webservice的方式实现。即另启一个webservice服务,使用JDK1.7,专门与第三方进行交互取数,然后通过webservice服务,将数据提供给应用系统。由此耦合关系解除,应用系统也无需更换JDK。
本文的整合内容就是webservice服务应用的整合。该服务架构Spring+Mybatis+Tomcat,在Idea中以Maven项目的方式构建。
整合过程
1. 新建Maven Web项目。在IDEA中生成的默认目录结构可手动调整。调整后的目录结构如下图
2. 设置pom.xml,添加spring+mybatis以及其他依赖包
<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.greatech.ws</groupId> <artifactId>dataprovider</artifactId> <version>0.0.1</version>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
<!--environment begion--> <!--spring framework--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!--mybatis--> <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.3.0</version> </dependency> <!--数据库连接池--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--CXF--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-maven-plugins</artifactId> <version>3.2.1</version> <type>pom</type> </dependency> </dependencies> </project> |
3. Web.xml中配置启动Spring context配置文件
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> |
4. Tomcat部署配置
在Deployment页签中,点击编辑
新建lib目录,将pom中的jar包都加到lib中。
若不加上会一直报错
java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener