IDEA实现SSM框架下简单的增删查改

工程结构

IDEA实现SSM框架下简单的增删查改_第1张图片

新建项目:点击File -> New -> Project -> Maven -> 勾选 Create from archetype -> 选择 maven-archetype-webapp出的new project

  • IDEA实现SSM框架下简单的增删查改_第2张图片
    选项卡中填写GroupId和Artifactid,其中GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,ArtifactID是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称
    IDEA实现SSM框架下简单的增删查改_第3张图片
    IDEA实现SSM框架下简单的增删查改_第4张图片
    点击Finish,完成项目骨架的创建;
    新建的项目如图所示:IDEA实现SSM框架下简单的增删查改_第5张图片
    然后我们要在这些项目上新建一些目录
    IDEA实现SSM框架下简单的增删查改_第6张图片
    注意java要设置成Source Root
    resource要设置成Resource Root,设置方法如下
    IDEA实现SSM框架下简单的增删查改_第7张图片
    上面新建的四个包:bean,service,dao,controller,其所存放的分别是:

bean: 存放自定义的java类。如:paper类,user类,book类等,每个类的属性设为private,并提供public属性的getter/setter方法让外界访问
service:定义接口,包含系统所提供的功能。(之后还会在service包下再新建impl包)。
dao:定义接口,包含与数据库进行交互的功能。
controller:控制器,负责接收页面请求,转发和处理。
在resource包下新建Directory:“mapper”
对项目的pom.xml文件进行配置,在此文件中配置项目所需要的jar包。
pom.xml




  4.0.0

  whut
  ssm3
  1.0-SNAPSHOT
  war

  ssm3 Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    
    5.0.2.RELEASE
    
    3.2.6
    
    1.7.7
    1.2.17
    0.9.5.2
    1.1.2
  
  
    
    
      org.springframework
      spring-core
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    
    
      org.springframework
      spring-oxm
      ${spring.version}
    
    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-aop
      ${spring.version}
    

    
      org.springframework
      spring-context-support
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
    
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    
    
    
      org.mybatis
      mybatis-spring
      1.2.2
    
    
    
      javax
      javaee-api
      7.0
    

    
    
      mysql
      mysql-connector-java
      5.1.30
    
    
    
      commons-dbcp
      commons-dbcp
      1.2.2
    
    
    
      jstl
      jstl
      1.2
    
    
    
    
      log4j
      log4j
      ${log4j.version}
    


    
    
      com.mchange
      c3p0
      ${c3p0.version}
    

    
      taglibs
      standard
      ${taglibs.version}
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    
    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    

    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    
    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.1
      provided
    

  

  
    ssmdemo5
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


下面开始完成JAVA代码,新建java的类和接口
IDEA实现SSM框架下简单的增删查改_第8张图片
在bean包中新建UserInfo

package com.bean;

public class UserInfo {
    private int id;
    private String username;
    private String password;


    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;
    }
}

可以右键选择Generate选择Getter and Setter自动生成
在service包中新建接口IUserservice

package com.service;

import com.bean.UserInfo;

import javax.xml.registry.infomodel.User;
import java.util.List;

public interface IUserservice {
    public List findall();
    public void deleteUser(int id);
    public void addUser(UserInfo userInfo);

    public void updateUser(UserInfo userInfo);
    UserInfo queryId(int id);
    UserInfo checkUser(String username);

}

在service包下新建impl包,并在此包中新建UserService.java


import com.bean.UserInfo;
import com.dao.IUserDao;
import com.service.IUserservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserservice {

    @Autowired
    private IUserDao userDao;

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

    @Override
    public void deleteUser(int id) {
        userDao.deleteUser(id);
    }

    @Override
    public void addUser(UserInfo userInfo) {
        userDao.addUser(userInfo);
    }

    @Override
    public void updateUser(UserInfo userInfo) {
    userDao.updateUser(userInfo);
}

    @Override
    public UserInfo queryId(int id) {
        return userDao.queryId(id);
    }

    @Override
    public UserInfo checkUser(String username) {
        return userDao.checkUser(username);
    }


}

