Spring+SpringMVC+MyBatis三大框架整合的SSM

1、三大框架简介

1.1、Spring

Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson创建。简单说,Spring是一个分层的Java SE/EE full-stack(一站式)轻量级开源框架。

1.2、SpringMVC

SpringMVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring框架提供了创建Web应用程序的全功能MVC模块。使用Spring可插入的MVC架构,从而在使用Spring进行Web开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

1.3、MyBatis

MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由Apache software foundation迁移到了Google code,并且改名为MyBatis。2013年11月迁移到GitHub。
iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Object(sDAO)。

2、整合步骤

2.1、数据库

数据库设计如下:
Spring+SpringMVC+MyBatis三大框架整合的SSM_第1张图片

2.2、项目整合步骤

①如下图搭建好基本的架子;
Spring+SpringMVC+MyBatis三大框架整合的SSM_第2张图片
②导入依赖包
pom.xml:

  4.0.0
  com.ssmblog
  SSMBlog
  war
  1.0-SNAPSHOT
  SSMBlog Maven Webapp
  http://maven.apache.org

  

    
    1.8
    1.8
    2.5

    
    UTF-8
    UTF-8

    
    4.3.9.RELEASE
    
    3.4.5

    1.3.1

    
    5.1.43

    
    2.3.23

    
    1.7.7
    1.2.17

    
    1.1.2

    
    1.2.37
    
    2.9.0
    
    2.9.0
    
    1.9.13

    
    1.3.3
    
    2.5

    
    1.10

    
    1.0
  

  
    
      junit
      junit
      4.8.1
      test
    

    
    
      jstl
      jstl
      1.2
    

    
      javax
      javaee-api
      7.0
    

    
    
      org.springframework
      spring-test
      ${spring.version}
    
    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-core
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    
      org.springframework
      spring-beans
      ${spring.version}
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-expression
      ${spring.version}
    
    
      org.springframework
      spring-context-support
      ${spring.version}
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
      org.springframework
      spring-orm
      ${spring.version}
    
    
      org.springframework
      spring-oxm
      ${spring.version}
    
    
      org.springframework
      spring-aspects
      ${spring.version}
    

    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    

    
    
      org.mybatis
      mybatis-spring
      ${mybatis-spring.version}
    

    
    
      mysql
      mysql-connector-java
      ${mysql-driver.version}
    

    
    
      com.alibaba
      druid
      ${druid.version}
    

    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    

    
    
      log4j
      log4j
      ${log4j.version}
    
    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    

    
    
      com.alibaba
      fastjson
      ${fastjson.version}
    
    
      com.fasterxml.jackson.core
      jackson-core
      ${jackson-core.version}
    
    
      com.fasterxml.jackson.core
      jackson-databind
      ${jackson-databind.version}
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      ${jackson-mapper-asl.version}
    

    
    
      commons-fileupload
      commons-fileupload
      ${commons-fileupload.version}
    

    
    
      commons-io
      commons-io
      ${commons-io.version}
    

    
    
      commons-codec
      commons-codec
      ${commons-codec.version}
    

    
    
      javax.annotation
      jsr250-api
      ${jsr250-api.version}
    

    
    
      org.freemarker
      freemarker
      ${freemarker.version}
    

  

  
    SSMBlog

    
    
      
        src/main/resources
        
          **/*.properties
          **/*.xml
        
        true
      
    

    
      
      
      
      
      
      
      
      
      
      

      
      
        org.mortbay.jetty
        maven-jetty-plugin
        6.1.19
        

          
            
              org.mortbay.jetty.Request.maxFormContentSize
              100000000
            
          

          
          
          10
          foo
          9999

          
          
            /
          

          
          automatic
          2

        
        
          
            start-jetty
            pre-integration-test
            
              run
            
            
              0
              true
            
          
          
            stop-jetty
            post-integration-test
            
              stop
            
          
        
      

      
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          ${maven.compiler.source}
          ${maven.compiler.target}
        
      

      
      
        org.apache.maven.plugins
        maven-resources-plugin
        ${maven.resources.version}
        
          
            copy-resources
            package
            
              copy-resources
            
            
              UTF-8
              ${project.build.directory}/lantooease/WEB-INF/
              
              
                
                  /src/main/resources
                  
                    *.xml
                    *.properties
                    *.conf
                  
                
              
            
          
        
      
    

  

③配置applicationContext.xml
applicationContext.xml:



    
    
    
    

    
    
        
    

    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    

    
    
        
        
            
                0
                UTF-8
                0.##########
                yyyy-MM-dd HH:mm:ss
                true
                ignore
            
        
    

    
    
        
    

    
