为Eclipse上的Maven project添加SpringMVC和Mybatis以实现数据库package的调用

背景

工作中要实现跨数据库的package调用,在jsp前端返回,目前项目上只是Maven,所以参照Andyの笔记来看看结合自己所在项目都需要哪些更新来实现一个简单的功能。

1、Maven

虽然项目中已经在Eclipse中嵌入了Maven,但还是可以单独安装一个Maven的工具在系统中,参考Andy的Maven环境搭建和介绍 来操作即可。为Eclipse上的Maven project添加SpringMVC和Mybatis以实现数据库package的调用_第1张图片

中间遇到了问题“-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.”,可以在这里解决。

2、Generate Mybatis

加载完dependence之后,按Andy的Mybatis使用generator自动生成映射配置文件信息交待生成配置文件。其中正确生成了BxxxxxLxxxx.java,BxxxxxLxxxxMapper.java 以及BxxxxxLxxxxMapper.xml,分别放在\src\main\java\com\xxxxx\xxx\domain,src\main\java\com\xxxxx\xxx\mapper以及src\main\java\com\xxxxx\xxx\mapper\sqlmaps目录下。

3、引入Spring并配置

按照步骤提示继续,针对即将新引入的jar包,更改pom.xml文件

        <!-- spring add by Steven -->
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>3.1.1.RELEASE</version>  
        </dependency>
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>3.1.1.RELEASE</version>  
            <scope>test</scope>  
        </dependency>
        <!-- mybatis add by Steven -->
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>3.1.0</version> 
        </dependency>
        <!--mybatis spring add by Steven -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.1.1</version>
        </dependency>
        <!--log4j add by Steven -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>1.2.17</version>
        </dependency>
        <!-- struts2 add by Steven -->
        <dependency>
	        <groupId>org.apache.struts</groupId>
	        <artifactId>struts2-core</artifactId>
	        <version>2.3.16.3</version>
        </dependency>
        <dependency>
	        <groupId>org.apache.struts</groupId>
	        <artifactId>struts2-json-plugin</artifactId>
	        <version>2.3.16.3</version>
        </dependency>
        <dependency>
	        <groupId>org.apache.struts</groupId>
	        <artifactId>struts2-spring-plugin</artifactId>
	        <version>2.3.16.3</version>
        </dependency>
        <!-- Gson add by Steven -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.1</version>
        </dependency>
        <!-- c3p0 add by Steven -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5</version>
        </dependency>
        <!-- aspectjweaver add by Steven -->
        <dependency>
	        <groupId>org.aspectj</groupId>
	        <artifactId>aspectjweaver</artifactId>
	        <version>1.6.8</version>
        </dependency>
        <!-- cglib add by Steven -->
        <dependency>
	        <groupId>cglib</groupId>
	        <artifactId>cglib</artifactId>
	        <version>2.2.2</version>
        </dependency>
...
        <resources>
         <resource>        
                 <directory>src/main/resource</directory>
           <includes>
              <include>*.xml</include>
              <include>*.properties</include>
           </includes>
             </resource>
     </resources> 
以及需要在build节点里加入以下resources引用,以帮助Maven加载需要的struts和myBatisConfig的配置文件。



你可能感兴趣的:(spring,maven,mybatis,struts2)