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

spring+springmvc+mybitis整合ssm

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

spring+springmvc+mybitis整合ssm

项目基本结构

导入jar pom.xml



  4.0.0

  com.cheng
  ssm
  1.0-SNAPSHOT
  war

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

  
    UTF-8
    1.7
    1.7
  
  
  
  
    org.springframework
    spring-jdbc
    5.3.4
  
  
  
    org.mybatis
    mybatis-spring
    2.0.6
  

  
  
    org.springframework
    spring-webmvc
    5.3.4
  
  
  
    org.projectlombok
    lombok
    1.18.18
  
  
  
    com.alibaba
    druid
    1.1.9
  
  
  
    mysql
    mysql-connector-java
    8.0.25
  
  
  
    org.mybatis
    mybatis
    3.5.6
  

  
  
    org.mybatis.generator
    mybatis-generator-core
    1.4.0
  
  
  
    javax.servlet
    servlet-api
    2.5
  
  
    javax.servlet.jsp
    jsp-api
    2.2
  

  
  
    com.fasterxml.jackson.core
    jackson-databind
    2.11.4
  
    
      com.alibaba
      fastjson
      1.2.72
    

  
  
    com.github.pagehelper
    pagehelper
    5.1.11
  



generator 代码生成器
gertor.xml





    

    
        
            
        
        
        

        
            
        

        
            
            
        

        
            
        

        
            
        


        

代码生成器 测试类