其中jdbc.properties如下配置:
#MySQL数据库驱动程序
jdbc.driverClasss=com.mysql.jdbc.Driver
#MySQL数据库连接
jdbc.url=jdbc:mysql://localhost:3306/SSMBlog?useSSL=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
#MySQL用户名
jdbc.username=root
#MySQL数据库密码
jdbc.password=qwer1234

#定义初始连接数
druid.initialSize=0
#定义最大连接数
druid.maxActive=20
#定义最大空闲
druid.maxIdle=20
#定义最小空闲
druid.minIdle=1
#定义最长等待时间
druid.maxWait=60000
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
druid.timeBetweenEvictionRunsMillis=60000
#配置一个连接在池中最小生存时间,单位毫秒
druid.minEvictableIdleTimeMillis=30000
druid.validationQuery=SELECT 'x'
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
#打开PSCache,并且指定每个连接上PSCache的大小
druid.poolPreparedStatements=false
druid.maxPoolPreparedStatementPerConnectionSize=20
druid.filters=stat

④配置spring-mvc.xml;
spring-mvc.xml:



    
    
    
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    

    
    
        
            
                
                
            
        
    

    
    
        
        
    

    
    
        
            /frontend/
        
    

    
    
        
        
        
    

    
    
        
        
        
        
        
        
    
    
        
            
                
                error_fileupload
            
        
    

