转载留以备用,原地址http://blog.csdn.net/zlw01234/article/details/54614079
第一步,创建Maven项目
1.在MyEclipse的项目列表中右键new,选MavenProject,点next
2.继续点next
3.在filter下面的列表中选maven-archetype-webapp,点next
4.填写groupId和artifactId,这里我个人理解的groupId就是你所在的组织的名称,一般是公司域名反写,artifiactId就是本项目的名称,而Version可以不改。
5.点finish,等待片刻,如果不常使用maven,等的时间可能更长一点。等MyEclipse进度条消失后,看项目列表里你刚建的项目,如果项目没有报错,那么恭喜你,一个maven项目就创建完成了。(如果报错,可能是你maven和MyEclipse整合有问题,这也是个很坑的事,但不在这篇博客的讨论范围内,可以找找专门写MyEclipse和Maven整合的博客看看)我的项目结构如下:
第二步、修改项目结构,引入框架依赖包
1.为了项目结构更清晰,我们新建源码包src/main/java,然后在webapp下创建文件夹jsp,修改后的项目结构如下图:
规定下我们的包的作用:
src/main/resources下放各种配置文件
src/main/java下放源代码
webapp/jsp下放jsp页面
2.在pom.xml文件中引入三大框架的依赖包
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.1.1.RELEASE</spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.8</mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.7.12</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- java ee包 -->
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>6.0</version>
- </dependency>
- <!-- spring框架包 start -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-oxm</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-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</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-expression</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-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring框架包 end -->
- <!-- mybatis框架包 start -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.2</version>
- </dependency>
- <!-- mybatis框架包 end -->
- <!-- 数据库驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.35</version>
- </dependency>
- <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.4</version>
- </dependency>
- <!-- jstl标签类 -->
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log END -->
- <!-- Json -->
- <!-- 格式化对象,方便输出日志 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.6</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.13</version>
- </dependency>
- <!-- 上传组件包 start -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.10</version>
- </dependency>
- </dependencies>
注意:
1.上面的pom文件是我从网上下载的,里面的依赖包是不是全都必须我也不确定,但应该是不少,既然如此,多几个也无所谓了。。。
2.修改pom文件保存后,MyEclipse会自动开始下载依赖的jar包,所以要保证你的电脑是联网状态,而且网速不能太差,不然会等很长时间。。。
如果下载完成后,羡慕没有报错,那么在此恭喜你,依赖包导入完成了!
第三步、配置文件
终于到了关键的地方了,那就是框架整合。
首先,我们在src/main/resources中创建几个配置文件:
其中
jdbc.properties用于配置连接数据库相关信息
log4j.properties用于配置log4j相关配置信息
spring.xml用于配置spring和spring管理mybatis的相关配置
springmvc.xml用于配置springmvc相关信息
SqlMapConfig.xml用于配置mybatis相关信息。
jdbc.properties
我这里只配置了基本信息,数据库连接还有很多可选配置,如连接池初始连接数量,增加连接数量等,这些都有默认值,不一定要配。
这里不要照搬我的代码,需要你配置自己的数据库连接信息,如用户名、密码、数据库名等
log4j.properties是我从网上找的一份,配置的比较多,相应的调试时打印的信息也比较多,便于排错。下面是全部代码:
- log4j.rootLogger=DEBUG, stdout, fileout , logfile
- log4j.logger.com.fiscal = DEBUG
- log4j.logger.com.system = DEBUG
- log4j.logger.com.ibatis = DEBUG
- log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG
- log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG
- log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG
- log4j.logger.java.sql.Connection = DEBUG
- log4j.logger.java.sql.Statement = DEBUG
- log4j.logger.java.sql.PreparedStatement = DEBUG, fileout
- log4j.logger.java.sql.ResultSet = DEBUG
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
- log4j.appender.logfile=org.apache.log4j.RollingFileAppender
- log4j.appender.logfile.File=D:/zpssoa.log
- log4j.appender.logfile.MaxFileSize=512KB
- log4j.appender.logfile.MaxBackupIndex=3
- log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
- log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
spring.xml配置如下,里面有注释,有spring和mybatis经验的同学应该能看懂,就不在这里介绍了
- <?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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <context:annotation-config />
- <!-- 自动扫描 -->
- <context:component-scan base-package="com.zlw.test" />
- <!-- 加载配置文件 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="classpath:jdbc.properties" />
- </bean>
- <!-- 数据库连接池 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <property name="maxActive" value="10" />
- <property name="maxIdle" value="5" />
- </bean>
- <!-- SqlsessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 数据源 -->
- <property name="dataSource" ref="dataSource" />
- <!-- mybatis配置文件 -->
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- </bean>
- <!-- MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象, 自动创建到spring容器中,bean的id是mapper的类名(首字母小写) -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 配置扫描包的路径 如果要扫描多个包,中间使用半角逗号分隔 要求mapper.xml和mapper.java同名且在同一个目录 -->
- <property name="basePackage" value="com.zlw.test" />
- <!-- 使用sqlSessionFactoryBeanName -->
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
- </beans>
springmvc.xml同上,相对来说更简单,因为这里只配置了springmvc的信息,没有整合相关的东西,只要学过springnvc的同学应该都能看懂,这里我们也是只配置了最基本的东西,如果个人有其他需要,记得配置相关信息哦。
- <?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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <!-- 使用spring组件扫描 -->
- <context:component-scan base-package="com.zlw.test" />
- <!-- 配置视图解析器 要求将jstl的包加到classpath -->
- <!-- ViewResolver -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 注册适配器 -->
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
- </list>
- </property>
- </bean>
- </beans>
SqlMapConfig.xml:由于在spring中配置了mapper扫描,所以不需要再在这里配置mapper了,但我们还是留下来这个配置文件,以防后面要对mybatis做配置。尽管现在这个文件中什么也没有。
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- </configuration>
第四步、web.xml配置
在web.xml中需要配置spring的核心过滤器和springmvc的核心控制器,为了方便还可以加上一个编码过滤器。
- <?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"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <welcome-file-list>
- <welcome-file>/index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 加载spring bean -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 编码过滤器 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <!-- 支持异步请求 -->
- <async-supported>true</async-supported>
- <init-param>
- <param-name>encoding</param-name>
- <!-- 为了支持emoji表情可能需要更高级的编码 -->
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Spring MVC servlet -->
- <servlet>
- <servlet-name>SpringMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:springmvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>SpringMVC</servlet-name>
- <url-pattern>*.action</url-pattern>
- </servlet-mapping>
- </web-app>
好了,到这里框架整合基本完成了,接下来我们做个测试。
第五步、测试
1.在src/main/java下建包:com.zlw.test
2.在包下创建entity,mapper,service,action四个包
3.在entity包下创建User.java作为实体类,为了简单,只给两个属性:username和id
- package com.zlw.test.entity;
- public class User {
- private int id;
- private String username;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserame() {
- return username;
- }
- public void setUserame(String name) {
- this.username = name;
- }
- }
4.在mapper包下创建UserMapper.java作为dao层接口。
- package com.zlw.test.mapper;
- public interface UserMapper {
- String findUserById(int id);
- }
5.在mapper包下创建UserMapper.xml,作为mybatis动态生成dao层实现类要用到的配置文件。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zlw.test.mapper.UserMapper" >
- <!-- 查询用户-->
- <select id="findUserById" resultType="String"
- parameterType="int">
- select username from user where id = #{id}
- </select>
- </mapper>
6.在service包下创建UserService.java,作为service层实现类(正式开发中应该把接口和实现类分开,这里只是为了方便,没有写接口;另外,我们全篇使用注解形式开发,为了以后尽可能少的修改配置文件)
- package com.zlw.test.service;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Service;
- import com.zlw.test.entity.User;
- import com.zlw.test.mapper.UserMapper;
- @Service
- public class UserService {
- @Resource
- private UserMapper userMapper;
- public String findUserById(int id) {
- return userMapper.findUserById(id);
- }
- }
7.在action包下创建UserAction.java文件,作为控制器
- package com.zlw.test.action;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.zlw.test.entity.User;
- import com.zlw.test.service.UserService;
- @Controller
- @RequestMapping("/user")
- public class UserAction {
- @Resource
- private UserService userService;
- @RequestMapping("/findUserById")
- public String findUserById(int id, Model model) {
- String username = userService.findUserById(id);
- if (username != null) {
- model.addAttribute("username", username);
- } else {
- model.addAttribute("username", "未找到");
- }
- return "test";
- }
- }
8.将index.jsp内容替换成下面内容
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>测试页面</title>
- </head>
- <body>
- <form
- action="${pageContext.request.contextPath }/user/findUserById.action"
- method="post">
- <input type="text" name="id" /> <input type="submit" value="测试整合性" />
- </form>
- </body>
- </html>
9.在webapp/jsp下新建jsp页面,叫test.jsp,内容如下
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>测试页面</title>
- </head>
- <body>
- 成功 名字是:${username }
- </body>
- </html>
9.在你配置的数据库中建表,插入一条记录,对我来说,就是在本机的MySQL数据库中建立一个叫foodtest的库,在里面创建叫user的表,插入一条记录是id=1,username=zlw
9.把项目部署到服务器上,我用的是Tomcat6.x,在浏览器中访问:http://localhost:8080/TestProject/index.jsp,出现如下页面:
10.在输入框中输入1,点击按钮,会跳转到结果页面,并显示根据id查出的username,如图:
至此,SSM框架整合成功,可以开始愉快的开发之旅了,再也不用管什么配置出错的问题了!