public class Test {
    public static void main(String[] args) throws Exception{
        List warnings = new ArrayList();
        boolean overwrite = true;
        File configFile = new File("gertor.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

**引入layui文件

index.jsp

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


    员工表
    
    


<%--表格--%>
<%--添加按钮--%> <%--修改 删除--%> <%--修改form--%>
<%--隐藏id--%> <%--姓名--%> <%--性别--%> <%--邮箱--%> <%-- --%> <%-- 部门--%>
<%-- input form表单提交可以添加转页面 button只能将数据提交到当前页面--%>

web.xml




    
        srpingmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        srpingmvc
        /
    

    
        org.springframework.web.context.ContextLoaderListener
    

    
        contextConfigLocation
        classpath:Spring.xml
    


springmvc.xml




        

    
    

        
            
            
        

srping.xml




    

    
        
        
        
        
    
    
    
        
        
        
        
        
            
                
                
                    
                        
                            true
                        
                    
                
            
        
    
    
    
        
        
    



db.properties

jdbc.driverName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

EmpMapper.xml




  
    
    
    
    
    
    
      
      
    
  
  
    empId, empName, gender, email, d_Id
  
  
    select * from tbl_emp  e join tbl_dept d on e.d_Id=d.deptId
  





  
    delete from tbl_emp
    where empId = #{empid,jdbcType=INTEGER}
  

  
    insert into tbl_emp (empId, empName, gender, 
      email, d_Id)
    values (#{empid,jdbcType=INTEGER}, #{empname,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR}, 
      #{email,jdbcType=VARCHAR}, #{dId,jdbcType=INTEGER})
  
  
    insert into tbl_emp
    
      
        empId,
      
      
        empName,
      
      
        gender,
      
      
        email,
      
      
        d_Id,
      
    
    
      
        #{empid,jdbcType=INTEGER},
      
      
        #{empname,jdbcType=VARCHAR},
      
      
        #{gender,jdbcType=CHAR},
      
      
        #{email,jdbcType=VARCHAR},
      
      
        #{dId,jdbcType=INTEGER},
      
    
  
  
    update tbl_emp
    
      
        empName = #{empname,jdbcType=VARCHAR},
      
      
        gender = #{gender,jdbcType=CHAR},
      
      
        email = #{email,jdbcType=VARCHAR},
      
      
        d_Id = #{dId,jdbcType=INTEGER},
      
    
    where empId = #{empid,jdbcType=INTEGER}
  
  
    update tbl_emp
    set empName = #{empname,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=CHAR},
      email = #{email,jdbcType=VARCHAR},
      d_Id = #{dId,jdbcType=INTEGER}
    where empId = #{empid,jdbcType=INTEGER}
  
  
    update tbl_emp
    
      
        empName = #{empname,jdbcType=VARCHAR},
      
      
        gender = #{gender,jdbcType=CHAR},
      
      
        email = #{email,jdbcType=VARCHAR},
      
      
        d_Id = #{dId,jdbcType=INTEGER},
      
    
    where empId = #{empid,jdbcType=INTEGER}
  


  
  
    delete from tbl_emp
    where empId = #{empid}
  



DeptMapper.xml




  
    
    
  
  
    deptId, deptName
  
  
            select   from tbl_dept
  





  
    delete from tbl_dept
    where deptId = #{deptid,jdbcType=INTEGER}
  
  
    insert into tbl_dept (deptId, deptName)
    values (#{deptid,jdbcType=INTEGER}, #{deptname,jdbcType=VARCHAR})
  
  
    insert into tbl_dept
    
      
        deptId,
      
      
        deptName,
      
    
    
      
        #{deptid,jdbcType=INTEGER},
      
      
        #{deptname,jdbcType=VARCHAR},
      
    
  
  
    update tbl_dept
    
      
        deptName = #{deptname,jdbcType=VARCHAR},
      
    
    where deptId = #{deptid,jdbcType=INTEGER}
  
  
    update tbl_dept
    set deptName = #{deptname,jdbcType=VARCHAR}
    where deptId = #{deptid,jdbcType=INTEGER}
  

TableLayui类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TableLayui {
    private Integer code;
    private String msg;
    private Long count;
    private Object data;
}

Reust类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Reust {
    Integer code;
    String msg;
    Object data;

}

Emp类

package com.cheng.entity;

public class Emp {
    private Integer empid;

    private String empname;

    private String gender;

    private String email;

    private Integer dId;
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }


    public Integer getEmpid() {
        return empid;
    }

    public void setEmpid(Integer empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname == null ? null : empname.trim();
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender == null ? null : gender.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public Integer getdId() {
        return dId;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }
}

dept类

package com.cheng.entity;

public class Dept {
    private Integer deptid;

    private String deptname;

    public Integer getDeptid() {
        return deptid;
    }

    public void setDeptid(Integer deptid) {
        this.deptid = deptid;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname == null ? null : deptname.trim();
    }
}

EmpMapper

package com.cheng.dao;

import com.cheng.entity.Emp;

import java.util.List;

public interface EmpMapper {
    int deleteByPrimaryKey(Integer empid);

    int insert(Emp record);

    int insertSelective(Emp record);

    Emp selectByPrimaryKey(Integer empid);

    int updateByPrimaryKeySelective(Emp record);

    int updateByPrimaryKey(Emp record);


//    查询emp所有
    List findAllEmp();

//    删除信息
    int del(Integer empid);
//    修改添加
    int ap(Emp emp);


}

DeptMapper

package com.cheng.dao;

import com.cheng.entity.Dept;

import java.util.List;

public interface DeptMapper {
    int deleteByPrimaryKey(Integer deptid);

    int insert(Dept record);

    int insertSelective(Dept record);

    Dept selectByPrimaryKey(Integer deptid);

    int updateByPrimaryKeySelective(Dept record);

    int updateByPrimaryKey(Dept record);

    List selectAll();
}

EmpController

package com.cheng.controller;


import com.cheng.dao.EmpMapper;
import com.cheng.entity.Emp;
import com.cheng.util.Reust;
import com.cheng.util.TableLayui;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class EmpController {
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("findAllEmp")
    public TableLayui findAllEmp(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List list = empMapper.findAllEmp();
        PageInfo pageInfo = new PageInfo(list);
        return new TableLayui(0, "", pageInfo.getTotal(), pageInfo.getList());
    }

    //
    @RequestMapping("svae")
    public Reust svae(Emp emp) {
         int ap;
        if (emp.getEmpid() != null && emp.getEmpid() != 0) {
            ap = empMapper.updateByPrimaryKey(emp);

        } else {
            ap = empMapper.insert(emp);
        }
        if (ap>0) {
            return new Reust(200, "添加成功", null);

        }else {
            return new Reust(500, "添加失败", null);
    }
    }
    //  删除
    @RequestMapping("del")
    public Reust del(Integer empId) {
        int del = empMapper.del(empId);
//        System.out.println(empId);
        if (del > 0) {
            return new Reust(200, "删除成功", null);

        } else {
            return new Reust(200, "删除成功", null);

        }
    }

    //
    @RequestMapping("cname")
    public Reust cname(String empname) {
        if (empname.equals("ccr")){
            return new Reust(200,"账号存在",null);
        }else {
            return new Reust(500,"账号失败",null);

        }
    }



}

Deptcontertller

package com.cheng.controller;

import com.cheng.dao.DeptMapper;
import com.cheng.entity.Dept;
import com.cheng.util.Reust;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Controller
public class Deptcontertller {
    @Autowired
    private DeptMapper deptMapper;
    @RequestMapping("/findById")
    @ResponseBody
    public Dept findByid(Integer id){
        Dept dept = deptMapper.selectByPrimaryKey(id);
        return dept;
    }
    @RequestMapping("getByDept")
    @ResponseBody
    public Reust getByDept(){
        List depts = deptMapper.selectAll();


        return new Reust(200,"查询成功",depts);
    }

}

项目运行效果



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530487.html

Java相关栏目本月热门文章

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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