【SSM】Eclipse使用Maven创建Web项目+整合SSM框架

一、准备环境:

       maven:apache-maven-3.2.3

       jdk:jdk1.8.0_25

       tomcat:tomcat-6.0.37

       mysql:阿里云centos7 搭建的mysql


二、配置Maven、jdk

     1、Window——>Preferences——>Maven——>设置自己的Settings

     2、Window——>Preferences——>Java——>Installed JREs——>Add

              

     配置好maven之后,就可以在本地设置好的地方看到对应的Maven文件了,还有包含resp文件夹


三、新建Maven项目:

     1、右击——>New(或者是File——>New——>other——>Maven——>Maven Project——>Next

           


           


2、这时候,我们就成功创建出一个Maven项目了,项目结构如下图:

          


       到这里,我们的Maven项目就建好了,接下来,我们来整合搭建SSM(Spring MVC + Spring + Mybatis)

首先我们来配置一些整合框架所必须要的jar包,也就是来配置我们的pom文件,然后直接alt+f5,保存后,就会自动下载,存放在之前配置好的本地maven仓库中。


pox.xml配置依赖的内容:

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   <groupId>org.stormgroupId>  
  5.   <artifactId>stormartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>0.0.1-SNAPSHOTversion>  
  8.   <name>storm Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <properties>    
  11.             
  12.         <spring.version>4.0.2.RELEASEspring.version>    
  13.             
  14.         <mybatis.version>3.2.6mybatis.version>    
  15.             
  16.         <slf4j.version>1.7.7slf4j.version>    
  17.         <log4j.version>1.2.17log4j.version>    
  18.     properties>    
  19.   <dependencies>  
  20.     <dependency>  
  21.       <groupId>junitgroupId>    
  22.       <artifactId>junitartifactId>    
  23.       <version>4.11version>    
  24.           
  25.       <scope>testscope>   
  26.     dependency>  
  27.     <dependency>    
  28.             <groupId>org.springframeworkgroupId>    
  29.             <artifactId>spring-coreartifactId>    
  30.             <version>${spring.version}version>    
  31.         dependency>    
  32.     
  33.         <dependency>    
  34.             <groupId>org.springframeworkgroupId>    
  35.             <artifactId>spring-webartifactId>    
  36.             <version>${spring.version}version>    
  37.         dependency>    
  38.         <dependency>    
  39.             <groupId>org.springframeworkgroupId>    
  40.             <artifactId>spring-oxmartifactId>    
  41.             <version>${spring.version}version>    
  42.         dependency>    
  43.         <dependency>    
  44.             <groupId>org.springframeworkgroupId>    
  45.             <artifactId>spring-txartifactId>    
  46.             <version>${spring.version}version>    
  47.         dependency>    
  48.     
  49.         <dependency>    
  50.             <groupId>org.springframeworkgroupId>    
  51.             <artifactId>spring-jdbcartifactId>    
  52.             <version>${spring.version}version>    
  53.         dependency>    
  54.     
  55.         <dependency>    
  56.             <groupId>org.springframeworkgroupId>    
  57.             <artifactId>spring-webmvcartifactId>    
  58.             <version>${spring.version}version>    
  59.         dependency>    
  60.         <dependency>    
  61.             <groupId>org.springframeworkgroupId>    
  62.             <artifactId>spring-aopartifactId>    
  63.             <version>${spring.version}version>    
  64.         dependency>    
  65.     
  66.         <dependency>    
  67.             <groupId>org.springframeworkgroupId>    
  68.             <artifactId>spring-context-supportartifactId>    
  69.             <version>${spring.version}version>    
  70.         dependency>    
  71.     
  72.         <dependency>    
  73.             <groupId>org.springframeworkgroupId>    
  74.             <artifactId>spring-testartifactId>    
  75.             <version>${spring.version}version>    
  76.         dependency>    
  77.             
  78.         <dependency>    
  79.             <groupId>org.mybatisgroupId>    
  80.             <artifactId>mybatisartifactId>    
  81.             <version>${mybatis.version}version>    
  82.         dependency>    
  83.             
  84.         <dependency>    
  85.             <groupId>org.mybatisgroupId>    
  86.             <artifactId>mybatis-springartifactId>    
  87.             <version>1.2.2version>    
  88.         dependency>    
  89.             
  90.         <dependency>    
  91.             <groupId>javaxgroupId>    
  92.             <artifactId>javaee-apiartifactId>    
  93.             <version>7.0version>    
  94.         dependency>    
  95.         <dependency>  
  96.           <groupId>com.alibabagroupId>  
  97.           <artifactId>druidartifactId>  
  98.           <version>1.0.31version>  
  99.         dependency>  
  100.             
  101.         <dependency>    
  102.             <groupId>mysqlgroupId>    
  103.             <artifactId>mysql-connector-javaartifactId>    
  104.             <version>5.1.30version>    
  105.         dependency>    
  106.             
  107.         <dependency>    
  108.             <groupId>commons-dbcpgroupId>    
  109.             <artifactId>commons-dbcpartifactId>    
  110.             <version>1.2.2version>    
  111.         dependency>    
  112.             
  113.         <dependency>    
  114.             <groupId>jstlgroupId>    
  115.             <artifactId>jstlartifactId>    
  116.             <version>1.2version>    
  117.         dependency>    
  118.             
  119.             
  120.         <dependency>    
  121.             <groupId>log4jgroupId>    
  122.             <artifactId>log4jartifactId>    
  123.             <version>${log4j.version}version>    
  124.         dependency>    
  125.             
  126.             
  127.             
  128.         <dependency>    
  129.             <groupId>com.alibabagroupId>    
  130.             <artifactId>fastjsonartifactId>    
  131.             <version>1.1.41version>    
  132.         dependency>    
  133.     
  134.     
  135.         <dependency>    
  136.             <groupId>org.slf4jgroupId>    
  137.             <artifactId>slf4j-apiartifactId>    
  138.             <version>${slf4j.version}version>    
  139.         dependency>    
  140.     
  141.         <dependency>    
  142.             <groupId>org.slf4jgroupId>    
  143.             <artifactId>slf4j-log4j12artifactId>    
  144.             <version>${slf4j.version}version>    
  145.         dependency>    
  146.             
  147.             
  148.         <dependency>    
  149.             <groupId>org.codehaus.jacksongroupId>    
  150.             <artifactId>jackson-mapper-aslartifactId>    
  151.             <version>1.9.13version>    
  152.         dependency>    
  153.             
  154.         <dependency>    
  155.             <groupId>commons-fileuploadgroupId>    
  156.             <artifactId>commons-fileuploadartifactId>    
  157.             <version>1.3.1version>    
  158.         dependency>    
  159.         <dependency>    
  160.             <groupId>commons-iogroupId>    
  161.             <artifactId>commons-ioartifactId>    
  162.             <version>2.4version>    
  163.         dependency>    
  164.         <dependency>    
  165.             <groupId>commons-codecgroupId>    
  166.             <artifactId>commons-codecartifactId>    
  167.             <version>1.9version>    
  168.         dependency>  
  169.   dependencies>  
  170.   <build>  
  171.     <finalName>stormfinalName>  
  172.   build>  
  173. project>  


2、在src/main/resources下添加配置文件:spring-mybatis.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    
  4.     xmlns:context="http://www.springframework.org/schema/context"    
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  7.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
  8.                         http://www.springframework.org/schema/context      
  9.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd      
  10.                         http://www.springframework.org/schema/mvc      
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    
  12.         
  13.     <context:component-scan base-package="com.cn.hnust" />    
  14.         
  15.         
  16.         <property name="initialSize" value="${jdbc.initialSize}">property>    
  17.             
  18.         <property name="maxActive" value="${jdbc.maxActive}">property>    
  19.             
  20.         <property name="maxIdle" value="${jdbc.maxIdle}">property>    
  21.             
  22.         <property name="minIdle" value="${jdbc.minIdle}">property>    
  23.             
  24.         <property name="maxWait" value="${jdbc.maxWait}">property>    
  25.     bean>    
  26.     
  27.         
  28.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
  29.         <property name="dataSource" ref="dataSource" />    
  30.             
  31.         <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml">property>    
  32.     bean>    
  33.     
  34.         
  35.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  36.         <property name="basePackage" value="com.cn.hnust.dao" />    
  37.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>    
  38.     bean>    
  39.     
  40.         
  41.     <bean id="transactionManager"    
  42.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  43.         <property name="dataSource" ref="dataSource" />    
  44.     bean>    
  45.     
  46. beans>    


配置数据库连接池:jdbc.properties

[html]  view plain  copy
  1. jdbc.driver=com.mysql.jdbc.Driver    
  2. jdbc.url=jdbc:mysql://**.**.**.**:3306/db_zsl    
  3. jdbc.username=root    
  4. jdbc.password=123456    
  5. #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570    
  6. jdbc.initialSize=0    
  7. #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570    
  8. jdbc.maxActive=20    
  9. #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2    
  10. jdbc.maxIdle=20    
  11. #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2    
  12. jdbc.minIdle=1    
  13. #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4  jdbc.  
  14. jdbc.maxWait=60000  


配置日志:log4j.properties

[html]  view plain  copy
  1. log4j.rootLogger=INFO,Console,File    
  2. #\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0    
  3. log4j.appender.Console=org.apache.log4j.ConsoleAppender    
  4. log4j.appender.Console.Target=System.out    
  5. #\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F    
  6. log4j.appender.Console.layout = org.apache.log4j.PatternLayout    
  7. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n    
  8.     
  9. #\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6    
  10. log4j.appender.File = org.apache.log4j.RollingFileAppender    
  11. #\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55    
  12. log4j.appender.File.File = logs/ssm.log    
  13. #\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F    
  14. log4j.appender.File.MaxFileSize = 10MB    
  15. # \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7    
  16. log4j.appender.File.Threshold = ALL    
  17. log4j.appender.File.layout = org.apache.log4j.PatternLayout    
  18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n  



接下来,我们来整合spring mvc:spring-mvc.xml:

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    
  4.     xmlns:context="http://www.springframework.org/schema/context"    
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  7.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd      
  8.                         http://www.springframework.org/schema/context      
  9.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd      
  10.                         http://www.springframework.org/schema/mvc      
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    
  12.         
  13.     <context:component-scan base-package="com.cn.hnust.controller" />    
  14.         
  15.     <bean id="mappingJacksonHttpMessageConverter"    
  16.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
  17.         <property name="supportedMediaTypes">    
  18.             <list>    
  19.                 <value>text/html;charset=UTF-8value>    
  20.             list>    
  21.         property>    
  22.     bean>    
  23.         
  24.     <bean    
  25.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
  26.         <property name="messageConverters">    
  27.             <list>    
  28.                 <ref bean="mappingJacksonHttpMessageConverter" />     
  29.             list>    
  30.         property>    
  31.     bean>    
  32.         
  33.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  34.             
  35.         <property name="prefix" value="/WEB-INF/jsp/" />    
  36.         <property name="suffix" value=".jsp" />    
  37.     bean>    
  38.         
  39.         
  40.     <bean id="multipartResolver"      
  41.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
  42.             
  43.         <property name="defaultEncoding" value="utf-8" />      
  44.             
  45.         <property name="maxUploadSize" value="10485760000" />      
  46.             
  47.         <property name="maxInMemorySize" value="40960" />      
  48.     bean>     
  49.     
  50. beans>    


修改web.xml

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.     xmlns="http://java.sun.com/xml/ns/javaee"    
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    
  5.     version="3.0">    
  6.     <display-name>Archetype Created Web Applicationdisplay-name>    
  7.         
  8.     <context-param>    
  9.         <param-name>contextConfigLocationparam-name>    
  10.         <param-value>classpath:spring-mybatis.xmlparam-value>    
  11.     context-param>    
  12.         
  13.     <filter>    
  14.         <filter-name>encodingFilterfilter-name>    
  15.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>    
  16.         <async-supported>trueasync-supported>    
  17.         <init-param>    
  18.             <param-name>encodingparam-name>    
  19.             <param-value>UTF-8param-value>    
  20.         init-param>    
  21.     filter>    
  22.     <filter-mapping>    
  23.         <filter-name>encodingFilterfilter-name>    
  24.         <url-pattern>/*url-pattern>    
  25.     filter-mapping>    
  26.         
  27.     <listener>    
  28.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>    
  29.     listener>    
  30.         
  31.     <listener>    
  32.         <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>    
  33.     listener>    
  34.     
  35.         
  36.     <servlet>    
  37.         <servlet-name>SpringMVCservlet-name>    
  38.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>    
  39.         <init-param>    
  40.             <param-name>contextConfigLocationparam-name>    
  41.             <param-value>classpath:spring-mvc.xmlparam-value>    
  42.         init-param>    
  43.         <load-on-startup>1load-on-startup>    
  44.         <async-supported>trueasync-supported>    
  45.     servlet>    
  46.     <servlet-mapping>    
  47.         <servlet-name>SpringMVCservlet-name>    
  48.             
  49.         <url-pattern>/url-pattern>    
  50.     servlet-mapping>    
  51.     <welcome-file-list>    
  52.         <welcome-file>/index.jspwelcome-file>    
  53.     welcome-file-list>    
  54.     
  55. web-app>    



到这里,我们的ssm框架就完成了,下面,我们来个实现:


在src/main/java下分别添加我们的dao包,service包,还有controller包.包里面添加相应的类,效果图如下:



UserController.java:

[java]  view plain  copy
  1. package com.cn.hnust.controller;  
  2.   
  3. import javax.annotation.Resource;  
  4. import javax.servlet.http.HttpServletRequest;  
  5.   
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.ui.Model;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9.   
  10. import com.cn.hnust.pojo.User;  
  11. import com.cn.hnust.service.IUserService;  
  12.   
  13. @Controller    
  14. @RequestMapping("/user")    
  15. public class UserController {  
  16.       @Resource    
  17.         private IUserService userService;    
  18.             
  19.         @RequestMapping("/showUser")    
  20.         public String toIndex(HttpServletRequest request,Model model){    
  21.             int userId = Integer.parseInt(request.getParameter("id"));    
  22.             User user = this.userService.getUserById(userId);    
  23.             model.addAttribute("user", user);    
  24.             return "showUser";    
  25.         }  
  26. }  


IUserDao.java:

[java]  view plain  copy
  1. package com.cn.hnust.dao;  
  2.   
  3. import com.cn.hnust.pojo.User;  
  4.   
  5. public interface IUserDao {  
  6.   
  7.     public User selectByPrimaryKey(int userId) ;  
  8. }  


User.java:

[java]  view plain  copy
  1. package com.cn.hnust.pojo;  
  2.   
  3. public class User {  
  4.     private Integer id;  
  5.   
  6.     private String userName;  
  7.   
  8.     private String password;  
  9.   
  10.     private Integer age;  
  11.   
  12.     public Integer getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(Integer id) {  
  17.         this.id = id;  
  18.     }  
  19.   
  20.     public String getUserName() {  
  21.         return userName;  
  22.     }  
  23.   
  24.     public void setUserName(String userName) {  
  25.         this.userName = userName == null ? null : userName.trim();  
  26.     }  
  27.   
  28.     public String getPassword() {  
  29.         return password;  
  30.     }  
  31.   
  32.     public void setPassword(String password) {  
  33.         this.password = password == null ? null : password.trim();  
  34.     }  
  35.   
  36.     public Integer getAge() {  
  37.         return age;  
  38.     }  
  39.   
  40.     public void setAge(Integer age) {  
  41.         this.age = age;  
  42.     }  
  43. }  

UserMapper.xml:

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <mapper namespace="com.cn.hnust.dao.IUserDao">  
  4.   <resultMap id="BaseResultMap" type="com.cn.hnust.pojo.User" >  
  5.     <id column="id" property="id" jdbcType="INTEGER" />  
  6.     <result column="user_name" property="userName" jdbcType="VARCHAR" />  
  7.     <result column="password" property="password" jdbcType="VARCHAR" />  
  8.     <result column="age" property="age" jdbcType="INTEGER" />  
  9.   resultMap>  
  10.   <sql id="Base_Column_List" >  
  11.     id, user_name, password, age  
  12.   sql>  
  13.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >  
  14.     select   
  15.     <include refid="Base_Column_List" />  
  16.     from user_t  
  17.     where id = #{id,jdbcType=INTEGER}  
  18.   select>  
  19. mapper>  



IUserService.java:

[java]  view plain  copy
  1. package com.cn.hnust.service;  
  2.   
  3. import com.cn.hnust.pojo.User;  
  4.   
  5. public interface IUserService {  
  6.     public User getUserById(int userId);  
  7. }  


UserServiceImpl.java:

[java]  view plain  copy
  1. package com.cn.hnust.service.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.cn.hnust.dao.IUserDao;  
  8. import com.cn.hnust.pojo.User;  
  9. import com.cn.hnust.service.IUserService;  
  10.   
  11. @Service("userService")  
  12. public class UserServiceImpl implements IUserService {  
  13.     @Resource    
  14.     private IUserDao userDao;    
  15.         
  16.     public User getUserById(int userId) {    
  17.         return this.userDao.selectByPrimaryKey(userId);  
  18.     }  
  19. }  



接下来就是我们的静态页面了jsp文件了,在WEB-INF文件夹下新建jsp文件夹,添加userinfo.jsp

showUser.jsp:

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. >  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title heretitle>  
  8. head>  
  9. <body>  
  10.   
  11. <input id="userName" value="${user.userName}">  
  12. body>  
  13. html>  


哦,忘记配置tomcat,配置一下tomcat,然后部署发布项目,进行测试


到此,框架成!

其实,我们完全可以使用工具来自动生成IDao、Mapper,相关知识就请大家自行学习,不再一一列举。


总结:
   真心知道用maven之后,我们大框架真的方便多了,最起码不用个顶个的去找jar包,然后逐一的
导入,只需要在pom文件里面配置一下依赖,就好了。虽然说比较简单,可是可能是自己没有动手弄过
吧,期间除了各种各样的错,或者说是失误,但是,总之,耗时一天半,总算是通了,这也是收获,
加油吧,少(qing)年。

你可能感兴趣的:(架构)