SSM框架搭建(Maven项目)

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/chenyao1994 https://blog.csdn.net/chenyao1994/article/details/79954044

一.项目的整体目录结构


二.构建Maven项目



三、pom文件

[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.0</modelVersion>  
  4.   <groupId>com.yuanjun</groupId>  
  5.   <artifactId>SSM</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>SSM Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.  <properties>    
  11.         <!-- spring版本号 -->    
  12.         <spring.version>4.0.2.RELEASE</spring.version>    
  13.         <!-- mybatis版本号 -->    
  14.         <mybatis.version>3.2.6</mybatis.version>    
  15.         <!-- log4j日志文件管理包版本 -->    
  16.         <slf4j.version>1.7.7</slf4j.version>    
  17.         <log4j.version>1.2.17</log4j.version>    
  18.     </properties>    
  19.     
  20.     <dependencies>    
  21.         <dependency>    
  22.             <groupId>junit</groupId>    
  23.             <artifactId>junit</artifactId>    
  24.             <version>4.11</version>    
  25.             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->    
  26.             <scope>test</scope>    
  27.         </dependency>    
  28.         <!-- spring核心包 -->    
  29.         <dependency>    
  30.             <groupId>org.springframework</groupId>    
  31.             <artifactId>spring-core</artifactId>    
  32.             <version>${spring.version}</version>    
  33.         </dependency>    
  34.     
  35.         <dependency>    
  36.             <groupId>org.springframework</groupId>    
  37.             <artifactId>spring-web</artifactId>    
  38.             <version>${spring.version}</version>    
  39.         </dependency>    
  40.         <dependency>    
  41.             <groupId>org.springframework</groupId>    
  42.             <artifactId>spring-oxm</artifactId>    
  43.             <version>${spring.version}</version>    
  44.         </dependency>    
  45.         <dependency>    
  46.             <groupId>org.springframework</groupId>    
  47.             <artifactId>spring-tx</artifactId>    
  48.             <version>${spring.version}</version>    
  49.         </dependency>    
  50.     
  51.         <dependency>    
  52.             <groupId>org.springframework</groupId>    
  53.             <artifactId>spring-jdbc</artifactId>    
  54.             <version>${spring.version}</version>    
  55.         </dependency>    
  56.     
  57.         <dependency>    
  58.             <groupId>org.springframework</groupId>    
  59.             <artifactId>spring-webmvc</artifactId>    
  60.             <version>${spring.version}</version>    
  61.         </dependency>    
  62.         <dependency>    
  63.             <groupId>org.springframework</groupId>    
  64.             <artifactId>spring-aop</artifactId>    
  65.             <version>${spring.version}</version>    
  66.         </dependency>    
  67.     
  68.         <dependency>    
  69.             <groupId>org.springframework</groupId>    
  70.             <artifactId>spring-context-support</artifactId>    
  71.             <version>${spring.version}</version>    
  72.         </dependency>    
  73.     
  74.         <dependency>    
  75.             <groupId>org.springframework</groupId>    
  76.             <artifactId>spring-test</artifactId>    
  77.             <version>${spring.version}</version>    
  78.         </dependency>    
  79.         <!-- mybatis核心包 -->    
  80.         <dependency>    
  81.             <groupId>org.mybatis</groupId>    
  82.             <artifactId>mybatis</artifactId>    
  83.             <version>${mybatis.version}</version>    
  84.         </dependency>    
  85.         <!-- mybatis/spring包 -->    
  86.         <dependency>    
  87.             <groupId>org.mybatis</groupId>    
  88.             <artifactId>mybatis-spring</artifactId>    
  89.             <version>1.2.2</version>    
  90.         </dependency>    
  91.         <!-- 导入java ee jar 包 -->    
  92.         <dependency>    
  93.             <groupId>javax</groupId>    
  94.             <artifactId>javaee-api</artifactId>    
  95.             <version>7.0</version>    
  96.         </dependency>    
  97.         <!-- 导入Mysql数据库链接jar包 -->    
  98.         <dependency>    
  99.             <groupId>mysql</groupId>    
  100.             <artifactId>mysql-connector-java</artifactId>    
  101.             <version>5.1.30</version>   
  102.         </dependency>    
  103.         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->    
  104.         <dependency>    
  105.             <groupId>commons-dbcp</groupId>    
  106.             <artifactId>commons-dbcp</artifactId>    
  107.             <version>1.2.2</version>    
  108.         </dependency>    
  109.         <!-- JSTL标签类 -->    
  110.         <dependency>    
  111.             <groupId>jstl</groupId>    
  112.             <artifactId>jstl</artifactId>    
  113.             <version>1.2</version>    
  114.         </dependency>    
  115.         <!-- 日志文件管理包 -->    
  116.         <!-- log start -->    
  117.         <dependency>    
  118.             <groupId>log4j</groupId>    
  119.             <artifactId>log4j</artifactId>    
  120.             <version>${log4j.version}</version>    
  121.         </dependency>    
  122.             
  123.             
  124.         <!-- 格式化对象,方便输出日志 -->    
  125.         <dependency>    
  126.             <groupId>com.alibaba</groupId>    
  127.             <artifactId>fastjson</artifactId>    
  128.             <version>1.1.41</version>    
  129.         </dependency>    
  130.     
  131.     
  132.         <dependency>    
  133.             <groupId>org.slf4j</groupId>    
  134.             <artifactId>slf4j-api</artifactId>    
  135.             <version>${slf4j.version}</version>    
  136.         </dependency>    
  137.     
  138.         <dependency>    
  139.             <groupId>org.slf4j</groupId>    
  140.             <artifactId>slf4j-log4j12</artifactId>    
  141.             <version>${slf4j.version}</version>    
  142.         </dependency>    
  143.         <!-- log end -->    
  144.         <!-- 映入JSON -->    
  145.         <dependency>    
  146.             <groupId>org.codehaus.jackson</groupId>    
  147.             <artifactId>jackson-mapper-asl</artifactId>    
  148.             <version>1.9.13</version>    
  149.         </dependency>    
  150.         <!-- 上传组件包 -->    
  151.         <dependency>    
  152.             <groupId>commons-fileupload</groupId>    
  153.             <artifactId>commons-fileupload</artifactId>    
  154.             <version>1.3.1</version>    
  155.         </dependency>    
  156.         <dependency>    
  157.             <groupId>commons-io</groupId>    
  158.             <artifactId>commons-io</artifactId>    
  159.             <version>2.4</version>    
  160.         </dependency>    
  161.         <dependency>    
  162.             <groupId>commons-codec</groupId>    
  163.             <artifactId>commons-codec</artifactId>    
  164.             <version>1.9</version>    
  165.         </dependency>    
  166.     </dependencies>    
  167.   <build>  
  168.     <finalName>SSM</finalName>  
  169.   </build>  
  170. </project>  

四、web.xml的配置,包括字符集的设置,Springmvc的分发器的配置,加载Spring文件

[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 Application</display-name>    
  7.     <!-- Spring和mybatis的配置文件 -->    
  8.     <context-param>    
  9.         <param-name>contextConfigLocation</param-name>    
  10.         <param-value>classpath:spring-mybatis.xml</param-value>    
  11.     </context-param>    
  12.     <!-- 编码过滤器 -->    
  13.     <filter>    
  14.         <filter-name>encodingFilter</filter-name>    
  15.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  16.         <async-supported>true</async-supported>    
  17.         <init-param>    
  18.             <param-name>encoding</param-name>    
  19.             <param-value>UTF-8</param-value>    
  20.         </init-param>    
  21.     </filter>    
  22.     <filter-mapping>    
  23.         <filter-name>encodingFilter</filter-name>    
  24.         <url-pattern>/*</url-pattern>    
  25.     </filter-mapping>    
  26.     <!-- Spring监听器 -->    
  27.     <listener>    
  28.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  29.     </listener>    
  30.     <!-- 防止Spring内存溢出监听器 -->    
  31.     <listener>    
  32.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>    
  33.     </listener>    
  34.     
  35.     <!-- Spring MVC servlet -->    
  36.     <servlet>    
  37.         <servlet-name>SpringMVC</servlet-name>    
  38.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  39.         <init-param>    
  40.             <param-name>contextConfigLocation</param-name>    
  41.             <param-value>classpath:spring-mvc.xml</param-value>    
  42.         </init-param>    
  43.         <load-on-startup>1</load-on-startup>    
  44.         <async-supported>true</async-supported>    
  45.     </servlet>    
  46.     <servlet-mapping>    
  47.         <servlet-name>SpringMVC</servlet-name>    
  48.         <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->    
  49.         <url-pattern>/</url-pattern>    
  50.     </servlet-mapping>    
  51.     <welcome-file-list>    
  52.         <welcome-file>/index.jsp</welcome-file>    
  53.     </welcome-file-list>    
  54.     
  55. </web-app>    

五.在rousources建立相关的配置文件

5.1 数据库的信息配置(注意不能有空格)

[html] view plain copy
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://127.0.0.1/websocket  
  3. jdbc.username=root  
  4. jdbc.password=123456  

5.2 日志信息配置

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


5.3 SpringMVC的配置,主要包括控制器的扫描路径,视图的配置,上传配置等

[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.     <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->    
  13.     <context:component-scan base-package="com.yuanjun.control" />    
  14.     <!--避免IE执行AJAX时,返回JSON出现下载文件 -->    
  15.     <bean id="mappingJacksonHttpMessageConverter"    
  16.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
  17.         <property name="supportedMediaTypes">    
  18.             <list>    
  19.                 <value>text/html;charset=UTF-8</value>    
  20.             </list>    
  21.         </property>    
  22.     </bean>    
  23.     <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->    
  24.     <bean    
  25.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
  26.         <property name="messageConverters">    
  27.             <list>    
  28.                 <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->    
  29.             </list>    
  30.         </property>    
  31.     </bean>    
  32.     <!-- 定义跳转的文件的前后缀 ,视图模式配置-->    
  33.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  34.         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->    
  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>    

 5.4 Spring整合Mybatis

[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.yuanjun" />    
  14.     <!-- 引入配置文件 -->    
  15.     <bean id="propertyConfigurer"    
  16.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  17.         <property name="location" value="classpath:jdbc.properties" />    
  18.     </bean>   
  19.     <!-- 配置数据源 -->   
  20.     <bean id="dataSource"    
  21.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
  22.         <property name="driverClassName" value="${jdbc.driver}" />    
  23.         <property name="url" value="${jdbc.url}" />    
  24.         <property name="username" value="${jdbc.username}" />    
  25.         <property name="password" value="${jdbc.password}" />    
  26.     </bean>     
  27.     
  28.     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    
  29.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
  30.         <property name="dataSource" ref="dataSource" />    
  31.         <!-- 自动扫描mapping.xml文件 -->    
  32.         <property name="mapperLocations" value="classpath:com/yuanjun/mapping/*.xml"></property>    
  33.     </bean>    
  34.     
  35.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    
  36.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  37.         <property name="basePackage" value="com.yuanjun.dao" />    
  38.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    
  39.     </bean>    
  40.     
  41.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    
  42.     <bean id="transactionManager"    
  43.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  44.         <property name="dataSource" ref="dataSource" />    
  45.     </bean>    
  46.     
  47. </beans>    

六.常规的各功能模块

6.1 bean 对应的实体类

[java] view plain copy
  1. package com.yuanjun.bean;  
  2. /** 
  3.  *  
  4.  * @author Administrator 
  5.  * 
  6.  */  
  7. public class Person {  
  8.     private int id;  
  9.       
  10.     private String password;  
  11.       
  12.     private String name;  
  13.       
  14.     public Person(){  
  15.           
  16.     }  
  17.   
  18.     public Person(int id, String password, String name) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.password = password;  
  22.         this.name = name;  
  23.     }  
  24.   
  25.     public int getId() {  
  26.         return id;  
  27.     }  
  28.   
  29.     public void setId(int id) {  
  30.         this.id = id;  
  31.     }  
  32.   
  33.     public String getPassword() {  
  34.         return password;  
  35.     }  
  36.   
  37.     public void setPassword(String password) {  
  38.         this.password = password;  
  39.     }  
  40.   
  41.     public String getUsername() {  
  42.         return name;  
  43.     }  
  44.   
  45.     public void setUsername(String name) {  
  46.         this.name = name;  
  47.     }  
  48.   
  49.     @Override  
  50.     public String toString() {  
  51.         return "User [id=" + id + ", password=" + password + ", name="  
  52.                 + name + "]";  
  53.     }  
  54.       
  55.       
  56. }  

6.2 dao层 采用xml配置方式

[java] view plain copy
  1. package com.yuanjun.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.yuanjun.bean.Person;  
  6.   
  7. public interface PersonDao {  
  8.   
  9.     List<Person> findall();   
  10.   
  11. }  

注意对应关系 namespace对象dao接口,id对应接口对应的方法,resultType返回的实体类

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  3. <mapper namespace="com.yuanjun.dao.PersonDao" >  
  4.   <select id="findall" resultType="com.yuanjun.bean.Person">  
  5.      select id,name,password from person  
  6.   </select>  
  7.     
  8. </mapper  

6.3 Service层

[java] view plain copy
  1. package com.yuanjun.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.yuanjun.bean.Person;  
  6.   
  7. public interface PersonService {  
  8.       
  9.     List<Person> findAll();  
  10.       
  11. }     

[java] view plain copy
  1. package com.yuanjun.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Service;  
  7.   
  8. import com.yuanjun.bean.Person;  
  9. import com.yuanjun.dao.PersonDao;  
  10. @Service  
  11. public class PersonServiceImpl implements PersonService {  
  12.       
  13.     @Autowired  
  14.     private PersonDao personDao;  
  15.       
  16.     public PersonDao getPersonDao() {  
  17.         return personDao;  
  18.     }  
  19.   
  20.     public void setPersonDao(PersonDao personDao) {  
  21.         this.personDao = personDao;  
  22.     }  
  23.   
  24.     public List<Person> findAll() {  
  25.         return personDao.findall();  
  26.     }  
  27.   
  28. }  

6.4 控制层

[java] view plain copy
  1. package com.yuanjun.control;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8.   
  9. import com.yuanjun.bean.Person;  
  10. import com.yuanjun.service.PersonService;  
  11.   
  12. @Controller  
  13. public class LoginControl {  
  14.     @Autowired  
  15.     private PersonService personService;  
  16.       
  17.     @RequestMapping("/main")  
  18.     public String test(){  
  19.           
  20.         List<Person> list = personService.findAll();  
  21.         for (Person person : list) {  
  22.             System.out.println(person);  
  23.         }  
  24.         return "main";  
  25.     }  
  26. }  



已成功的跳转,并且在控制台打印出相应的数据库信息,完成SSM的框架的整合,

阅读更多

没有更多推荐了,返回首页