Spring-Mybatis-SpringMVC三大框架整合

我们直接切人正题,不多逼逼赖赖

第一步:依赖,一下的这些基本上是SSM整合的全部依赖


    
      org.springframework
      spring-beans
      5.1.5.RELEASE
    
    
    
      org.springframework
      spring-context
      5.1.5.RELEASE
    
    
      log4j
      log4j
      1.2.16
    
    
      commons-logging
      commons-logging
      1.2
    
    
    
      org.aspectj
      aspectjweaver
      1.9.2
    
    
    
      org.springframework
      spring-jdbc
      5.1.5.RELEASE
    
    
    
      mysql
      mysql-connector-java
      5.1.38
    
    
      org.mybatis
      mybatis
      3.4.6
    
    
      org.mybatis.generator
      mybatis-generator-core
      1.3.2
    
    
    
      net.sf.ehcache
      ehcache
      1.2.3
    
    
    
      org.mybatis
      mybatis-ehcache
      1.0.0
    
    
    
      org.mybatis
      mybatis-spring
      1.3.2
    
    
    
      com.alibaba
      druid
      1.1.10
    
    
      org.slf4j
      slf4j-api
      1.7.25
    


    
      org.slf4j
      slf4j-log4j12
      1.7.25
      test
    
    
    
      com.alibaba
      fastjson
      1.2.47
    

    
    
      org.springframework
      spring-webmvc
      5.1.5.RELEASE
    
    
      org.springframework
      spring-web
      5.1.5.RELEASE
    
    
      javaee
      javaee-api
      5
    
    
    
      com.fasterxml.jackson.core
      jackson-core
      2.9.8
    
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.8
    
    
    
      commons-io
      commons-io
      2.4
    
    
    
      commons-fileupload
      commons-fileupload
      1.3.1
    
    
      jstl
      jstl
      1.2
    

第二步:com.ssm.entity层

public class Smbms_User implements Serializable {
    private Integer id;

    private String usercode;

    private String username;

    private String userpassword;


    public Integer getId() {
        return id;
    }

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

    public String getUsercode() {
        return usercode;
    }

    public void setUsercode(String usercode) {
        this.usercode = usercode == null ? null : usercode.trim();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getUserpassword() {
        return userpassword;
    }

    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword == null ? null : userpassword.trim();
    }


}

第三步:com.ssm.dao层

@Repository
public interface Smbms_UserMapper {


    @Select("select * from smbms_user where userCode=#{user_code} and userPassword=#{user_password}")
    public Smbms_User login(@Param("user_code") String user_code, @Param("user_password") String user_password);


}

 

第四步:com.ssm.dao层中的.xml文件

"1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
namespace="com.ssm.dao.Smbms_UserMapper" >
  "BaseResultMap" type="com.ssm.entity.Smbms_User" >
    "id" property="id" jdbcType="BIGINT" />
    "userCode" property="usercode" jdbcType="VARCHAR" />
    "userName" property="username" jdbcType="VARCHAR" />
    "userPassword" property="userpassword" jdbcType="VARCHAR" />
    "gender" property="gender" jdbcType="INTEGER" />
    "birthday" property="birthday" jdbcType="DATE" />
    "phone" property="phone" jdbcType="VARCHAR" />
    "address" property="address" jdbcType="VARCHAR" />
    "userRole" property="userrole" jdbcType="INTEGER" />
    "createdBy" property="createdby" jdbcType="BIGINT" />
    "creationDate" property="creationdate" jdbcType="TIMESTAMP" />
    "modifyBy" property="modifyby" jdbcType="BIGINT" />
    "modifyDate" property="modifydate" jdbcType="TIMESTAMP" />
  

 

第五步:com.ssm.service层

public interface Smbms_UserService {

    public Smbms_User login(String user_code, String user_password);
}

 

第六步:com.ssm.service.impl层

@Service("smbms_UserService")
public class Smbms_UserServcieImpl implements Smbms_UserService {
    //植入Dao层对象
    @Resource
    private Smbms_UserMapper smbms_userMapper;
    //事务
   /* @Transactional(isolation=Isolation.DEFAULT)*/
    @Override
    public Smbms_User login(String user_code, String user_password) {
        return smbms_userMapper.login(user_code,user_password);
    }
}

 

第七步:com.ssm.controller层

@Controller
@RequestMapping("/user")
public class Smbms_UserController {
    //植入Service层分对象
    @Resource
    private Smbms_UserService smbms_userService;

    @RequestMapping("/login")
    public String doLogin(Smbms_User smbms_user, HttpSession session){
        Smbms_User user = smbms_userService.login(smbms_user.getUsercode(), smbms_user.getUserpassword());
        if(user!=null){
            session.setAttribute("user",user);
            return "welcome";
        }
        return "login";
    }
}

 

第八步:resourdes中的mybatis-config.xml文件

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




    
        
        "logImpl" value="LOG4J"/>

    

    
    
        
        "com.ssm.entity"/>
    


    
    
        
       "com.ssm.dao"/>
    

 

第九步:resourdes中的jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

 

第十步:resourdes中的applicationContext.xml文件

"1.0" encoding="UTF-8"?>
"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" xmlns:tx="http://www.springframework.org/schema/tx"
       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 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">
    
    base-package="com.ssm"/>
    
    
    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        "prefix" value="/"/>
        "suffix" value=".jsp"/>
    
    
    default-servlet-handler/>
    
    "classpath:jdbc.properties"/>
    
    "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        "driverClassName" value="${jdbc.driver}"/>
        "url" value="${jdbc.url}"/>
        "username" value="${jdbc.username}"/>
        "password" value="${jdbc.password}"/>
    
    
    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        "dataSource" ref="dataSource"/>
        
        "configLocation" value="classpath:mybatis-config.xml"/>
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        "basePackage" value="com.ssm.dao"/>
    
    
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        "dataSource" ref="dataSource"/>
    

 

第十一步:WEB-INF下的web.xml文件

app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


  Archetype Created Web Application
  
    characterEncodingFilter
    class>org.springframework.web.filter.CharacterEncodingFilterclass>
    
    
      encoding
      UTF-8
    
    
    
      forceEncoding
      true
    
  
  
    characterEncodingFilter
    /*
  
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:applicationContext.xml
    
    1
  
  
    springmvc
    /
  

 

第十二步:登录页面的搭建

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


    登陆


    
"/user/login" method="post"> 用户名:"text" name="usercode"/> 密码:"password" name="userpassword"/> "submit" value="登陆"/>

登录成功的页面

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


    欢迎


    欢迎光临:${user.username}

以上都是对SSM整合的讲解,具体顺序可以自己搭配,希望对大家有帮助,如果有什么看不懂的地方,可以评论,我一定会第一时间帮助大家解答疑惑,谢谢

你可能感兴趣的:(Spring-Mybatis-SpringMVC三大框架整合)