ssm整合超详细步骤

简介

  1. 首先目录 java根目录、resources配置文件目录、webapp目录
  2. java 目录设为Sources Root resources 设为Resources Root
  3. java 目录下面建必须有的四个文件包
    • Controller
    • Mapper
    • Service
      • 建一个Impl包
    • Pojo (存放实体类的包)
  4. resources
    • heuet.edu.Mapper
    • spring
      • applicationContext.xml
      • spring-servlet.xml
      • spring-mybatis.xml
      • spring-context.xml
    • db.properties
    • log4j.properties

ssm整合超详细步骤_第1张图片

首先导入jar包

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
    <spring.version>5.0.8.RELEASEspring.version>
    <jackson.version>2.9.8jackson.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring.version}version>
    dependency>



    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-coreartifactId>
      <version>${jackson.version}version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>${jackson.version}version>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>${jackson.version}version>
    dependency>







    

    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-core-aslartifactId>
      <version>1.9.13version>
    dependency>
    <dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>



    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.1version>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>8.0.13 version>
    dependency>
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.6version>
    dependency>

    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>1.7.25version>
    dependency>
    
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>1.7.25version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-nopartifactId>
      <version>1.7.2version>
    dependency>
    
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>1.2.17version>
    dependency>


    
    
    <dependency>
      <groupId>com.mchangegroupId>
      <artifactId>c3p0artifactId>
      <version>0.9.5.2version>
    dependency>

    
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>1.3.2version>
    dependency>


    
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>4.0.1version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>javax.servlet.jsp-apiartifactId>
      <version>2.3.1version>
      <scope>providedscope>
    dependency>
    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>jstlartifactId>
      <version>1.2version>
    dependency>
    
    
    <dependency>
      <groupId>joda-timegroupId>
      <artifactId>joda-timeartifactId>
      <version>2.9.9version>
    dependency>
    
    
    <dependency>
      <groupId>com.github.pagehelpergroupId>
      <artifactId>pagehelperartifactId>
      <version>5.1.2version>
    dependency>
    
    
    <dependency>
      <groupId>commons-codecgroupId>
      <artifactId>commons-codecartifactId>
      <version>1.10version>
    dependency>



  dependencies>

###配置applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:spring/spring-*.xml"/>
beans>

配置spring-mybatis.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		//http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	
    <context:component-scan base-package="heuet.edu">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <context:property-placeholder  location="classpath:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}">property>
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>
        
        

        <property name="maxPoolSize" value="100">property>

        
        <property name="autoCommitOnClose" value="false"/>
        
        <property name="checkoutTimeout" value="100000"/>
        
        <property name="acquireRetryAttempts" value="2"/>
    bean>
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    

    
    <property name="mapperLocations" value="classpath:heuet/edu/Mapper/**/*.xml"/>
    <property name="configuration">
        <bean class="org.apache.ibatis.session.Configuration">
            
            <property name="mapUnderscoreToCamelCase" value="true">property>
        bean>
    property>
        



















    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="heuet.edu.Mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    bean>
    
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager" >
        <property name="dataSource"  ref="dataSource">property>
    bean>
    
    <tx:advice transaction-manager="transactionManager" id="transactionAdvice">
        
        <tx:attributes>
            
            <tx:method name="select*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="delete*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
        tx:attributes>
    tx:advice>
    
    
    <aop:config>
        <aop:pointcut id="txCut" expression="execution(* heuet.edu.Service..*.*(..))"/>
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="txCut"/>
    aop:config>
    
    <tx:annotation-driven transaction-manager="transactionManager" />

beans>

spring-servlet.xml配置


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		//http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <context:component-scan base-package="heuet.edu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
context:component-scan>

    <bean id="resourceViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver ">
        <property name="prefix">
            <value>/value>
        property>
        <property name="suffix">
            <value>.jspvalue>
        property>
    bean>
    
    <mvc:annotation-driven >
        
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" >
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8value>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>
    
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         
        <property name="maxUploadSize" value="54000000000">property>
       
        <property name="defaultEncoding" value="utf-8"/>
    bean>
    
    <mvc:default-servlet-handler/>
    
beans>

