Home >Database >Mysql Tutorial >为Eclipse上的Maven project添加SpringMVC和Mybatis以实现数据库
背景 工作中要实现跨数据库的package调用,在jsp前端返回,目前项目上只是Maven,所以参照Andyの笔记来看看结合自己所在项目都需要哪些更新来实现一个简单的功能。 1、Maven 虽然项目中已经在Eclipse中嵌入了Maven,但还是可以单独安装一个Maven的工具在系统
工作中要实现跨数据库的package调用,在jsp前端返回,目前项目上只是Maven,所以参照Andyの笔记来看看结合自己所在项目都需要哪些更新来实现一个简单的功能。
虽然项目中已经在Eclipse中嵌入了Maven,但还是可以单独安装一个Maven的工具在系统中,参考Andy的Maven环境搭建和介绍 来操作即可。
中间遇到了问题“-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.”,可以在这里解决。
加载完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目录下。
按照步骤提示继续,针对即将新引入的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的配置文件。