SSM整合


项目源码:https://github.com/zhongyushi-git/spring-collection.git。下载代码后,示例代码在ssmSystem文件夹下。 

1.创建项目

1.1新建一个maven的web项目

 点击下一步,填写相关信息。

1.2添加java和resources目录

在main目录下新建java目录和resources目录,并把他们设置为资源目录

2.在pom.xml导入依赖


        4.3.0.RELEASE
        3.2.6
        1.7.7
        1.2.17
        1.16.18
        1.2.9
        5.1.32
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            1.2.2
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.22
        
        
        
            com.alibaba
            druid
            1.0.9
        
        
        
            jstl
            jstl
            1.2
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet
            jsp-api
            2.0
        
        
        
            org.projectlombok
            lombok
            ${lombok.version}
        
        
        
            com.alibaba
            fastjson
            ${fastjson.version}
        
        
            junit
            junit
            4.12
            compile
        
    

3.配置mybatis

3.1在resources目录下新建mybatis目录

3.2在mybatis目录下新建文件SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
        
        "com.zys.ssm.entity"/>
    

4.配置数据源

4.1在resources目录下新建properties目录

4.2在properties目录下新建文件db.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2020?characterEncoding=utf-8
jdbc.username=root
jdbc.password=zys123456

4.3在properties目录下新建文件log4j.properties文件

### 设置当前日志级别 ###
log4j.rootLogger = stdout,D,E
#debug,
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=C://logs/log.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = C://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=C://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =C://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

5.配置spring

5.1在resources目录下新建spring目录

5.2在spring目录下新建文件applicationContext-dao.xml文件

"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">



"classpath:properties/db.properties"/>

"dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
       "driverClassName" value="${jdbc.driver}"/>
        "url" value="${jdbc.url}"/>
        "username" value="${jdbc.username}"/>
        "password" value="${jdbc.password}"/>
        "maxActive" value="10"/>
        "maxIdle" value="5"/>
    


"sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    
    "dataSource" ref="dataSource"/>
    
    "configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    "mapperLocations" value="classpath:mapper/*.xml"/>

 class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     "basePackage" value="com.zys.ssm.dao"/>
     "sqlSessionFactoryBeanName" value="sqlSessionFactory">
 


5.3在spring目录下新建文件applicationContext-service.xml文件

"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">

base-package="com.zys.ssm.service">

5.4在spring目录下新建文件applicationContext-transaction.xml文件

"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">


"transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    "dataSource" ref="dataSource"/>

"transactionManager"/>

5.5在spring目录下新建文件springmvc.xml文件

"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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-3.1.xsd">

    
    base-package="com.zys.ssm.controller"/>


    
    
        "true">
            class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                "supportedMediaTypes" value="application/json"/>
                "features">
                    
                        WriteMapNullValue
                        WriteDateUseDateFormat
                    
                
            
        
    
    default-servlet-handler/>

6.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
"http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    
        login.html
    

    
    
        charsetEncodingFilter
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        
            CharsetEncoding
            UTF-8
        
    
    
    
        charsetEncodingFilter
        /*
    

    
    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
    
    
        DispatcherServlet
        /*
    

7.mapper文件的配置

在resourcesm目录下创建mapper文件夹,所有的mapper.xml文件将放在这里。

8.添加tomcat服务器

前提是本地已安装tomcat server,若没有请先安装。

8.1选择环境配置

8.2选择tomcat服务器

8.3点击+号选择本地的tomcat server

8.4选择要启动的项目类型

 

8.5选择启动的项目

选择后一般会带项目名加war进行访问,可以直接修改为项目名

8.6选择tomcat服务器

 然后点击确定即可。后面直接运行这个服务就能够访问资源。

9.在webapp下新建页面

新建login.html和view/index.html,还需要加入bootstrap的文件,详见源码。

10.数据库执行脚本

create database db2020;
use db2020;

CREATE TABLE `user`  (
  `id` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'id',
  `name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '姓名',
  `phone` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '电话',
  `password` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

INSERT INTO `user` VALUES ('admin', '曾漫华', '123444', '123444');
INSERT INTO `user` VALUES ('likai', '李凯1', '1234', '123');
INSERT INTO `user` VALUES ('liming', '李明', '101', '12345');
INSERT INTO `user` VALUES ('lisi', '李四', '111', '111');
INSERT INTO `user` VALUES ('wangwu', '王五', '1111', '1111');
INSERT INTO `user` VALUES ('xixixi', '嘻嘻嘻', '1134444', '12345');
INSERT INTO `user` VALUES ('zhangmin', '张敏', '123', '123');
INSERT INTO `user` VALUES ('zhangsan', '张三', '112', '112');

 数据库执行完成后启动项目,访问http://localhost:8080/ssmSystem/login.html使用正确的用户名和密码即可登录,登录后会跳转到首页,对用户的信息进行基本的增删改查。

相关