spring-context.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		//http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
    <context:annotation-config/>
    
    <context:component-scan base-package="heuet.edu"/>
    
    <aop:aspectj-autoproxy/>
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="registerDefaultFormatters" value="false">property>
        <property name="formatters">
            <set>
                <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory"/>
            set>
        property>
        <property name="formatterRegistrars">
            <set>
                <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
                    <property name="dateFormatter">
                        <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean">
                            <property name="pattern" value="yyyyMMdd">property>
                        bean>
                    property>
                bean>
            set>
        property>
    bean>
beans>

配置web.xml文件



<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Archetype Created Web Applicationdisplay-name>

    
    
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring/applicationContext.xmlparam-value>
  context-param>
  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
  <filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  <servlet>
    <servlet-name>springMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring/spring-servlet.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

配置db.properties

注意如果mysql-connection-java 的版本高的话url后面加上serverTimezone=UTC

driver用com.mysql.cj.jdbc.Driver

低版本的话 用com.mysql.jdbc.Driver

可以都试试

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql?3useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
#jdbc.url=jdbc:mysql://localhost:3306/mysql
jdbc.username=root
jdbc.password=123456

配置log4j.properties

#定义log输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活的指定日志输出格是,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%C]-%m%n

#mybatis显示sql语句配置
log4j/logger.heuet.edu.Mapper=DEBUG
#文件大小制定尺寸的时候产生一个新文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录,这里会放tomcat之下
log4j.appender.File.File=E://idea//log4j//log.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=100MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

配置完毕


做一个简单的登陆功能

  1. 建立一个数据库found 并创建一个student表

  2. 建立两个字段username 和password (自己加一个值 username 123456 )

  3. 然后在Pojo文件夹下创建一个User实体类

    • package heuet.edu.Pojo;
      
      public class User {
          private String username;
          private String password;
      
          public String getUsername() {
              return username;
          }
      
          public void setUsername(String username) {
              this.username = username;
          }
      
          public String getPassword() {
              return password;
          }
      
          public void setPassword(String password) {
              this.password = password;
          }
      }
      
      
  4. 在Controller包下创建一个UserController的类

    • package heuet.edu.Controller;
      
      import heuet.edu.Pojo.User;
      import heuet.edu.Service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import org.springframework.web.bind.annotation.ResponseBody;
      
      @Controller
      @RequestMapping("/user")
      public class UserController {
      
          @Autowired
          private UserService userService;
          @RequestMapping("/login")
      
          public String login( User user,Model model){
              user=userService.get(user);
              model.addAttribute("user",user);
              return "/login/login";
          }
      }
      
      
  5. 在Mapper下创建一个UserMapper的接口

    • package heuet.edu.Mapper;
      
      import heuet.edu.Pojo.User;
      
      public interface UserMapper {
          User select(User user);
      }
      
      
  6. 在resources 中的heuet.edu.Mapper的包下创建一个UserMapper.xml文件

    • 
      
      <mapper namespace="heuet.edu.Mapper.UserMapper">
      <select id="select" parameterType="heuet.edu.Pojo.User" resultType="heuet.edu.Pojo.User">
          select *
          from found.student
          where
          username=#{username}
          and
          password=#{password}
      select>
      
      mapper>
      
  7. 在Service包下创建一个接口UserService

    • package heuet.edu.Service;
      
      
      import heuet.edu.Pojo.User;
      
      public interface UserService {
          User get(User user);
      }
      
      
    • 在Service包中的Impl包下创建一个UserServiceImpl的UserService实现类

      • package heuet.edu.Service.Impl;
        
        import heuet.edu.Mapper.UserMapper;
        import heuet.edu.Pojo.User;
        import heuet.edu.Service.UserService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;
        
        @Service("userService")
        public class UserServiceImpl implements UserService {
        
            @Autowired
            private UserMapper userMapper;
            @Transactional
            public User get(User user) {
        
                return userMapper.select(user);
            }
        }
        
        

至此所有的后台代码都写完了下面只需写两个前端网页

在webapp下创建一个login文件夹 和一个login.jsp

login文件夹下放一个user.jsp

首先login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


账号:
密码:

user.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



欢迎您${user.username}




注意

  • 看一下你JDK包下jre\lib\ext是否有mysql-connector-java-8.0.13.jar 或者是否与所引入的依赖的版本号相同如果不一样的话有可能报错,版本过低也有可能报错。当时就是因为这个问题困扰了我两天。
  • 希望这个能够帮助到你。

你可能感兴趣的:(web开发,ssm框架整合,ssm整合超详细步骤,ssm,ssm框架新手学习整合)