SSM整合(实现全部用户查询)

准备

1、创建工程(我创建了一个maven工程)

2、pom文件导入依赖:

https://blog.csdn.net/qq_43154385/article/details/84308826

3、工程结构

SSM整合(实现全部用户查询)_第1张图片

代码

mybatis-config.xml(SqlMapConfig)



  
    
    
        
        
    

    
    
        
    
    

    
    
         
    


applicationContext.xml



    
    
    
    
    
        
        
        
        
        
    
    
    
    
        
        
    
    
    
    
        
        
    
    
    
    
    
    
    
    
        
    
    
    
        
            
            
            
            
            
            
            
            
        
    
    
    
        
        
    
    


springMVC.xml





    
    

    
    
    
    
    
    
    
    
    
        
        
    


    
    
    


jdbc.properties

driver=com.mysql.jdbc.Driver
#\u5728\u548Cmysql\u4F20\u9012\u6570\u636E\u7684\u8FC7\u7A0B\u4E2D\uFF0C\u4F7F\u7528unicode\u7F16\u7801\u683C\u5F0F\uFF0C\u5E76\u4E14\u5B57\u7B26\u96C6\u8BBE\u7F6E\u4E3Autf-8
url=jdbc:mysql://localhost:3306/witkey?characterEncoding=utf-8
user=root
password=root

maxActive=3

web.xml



    party
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        characterEncoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncoding
        /*
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        springDispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    
    
        springDispatcherServlet
        /
    
    
        index.jsp
    

SSM整合(实现全部用户查询)_第2张图片

UserController

package com.buba.witkey.controller;

import com.buba.witkey.pojo.User;
import com.buba.witkey.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/selectAll")
    public String selectAll(Model model){
        List list = userService.selectAll();
        model.addAttribute("list",list);
        return "/showAllUser";
    }
    @RequestMapping("/insertOne")
    public String insertOne(Model model, User user){
        int i = userService.insertOne(user);
        return "redirect:selectAll";
    }

}

UserMapper

package com.buba.witkey.mapper;

import com.buba.witkey.pojo.User;
import java.util.List;

public interface UserMapper {
    List selectAll();
    int insertOne(User user);
}

UserMapper.xml




    
    
        insert into manager_user values (null,#{userName},#{phone},#{qq},#{password},#{dept})
    

User

package com.buba.witkey.pojo;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String userName;
    private String phone;
    private String qq;
    private String password;
    private String dept;
}

UserService

package com.buba.witkey.service;

import com.buba.witkey.pojo.User;

import java.util.List;

public interface UserService {
    List selectAll();

    int insertOne(User user);
}

UserServiceImpl

package com.buba.witkey.serviceImpl;

import com.buba.witkey.mapper.UserMapper;
import com.buba.witkey.pojo.User;
import com.buba.witkey.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List selectAll() {
        List list = userMapper.selectAll();
        return list;
    }

    @Override
    public int insertOne(User user) {
        int i = userMapper.insertOne(user);
        return i;
    }
}

 

showAllUser.jsp

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



    
    
    
    
    Bootstrap 101 Template

    
    

    
    
    


<%--class="container-fluid"  style="background-color: #4cae4c"--%>
id username phone qq password dept
${user.id} ${user.userName} ${user.phone} ${user.qq} ${user.password} ${user.dept}

 

index.jsp

 

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



    
    
    
    
    Bootstrap 101 Template

    
    

    
    
    
    <%----%>



<%--class="container-fluid"  style="background-color: #4cae4c"--%>

添加用户


你可能感兴趣的:(ssm)