JAVA--Mybatis-Spring-SpringMVC框架整合

------Mybatis-Spring-SpringMVC框架整合示例-----

mybatis SQL映射文件




     
    

    
    

    
    
        insert  into user(userName,gender,address)values(#{userName},#{gender},#{address})
    

    
    
        update user set userName=#{userName},gender=#{gender},address=#{address} where id=#{id}
    

    
    
        delete  from user where id=#{id}
    




















Mybatis配置文件





    
        
        
        
    


    
    
        
        
            
            
            
            
                
                
                
                
            
        
    
    
    
    
        
        
        

        
        
    

Spring【配置文件】



    
    
    
    
        
        
    
    
    

    
    
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
    


    
    
        
    
    
    
        
        
            
            
            
            
            
            
            
            
        
    
    
    
        
        
        
        
    


SpringMVC【配置文件】



    
    
        
        
    
    
    
    

    

    
    
        
        
        
        
    

    
    
    
    

Web.xml的配置



  
  
  
  
    contextConfigLocation
    classpath:Spring.xml
  
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
  
    characterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
    
      encoding
      UTF-8
    
    
    
      forceEncoding
      true
    
  
  
  
    characterEncodingFilter
    /*
  
  
  
    hiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter
  
  
    hiddenHttpMethodFilter
    /*
  
  
  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:SpringMVC.xml
    
    
    0
  
  
  
    dispatcherServlet
    /
  

  
    index.html
    index.jsp
  

ab.properties属性文件

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ab_wzy?serverTimezone=GMT
user=root
password=root

准备实体类

package com.bdit.model;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 5841583645389273544L;
    private Integer id;
    private String userName;
    private String gender;
    private String address;
    public User(){

    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", gender='" + gender + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

准备实体类

package com.bdit.model;

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 5841583645389273544L;
    private Integer id;
    private String userName;
    private String gender;
    private String address;
    public User(){

    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", gender='" + gender + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

准备持久层DAO

import java.util.List;

//编写持久层Dao接口
public interface IUserDao {

    //查询所有的信息
    public List findAll();
    //根据id去查询
    public User findByid(Integer id);
    //添加的方法
    public int insert(User  user);
    //更新的方法
    public int update(User  user);
    //删除的方法
    public int delete(Integer id);
}

准备业务层接口和业务层的实现类

package com.bdit.Service;

import com.bdit.model.User;

import java.util.List;

//编写业务层接口
public interface IUserServiceDao{
    //查询所有的信息
    public List findAll();
    //根据id去查询
    public User findByid(Integer id);
    //添加的方法
    public int insert(User  user);
    //更新的方法
    public int update(User  user);
    //删除的方法
    public int delete(Integer id);
}
实现类
package com.bdit.Service.impl;

import com.bdit.Service.IUserServiceDao;
import com.bdit.dao.IUserDao;
import com.bdit.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceimpl implements IUserServiceDao {

    @Autowired
    private IUserDao  userDao;

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List findAll(){
        return userDao.findAll();
    }

    @Override
    public User findByid(Integer id) {
        return userDao.findByid(id);
    }

    @Override
    public int insert(User user) {
        return  userDao.insert(user);
    }

    @Override
    public int update(User user) {
        return  userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return  userDao.delete(id);
    }
}

准备控制器Controller

import com.bdit.Service.impl.UserServiceimpl;
import com.bdit.model.User;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//用户控制器
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceimpl userService;

    @RequestMapping("/list")  //查询用户信息的方法
    public ModelAndView  user(){
        ModelAndView  mav=new ModelAndView();
        Listlist=userService.findAll();
        //指定响应的客户端
        mav.addObject("list",list);
        //指定要跳转的逻辑视图
        mav.setViewName("list");
        return mav;
    }
    //添加用户信息的方法

    @RequestMapping("/insert")
    public String insert(User  user){
        int i=userService.insert(user);
         if(i>0){
             return "forward:/WEB-INF/pags/success.jsp";
         }else{
             return "error";
         }
    }

    //编辑用户的信息
    @RequestMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Integer  id){
        ModelAndView  Mav=new ModelAndView();
        User  user=userService.findByid(id);
        //指定响应的客户端
        Mav.addObject("user",user);
        Mav.setViewName("edit");
        return Mav;
    }

    //用户信息的修改页面
    @RequestMapping("/update")
    public String update(User  user){
       int  i=userService.update(user);
       if(i>0){
           return "redirect:/user/list";
       }else{
           return  "error";
       }
    }
    //用户信息的修改页面
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        int  i=userService.delete(id);
        if(i>0){
            return "redirect:/user/list";
        }else{
            return  "error";
        }
    }
}

准备前端页面

用户信息的添加页面

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


Hello World 恭喜你SSM配置成功!

查询用户的信息
用户名:
性别:
住址:

用户信息的展示页面

<%--
  Created by IntelliJ IDEA.
  User: Lenovo-T410
  Date: 2020/1/7
  Time: 18:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    用户信息的查询页面


用户信息的展示页面

用户ID 用户名 性别 住址 操作
${user.id} ${user.userName} ${user.gender} ${user.address} 编辑 删除

用户的编辑页面

<%--
  Created by IntelliJ IDEA.
  User: Lenovo-T410
  Date: 2020/1/7
  Time: 20:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    用户信息的编辑页面


用户信息的编辑页面

用户名:
性别:

住址:

来源:站长平台

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