⑤配置web.xml;
web.xml:


  SSMBlog

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
  
    log4jConfigLocation
    classpath:log4j.properties
  

  
    log4jRefreshInterval
    60000
  

  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  

  
    encodingFilter
    /*
  

  
    encodingFilter
    *.html
  

  
    encodingFilter
    *.json
  


  
    DruidWebStatFilter
    com.alibaba.druid.support.http.WebStatFilter
    
      exclusions
      *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
    
  
  
    DruidWebStatFilter
    /*
  
  
    DruidStatView
    com.alibaba.druid.support.http.StatViewServlet
  
  
    DruidStatView
    /druid/*
  

  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    org.springframework.web.util.IntrospectorCleanupListener
  


  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
    true
  

  
    SpringMVC
    
    *.html
  

  
    SpringMVC
    *.json
  

  
  
    /index.jsp
    /index.html
  

  
  
    15
  

  
  
    java.lang.Throwable
    /frontend/common/500.html
  

  
    500
    /frontend/common/500.html
  

  
    404
    /frontend/common/404.html
  


其中,log4j.properties配置如下;
log4j.properties:
#日志文件属性
log4j.rootCategory=info,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n
#日式文件路径配置
log4j.appender.R.file=./logFile.log
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c -%m%n

⑥配置映射、实体类和DAO持久层接口;
     以下三者可以自动生成,方法参考: http://blog.csdn.net/qq_34197553/article/details/77745773
映射Mapper.xml:



  
    
    
    
    
    
    
    
  
  
    id, name, address, gender, dob, email, mobile
  
  
  
    delete from contacts
    where id = #{id,jdbcType=INTEGER}
  
  
    insert into contacts (id, name, address, 
      gender, dob, email, mobile
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, 
      #{gender,jdbcType=VARCHAR}, #{dob,jdbcType=DATE}, #{email,jdbcType=VARCHAR}, #{mobile,jdbcType=BIGINT}
      )
  
  
    insert into contacts
    
      
        id,
      
      
        name,
      
      
        address,
      
      
        gender,
      
      
        dob,
      
      
        email,
      
      
        mobile,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{name,jdbcType=VARCHAR},
      
      
        #{address,jdbcType=VARCHAR},
      
      
        #{gender,jdbcType=VARCHAR},
      
      
        #{dob,jdbcType=DATE},
      
      
        #{email,jdbcType=VARCHAR},
      
      
        #{mobile,jdbcType=BIGINT},
      
    
  
  
    update contacts
    
      
        name = #{name,jdbcType=VARCHAR},
      
      
        address = #{address,jdbcType=VARCHAR},
      
      
        gender = #{gender,jdbcType=VARCHAR},
      
      
        dob = #{dob,jdbcType=DATE},
      
      
        email = #{email,jdbcType=VARCHAR},
      
      
        mobile = #{mobile,jdbcType=BIGINT},
      
    
    where id = #{id,jdbcType=INTEGER}
  
  
    update contacts
    set name = #{name,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=VARCHAR},
      dob = #{dob,jdbcType=DATE},
      email = #{email,jdbcType=VARCHAR},
      mobile = #{mobile,jdbcType=BIGINT}
    where id = #{id,jdbcType=INTEGER}
  
  
  
实体类.java:
package com.ssmblog.entity;

import java.util.Date;

public class Contacts {
    private Integer id;

    private String name;

    private String address;

    private String gender;

    private Date dob;

    private String email;

    private Long mobile;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender == null ? null : gender.trim();
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public Long getMobile() {
        return mobile;
    }

    public void setMobile(Long mobile) {
        this.mobile = mobile;
    }
}
DAO持久化层接口.java:
package com.ssmblog.dao;

import com.ssmblog.entity.Contacts;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ContactsMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Contacts record);

    int insertSelective(Contacts record);

    Contacts selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Contacts record);

    int updateByPrimaryKey(Contacts record);

    List findAllContacts();

    List findByContactName(String name);
}

⑦Service服务层;
ContactsServiceI.java:
package com.ssmblog.service;

import com.ssmblog.entity.Contacts;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ContactsServiceI {

    List findAllContacts();

    Contacts selectByPrimaryKey(int id);

    void newContact(Contacts contacts);

    void deleteContact(int id);

    void updateContactByPrimaryKey(Contacts contacts);

    List findByContactName(String name);
}
ContactsServiceImpl.java:
package com.ssmblog.service.impl;

import com.ssmblog.dao.ContactsMapper;
import com.ssmblog.entity.Contacts;
import com.ssmblog.service.ContactsServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("contactsService")
@Transactional
public class ContactsServiceImpl implements ContactsServiceI{

    @Autowired
    private ContactsMapper contactsMapper;

    public void setContactsMapper(ContactsMapper contactsMapper) {
        this.contactsMapper = contactsMapper;
    }

    @Override
    public List findAllContacts() {
        return this.contactsMapper.findAllContacts();
    }

    @Override
    public Contacts selectByPrimaryKey(int id) {
        return this.contactsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void newContact(Contacts contacts) {
        this.contactsMapper.insert(contacts);
    }

    @Override
    public void deleteContact(int id) {
        this.contactsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateContactByPrimaryKey(Contacts contacts) {
        this.contactsMapper.updateByPrimaryKey(contacts);
    }

    @Override
    public List findByContactName(String name) {
        return this.contactsMapper.findByContactName(name);
    }
}

⑧Controller层;
SSMBlogController.java:
package com.ssmblog.controller;

import com.ssmblog.entity.Contacts;
import com.ssmblog.service.ContactsServiceI;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/")
public class SSMBlogController {
    @Autowired
    private ContactsServiceI contactsServiceI;

    public void setContactsServiceI(ContactsServiceI contactsServiceI) {
        this.contactsServiceI = contactsServiceI;
    }

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView indexPage(){
        ModelAndView mav = new ModelAndView("index");
        List contacts = this.contactsServiceI.findAllContacts();
        mav.addObject("contacts",contacts);
        return mav;
    }

    @RequestMapping(value = "save", method = RequestMethod.GET)
    public ModelAndView newContactPage(){
        ModelAndView mav = new ModelAndView("newContact");
        return mav;
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String newContact(@RequestParam("dateOfb")String dob, Contacts contacts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dob);
        contacts.setDob(date);
        this.contactsServiceI.newContact(contacts);
        return "redirect:index.html";
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public String deleteContact(int id){
        this.contactsServiceI.deleteContact(id);
        return "redirect:index.html";
    }

    @RequestMapping(value = "update", method = RequestMethod.GET)
    public ModelAndView updateContactPage(int id){
        ModelAndView mav = new ModelAndView("editContact");
        Contacts contacts = this.contactsServiceI.selectByPrimaryKey(id);
        mav.addObject("editContact", contacts);
        return mav;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String updateContact(@RequestParam("dateOfb")String dob, Contacts contacts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dob);
        contacts.setDob(date);
        this.contactsServiceI.updateContactByPrimaryKey(contacts);
        return "redirect:index.html";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public ModelAndView findByContactName(String name){
        ModelAndView mav = new ModelAndView("index");
        String contactName = "%" + name + "%";
        List contacts = this.contactsServiceI.findByContactName(contactName);
        mav.addObject("contacts",contacts);
        return mav;
    }

    @RequestMapping(value = "findAllContacts", method = RequestMethod.GET)
    @ResponseBody
    public List findAllContacts(){
        return this.contactsServiceI.findAllContacts();
    }
}

⑨整合完毕,在Tomcat服务器上运行,若发现浏览器上查看得到如下图所示的情况,则代表整合成功。
Spring+SpringMVC+MyBatis三大框架整合的SSM_第3张图片


3、项目源码下载

提供了源码下载地址:
源码地址

你可能感兴趣的:(IntelliJ,IDEA,Spring,SpringMVC,GitHub,MyBatis,SSM)