一步一步带你搭建后台管理系统之SSM框架整合

一步一步带你搭建后台管理系统之SSM框架整合

README:本文后端采用 SpringMvc、MyBatis、Spring
由于mybatis最新包对pagehelper会有错误,所以本文未采用最新的依赖。

pom.xml:



  4.0.0
  com.wrpys.ssm
  SSM
  war
  1.0-SNAPSHOT
  SSM

  
  
    UTF-8
    UTF-8
  

  
  

    
    
      org.springframework
      spring-core
      4.3.5.RELEASE
    
    
      org.springframework
      spring-aop
      4.3.5.RELEASE
    
    
      org.springframework
      spring-orm
      4.3.5.RELEASE
    
    
      org.springframework
      spring-web
      4.3.5.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.3.5.RELEASE
    
    
      org.springframework
      spring-tx
      4.3.5.RELEASE
    
    
      org.springframework
      spring-test
      4.3.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.3.5.RELEASE
    
    
      org.springframework
      spring-mock
      2.0.8
    
    
      org.springframework
      spring-context
      4.3.5.RELEASE
    
    
      org.springframework
      spring-expression
      4.3.5.RELEASE
    
    
      org.springframework
      spring-context-support
      4.3.5.RELEASE
    

    
    
      org.mybatis
      mybatis-spring
      1.3.1
    

    
    
      org.mybatis
      mybatis
      3.2.8
    
    
    
      com.github.pagehelper
      pagehelper
      3.7.3
    
    
      com.github.jsqlparser
      jsqlparser
      0.9.1
    

    
    
      javax.servlet
      servlet-api
      3.0-alpha-1
    
    
      javax.servlet
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
    
      org.aspectj
      aspectjrt
      1.8.10
    
    
      org.aspectj
      aspectjweaver
      1.8.10
    
    
      commons-lang
      commons-lang
      2.6
    
    
      dom4j
      dom4j
      1.6.1
    

    
    
      mysql
      mysql-connector-java
      5.1.40
    
    
    
      commons-dbcp
      commons-dbcp
      1.4
    

    
    
      com.alibaba
      fastjson
      1.2.23
    
    
      xml-apis
      xml-apis
      2.0.2
    
    
      commons-net
      commons-net
      3.5
    

    
    
      org.slf4j
      slf4j-api
      1.7.22
    
    
      org.slf4j
      slf4j-log4j12
      1.7.22
    
    
      log4j
      log4j
      1.2.17
    

    
    
      junit
      junit
      4.12
      test
    

  

  
  
    
      
        org.apache.maven.plugins
        maven-war-plugin
        2.4
        
          SSM
          
          
            
              src/main/resources/resources/config
              WEB-INF/classes/resources/config
              true
            
            
              src/main/java
              
                **/*.xml
              
              WEB-INF/classes
            
          
        
      

      
        org.apache.maven.plugins
        maven-resources-plugin
        2.4.3
        
          UTF-8
        
      
      
        org.apache.maven.plugins
        maven-compiler-plugin
      
      
        org.apache.felix
        maven-bundle-plugin
        true
        true
      
      
        org.apache.maven.plugins
        maven-surefire-plugin
        
          true
        
      
      
        org.apache.maven.plugins
        maven-site-plugin
        
        3.3
        
          zh_CN
        
      

    
  



配置文件:

一步一步带你搭建后台管理系统之SSM框架整合_第1张图片

jdbc.properties:

#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost/ssm_demo
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=10
jdbc.maxIdle=10
jdbc.maxWait=5000
jdbc.initialSize=5
jdbc.validationQuery=SELECT 1
jdbc.testWhileIdle=true
jdbc.testOnBorrow=true
jdbc.timeBetweenEvictionRunsMillis=3600000
jdbc.numTestsPerEvictionRun=50
jdbc.minEvictableIdleTimeMillis=120000
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=6000000

log4j.properties:

log4j.rootLogger=DEBUG,Console,FILE
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#File Appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=C:/soft/logs/SSM.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] [%l] [%t] [%-5p] : %m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO

applicationContext.xml:




    
    
    
    


spring-mybatis.xml:




    
        
            
                classpath:resources/config/jdbc.properties
            
        
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
            
                
                    
                        
                            dialect=mysql
                        
                    
                
            
        
    

    
        
        
    

    
    
        
    

    
    
        
            
            
            
            
            
            
            
            
            
            
            
        
    

    
    
        
        
        
        
    

    
        
    

    
        
    


rest-servlet.xml:




    
    

    
    

    
    
        
            
            
        
    
    
        
        
            
                text/plain;charset=UTF-8
            
        
    
    
        
            
                application/json;charset=UTF-8
                text/html;charset=UTF-8
            
        
        
            
                WriteMapNullValue
                QuoteFieldNames
                DisableCircularReferenceDetect
            
        
    

    
    
        
        
        
    


web.xml:



  
    webAppRootKey
    WeddingPhoto.root
  
  
    contextConfigLocation
    
      classpath:/resources/applicationContext.xml
    
  

  
    log4jConfigLocation
    classpath:/resources/config/log4j.properties
  
  
    org.springframework.web.util.Log4jConfigListener
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    CharacterEncodingFilter
    /*
    REQUEST
    FORWARD
  

  
    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:resources/rest-servlet.xml
    
  
  
    dispatcher
    
    /
  



到此SSM框架整合完毕。接下来新建一张表进行基本增删改查的测试。
用户表(t_user):
CREATE TABLE `t_user` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(64) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

在model包下新建类实体类User:
package com.wrpys.ssm.model;

/**
 * @author wrp
 * @Description com.wrpys.ssm.model.User
 * @Date 2017/1/13
 */
