Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)

欢迎访问个人博客:打开链接

项目下载链接:打开

1. SSM框架整合环境

jdk1.8   +   tomcat8.5  + myeclipse2014  
spring4.3.12  +  mybatis3.4.6  + springmvc4.3.12

2. 项目总体概况

Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)_第1张图片

3. 所需jar包

Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)_第2张图片Spring4.3.12+mybatis3.4.6+Springmvc4.3.2整合(SSM整合)_第3张图片

4. 配置文件

  • (1)mybatisConfig.xml


<configuration>

    <settings>
        <setting name="useGeneratedKeys" value="true"/>
    settings>
    <plugins>
        
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>

            
            <property name="offsetAsPageNum" value="true"/>

            
            <property name="rowBoundsWithCount" value="true"/>
            
            
            <property name="pageSizeZero" value="true"/>

            <property name="reasonable" value="false"/>

        plugin>
    plugins>
configuration>
  • (2)applicationContext-dao.xml

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


        <context:property-placeholder location="classpath:jdbc.properties"/>

        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
            <property name="driverClassName" value="${jdbc_driver}" />
            <property name="maxActive" value="10" />
            <property name="minIdle" value="5" />

        bean>

        <bean name="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref = "dataSource">property>

            <property name="configLocation" value="classpath:mybatis/mybatisConfig.xml">property>
            
            <property name="mapperLocations" value="classpath:cn/jasonjing/mapper/*.xml" />
        bean>

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.jasonjing.dao">property>
            <property name="sqlSessionFactoryBeanName" value="sessionFactory">property>
        bean>
beans>
  • (3)applicationContext-service.xml

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

    <context:component-scan base-package="cn.jasonjing.service.impl">context:component-scan>


    <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">property>
    bean>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
beans>
  • (4)springmvc.xml

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


    <context:component-scan base-package="cn.jasonjing.controller">context:component-scan>

    <mvc:annotation-driven>mvc:annotation-driven>

    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/">property>
        <property name="suffix" value=".jsp">property>
    bean>

    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/WEB-INF/jsp/" mapping="/jsp/**"/>
beans>
  • (5)jdbc.properties
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc\:mysql\://localhost\:3306/ssm?useUnicode\=true&characterEncoding\=UTF-8
jdbc_username=root
jdbc_password=root
  • (6)log4j.properties
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=info,console,FILE

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=info
log4j.appender.console.layout=org.apache.log4j.PatternLayout  
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] -%F(%L) -%m%n  
  • (7) web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSMdisplay-name>
  <servlet>
    <servlet-name>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>  
                classpath:spring/springmvc.xml 
            param-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring/applicationContext-*.xmlparam-value>
  context-param>
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
web-app>

5. 面向接口编程三层架构文件

  • (1)UserController
package cn.jasonjing.controller;

import java.util.List;

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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jasonjing.domain.User;
import cn.jasonjing.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUsers")
    @ResponseBody
    public List getAllUsers(){
        List userList = userService.findAllUsers();
        return userList;
    }

    @RequestMapping("/user")
    public ModelAndView getUser(){
        ModelAndView mv = new ModelAndView();


        List userList = userService.findAllUsers();
        mv.addObject("user", userList.get(0));
        return mv;
    }
}
  • (2) UserService
package cn.jasonjing.service;

import java.util.List;

import cn.jasonjing.domain.User;

public interface UserService {

    public List findAllUsers();
}
  • (3)UserServiceImpl
package cn.jasonjing.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jasonjing.dao.UserMapper;
import cn.jasonjing.domain.User;
import cn.jasonjing.service.UserService;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List findAllUsers() {
        List userList = userMapper.findAllUsers();
        return userList;
    }

}
  • (4)UserMapper
package cn.jasonjing.dao;

import java.util.List;

import cn.jasonjing.domain.User;

public interface UserMapper {

    public List findAllUsers();

}
  • (5)UserMapper.xml

<mapper namespace="cn.jasonjing.dao.UserMapper">
    <select  id="findAllUsers"  resultType="cn.jasonjing.domain.User">
        select * from tb_user
    select>
mapper>
  • (6)User
package cn.jasonjing.domain;

public class User {

    private int id;
    private String username;
    private String password;
    private String phone;
    private String email;
    private String created;
    private String updated;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getCreated() {
        return created;
    }
    public void setCreated(String created) {
        this.created = created;
    }
    public String getUpdated() {
        return updated;
    }
    public void setUpdated(String updated) {
        this.updated = updated;
    }

}

6.前端显示

这里只显示了一点点内容,属于仅仅测试一下
http://localhost:8080/SSM/user

user.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting pagetitle>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
  head>

  <body>
    This is my JSP page. <br>
    ${user.id }<br>
    ${user.username }<br>
    ${user.password }<br>



  body>
html>

测试json数据
http://localhost:8080/SSM/getUsers

项目下载链接:打开

你可能感兴趣的:(Spring,Mybatis,SpringMVC,SSM框架,JavaWeb,MySQL,Java基础)