在dao包中新建接口 IUserDao.java

package com.dao;

import com.bean.UserInfo;

import java.util.List;

public interface IUserDao  {     //接口  在UserMapper中实现接口
    public List findall();
    public void deleteUser(int id);
    public void updateUser(UserInfo userInfo);
    public void addUser(UserInfo userInfo);
    UserInfo queryId(int id);
    UserInfo checkUser(String username);

}

在controller包中新建 UserController.java

package com.controller;

import com.bean.UserInfo;
import com.service.IUserservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserservice userService;

    @RequestMapping("/findAll.do")
    public ModelAndView findall(){
        List all=userService.findall();
        ModelAndView mv=new ModelAndView();
        mv.addObject("userinfos",all);
        mv.setViewName("allUser");
        return mv;
    }


    @RequestMapping("/todeleteUser.do")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "redirect:findAll.do";
    }

    @RequestMapping("/toAddUser.do")
    public String  toAddUser(){
        return "addUser";
    }

    @RequestMapping("/save.do")
    public String AddUser(UserInfo userInfo){
        userService.addUser(userInfo);
        return "redirect:findAll.do";
    }

    @RequestMapping("/toUpdateUser.do")
    public String toupdateUser(UserInfo userInfo,int id){
       userInfo=userService.queryId(id);
        return "updateUser";
    }

    @RequestMapping("/update.do")
    public String updateUser(UserInfo userInfo){

        userService.updateUser(userInfo);
        userInfo=userService.queryId(userInfo.getId());
        return "redirect:findAll.do";

    }

    @RequestMapping("/inUser.do")
    public String login(HttpServletRequest request){
        String username= request.getParameter("username");
        String password=request.getParameter("password");
        boolean flag=logincheck(username,password);
        if(flag){
            return "redirect:findAll.do";
        }
        else{
            return "inUser";
        }
    }

    private boolean logincheck(String username,String password){
        UserInfo userInfo=userService.checkUser(username);
        if(userInfo==null||"".equals(username)){
            return false;
        }
        if(userInfo.getPassword().equals(password)){
            return true;
        }
        else{
            return false;
        }
    }

}

在Mapper包下新建UserMapper.xml







    
        insert into userinfo(id,username,password) value (#{id},#{username},#{password})
    

    
        delete from userinfo where id=#{id}
    


    update userinfo set id=#{id},username=#{username},password=#{password} where id=#{id}


    

    

添加前面目录上所有的applicationContext,db.properties,log4j.properties,spring-mvc.xml
applicationContext



    
    

    
    
        
        
        
        
        
        
    

    
    
        
        
        
        

        
        

    

    
    
    
        
        
        
        
    



    
    


    
    
    
        
    
    
    


db.properties

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

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

spring-mvc.xml




    
    

    
    
    

    
    
        
        
    

添加jsp页面
addUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    新增用户
    
    
    


用  户  id:


用户姓名:


用户密码:


allUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    user列表
    
    
    


id 用户名 密码 操作
${userInfo.id} ${userInfo.username} ${userInfo.password} 更改 | 删除

inUser.jsp

<%--
  Created by IntelliJ IDEA.
  User: SXX
  Date: 2019/6/28
  Time: 16:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    用户登录


   
用户名:


用户密码:


updateUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    修改论文
    
    
    


ID 用户姓名: 用户密码:

index.jsp

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


Hello World!

查询所有文件 登录界面

设置Tomcat
IDEA实现SSM框架下简单的增删查改_第9张图片
IDEA实现SSM框架下简单的增删查改_第10张图片
IDEA实现SSM框架下简单的增删查改_第11张图片
IDEA实现SSM框架下简单的增删查改_第12张图片
IDEA实现SSM框架下简单的增删查改_第13张图片
点击Apply应用即可切换到index.jsp页面运行程序

你可能感兴趣的:(IDEA实现SSM框架下简单的增删查改)