public class User {

    private Integer userId;

    private String userName;

    private Integer age;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
在dao包下新建接口UserDao;
package com.wrpys.ssm.dao;

import com.wrpys.ssm.model.User;

import java.util.List;

/**
 * @author wrp
 * @Description com.wrpys.ssm.dao.UserDao
 * @Date 2017/1/13
 */
public interface UserDao {

    List findUserByParams(User user);

    void createUser(User user);

    void deleteUser(User user);

    void batchDeleteUser(List userIds);

    void updateUser(User user);

}
在model.mybatis包下新建mapper文件User.xml:




    
        
            
                and user_id = #{userId}
            
            
                and user_name = #{userName}
            
            
                and age = #{age}
            
        
    

    
        INSERT INTO t_user(user_name, age) VALUES (#{userName, jdbcType=VARCHAR}, #{age, jdbcType=INTEGER})
    

    
        DELETE FROM t_user
        
    

    
        DELETE FROM t_user
        where user_id in
        
            #{userId}
        
    

    
      UPDATE t_user
      
          
              user_name = #{userName, jdbcType=VARCHAR},
          
          
              age = #{age, jdbcType=INTEGER},
          
      
      where user_id = #{userId}
    

    


在service新建接口UserService:
package com.wrpys.ssm.service;

import com.wrpys.ssm.common.PageInfo;
import com.wrpys.ssm.model.User;

import java.util.List;

/**
 * @author wrp
 * @Description com.wrpys.ssm.service.UserService
 * @Date 2017/1/13
 */
public interface UserService {

    void findUser(PageInfo pageInfo, User user);

    void createUser(User user);

    void deleteUser(User user);

    void batchDeleteUser(List userIds);

    void updateUser(User user);

}
在service.impl包下新建类UserServiceImpl:
package com.wrpys.ssm.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wrpys.ssm.common.PageInfo;
import com.wrpys.ssm.dao.UserDao;
import com.wrpys.ssm.model.User;
import com.wrpys.ssm.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wrp
 * @Description com.wrpys.ssm.service.impl.UserServiceImpl
 * @Date 2017/1/13
 */
@Service
public class UserServiceImpl implements UserService {

    Log logger = LogFactory.getLog(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public void findUser(PageInfo pageInfo, User user) {
        logger.info("findUser pageInfo:"+ JSON.toJSONString(pageInfo));
        Page page = PageHelper.startPage(pageInfo.getPageNumber(), pageInfo.getPageSize());
        pageInfo.setRows(userDao.findUserByParams(user));
        pageInfo.setTotal(page.getTotal());
    }

    @Override
    public void createUser(User user) {
        userDao.createUser(user);
    }

    @Override
    public void deleteUser(User user) {
        userDao.deleteUser(user);
    }

    @Override
    public void batchDeleteUser(List userIds) {
        userDao.batchDeleteUser(userIds);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

}
在ctroller包下先建类UserCtr:
package com.wrpys.ssm.controller;

import com.wrpys.ssm.common.Constans;
import com.wrpys.ssm.common.PageInfo;
import com.wrpys.ssm.common.ResultBean;
import com.wrpys.ssm.model.User;
import com.wrpys.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wrp
 * @Description com.wrpys.ssm.controller.UserCtr
 * @Date 2017/1/13
 */
@Controller
@RequestMapping(value = "user")
public class UserCtr {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public PageInfo findUser(PageInfo pageInfo, User user) {
        userService.findUser(pageInfo, user);
        return pageInfo;
    }

    @RequestMapping(value = "add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultBean add(User user) {
        ResultBean rb = new ResultBean();
        userService.createUser(user);
        rb.setFlag(Constans.SUCCESS);
        return rb;
    }

    @RequestMapping(value = "delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultBean delete(User user) {
        ResultBean rb = new ResultBean();
        userService.deleteUser(user);
        rb.setFlag(Constans.SUCCESS);
        return rb;
    }

    @RequestMapping(value = "batchDeleteUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultBean batchDeleteUser(@RequestParam(value = "userIds") List userIds) {
        ResultBean rb = new ResultBean();
        userService.batchDeleteUser(userIds);
        rb.setFlag(Constans.SUCCESS);
        return rb;
    }

    @RequestMapping(value = "update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultBean update(User user) {
        ResultBean rb = new ResultBean();
        userService.updateUser(user);
        rb.setFlag(Constans.SUCCESS);
        return rb;
    }

}
用到的一些公共类:
静态变量Constans:
package com.wrpys.ssm.common;

/**
 * @author wrp
 * @Description com.wrpys.ssm.common.Common
 * @Date 2017/1/16
 */
public class Constans {

    public final static Integer SUCCESS = 1;
    public final static Integer FAIL = 0;


}
分页信息PageInfo:
package com.wrpys.ssm.common;

import java.util.List;

/**
 * @author wrp
 * @Description com.wrpys.ssm.common.PageInfo
 * @Date 2017/1/13
 */
public class PageInfo {

    private Integer pageNumber;

    private Integer pageSize;

    private Long total;

    private List rows;

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }
}
返回结果ResultBean:
package com.wrpys.ssm.common;

/**
 * @author wrp
 * @Description com.wrpys.ssm.common.ResultBean
 * @Date 2017/1/16
 */
public class ResultBean {

    private Integer flag;

    private String msg;

    private Object data;

    public Integer getFlag() {
        return flag;
    }

    public void setFlag(Integer flag) {
        this.flag = flag;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

到此t_user表的基本操作已经写完,启动项目,进行测试。
新增:
一步一步带你搭建后台管理系统之SSM框架整合_第2张图片
一步一步带你搭建后台管理系统之SSM框架整合_第3张图片
修改:
一步一步带你搭建后台管理系统之SSM框架整合_第4张图片

一步一步带你搭建后台管理系统之SSM框架整合_第5张图片
分页查询:
测试数据:
一步一步带你搭建后台管理系统之SSM框架整合_第6张图片
结果:


删除:
一步一步带你搭建后台管理系统之SSM框架整合_第7张图片
一步一步带你搭建后台管理系统之SSM框架整合_第8张图片

到此对增删改查的接口测试完毕。后端的SSM框架搭建完成。过程可能会出现各种个样的报错,希望耐心查询资料或者留言,有时间会过来帮忙解答。接下来篇章将进行前端插件的引入及其封装。
源码地址:http://pan.baidu.com/s/1eSEAsuu

你可能感兴趣的:(JAVA,WEB,maven,mybatis,spring,spring,mvc,intellij,idea)