栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM框架整合详细案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM框架整合详细案例

目录描述
    • 一、创建web项目(使用idea maven)
    • 二、使用maven添加依赖
      • 1、在pom.xml中添加项目所需的所有依赖
      • 2、添加tomcat运行驱动
    • 三、建立整体项目架构
    • 四、搭建mybatis
      • 1、编写mybatis-config.xml
      • 2、编写数据库连接配置文件
      • 3、编写spring-mybatis.xml
      • 4、编写pojo和dao层
      • 5、编写映射文件
    • 五、搭建spring
      • 1、spring-context.xml
    • 六、DAO层测试
    • 七、搭建SpringMVC
      • 1、编写spring-mvc.xml
      • 2、编写Service层
      • 3、测试Service层
      • 4、编写web.xml
      • 5、编写Controller层
    • 八、编写页面
    • 九、运行
      • 1、配置tomcat
      • 2、运行

一、创建web项目(使用idea maven)

都会

二、使用maven添加依赖 1、在pom.xml中添加项目所需的所有依赖
     
      mysql
      mysql-connector-java
      5.1.47
    
    
    
      org.mybatis
      mybatis
      3.4.6
    

    
    
      org.projectlombok
      lombok
      1.18.12
      provided
    
    
    
      org.springframework
      spring-context
      5.3.13
    
    
      org.springframework
      spring-aspects
      5.3.13
    
    
    
      org.springframework
      spring-jdbc
      5.3.13
    
    
    
      org.mybatis
      mybatis-spring
      2.0.6
    
    
    
    
      com.alibaba
      druid
      1.1.10
    
    
    
      org.springframework
      spring-test
      5.3.13
    
    
    
      junit
      junit
      4.13.1
      test
    
    
    
      javax.servlet
      jsp-api
      2.0
      provided
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
    
      com.github.pagehelper
      pagehelper
      5.1.10
    

    
      com.github.3tty0n
      jwt-scala_2.12
      1.3.0
    

    
      com.github.3tty0n
      jwt-scala_2.11
      1.3.0
    
    
    
    
      org.springframework
      spring-webmvc
      5.3.13
    
    
    
      org.springframework
      spring-web
      5.3.13
    
    
    
      com.fasterxml.jackson.core
        jackson-databind
      2.13.0
    
    
      org.junit.jupiter
      junit-jupiter
      RELEASE
      compile
    
    
      junit
      junit
      4.13.1
      compile
    
  
2、添加tomcat运行驱动
      
        
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.1
      
三、建立整体项目架构

四、搭建mybatis 1、编写mybatis-config.xml

空文件






其实可以省略。

2、编写数据库连接配置文件
druid.url=jdbc:mysql://localhost:3306/biology?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
druid.driver=com.mysql.jdbc.Driver
druid.username=root
druid.password=123456
druid.pool.init=2
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000
3、编写spring-mybatis.xml



    
    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
         
七、搭建SpringMVC 
1、编写spring-mvc.xml 


    
    


2、编写Service层

UserService接口:

package com.atmae.service;

import com.atmae.pojo.User;

public interface UserService {
    public User checkLogin(String userName, String UserPassword);
}

UserServiceImpl实现类:

package com.atmae.service.impl;

import com.atmae.dao.UserDao;
import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    
    @Transactional
    public User checkLogin(String userName, String UserPassword) {
        User user = userDao.queryUserByName(userName);
        if (user!=null&&user.getUserPassword().equals(UserPassword)) {
            return user;
        } else {
            return null;
        }
    }
}
3、测试Service层
package com.atmae.test;

import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml", "classpath:spring-mybatis.xml", "classpath:spring-mvc.xml"})
public class UserServiceTest {

    @Resource
    UserService userService;
    @Test
    public void checkLogin() {
        User user = userService.checkLogin("Admin", "666666");
        assertNotNull(user);
    }
}
4、编写web.xml





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


        
5、编写Controller层

userController:

package com.atmae.controller;

import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/login")
    public String login(String userName, String userPassword, HttpServletRequest request){
        User user=userService.checkLogin(userName,userPassword);
        if (user==null){
            request.getSession().setAttribute("tips","用户名或者密码错误!");
            return "/login.jsp";
        }else{
            request.getSession().setAttribute("user",user);
            return "/index.jsp";
        }
    }
}
八、编写页面
<%--
  Created by IntelliJ IDEA.
  User: Mae
  Date: 2021/12/12
  Time: 11:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Index


${user.userName}

<%--
  Created by IntelliJ IDEA.
  User: Mae
  Date: 2021/12/12
  Time: 11:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    Login


${tips}

账号:

密码:

登录:

九、运行 1、配置tomcat

2、运行


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/657020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号