SpringBoot + MyBatis 实现对员工的增删改查

一、使用idea新建SpringBoot项目

File——>New——>Project——>Spring Assistant——>Next——>修改报名,项目名等信息——>Next——>
在web中选中web,在SQL中选中MySQL、JDBS、MyBatis——>Next——>Finish
SpringBoot + MyBatis 实现对员工的增删改查_第1张图片

二、修改pom.xml文件



    4.0.0

    cn.qiuuuu
    empboot
    0.0.1-SNAPSHOT
    jar

    empboot
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
    

    
        UTF-8
        1.8
        1.8
    

    
        
            org.apache.commons
            commons-dbcp2
            2.2.0
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
        
            org.springframework.boot
            spring-boot-starter-tomcat
        

        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        

        
        
            javax.servlet
            jstl
            1.2
        

        
        
            mysql
            mysql-connector-java
            5.1.21
        

        
        
            javax.servlet
            javax.servlet-api
            provided
        

        
        
            javax.servlet
            jstl
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        

        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            
        
        
            com.alibaba
            druid
            1.0.27
        

        
            com.alibaba
            fastjson
            1.2.23
        

    

三、新建包和相应的接口和类

SpringBoot + MyBatis 实现对员工的增删改查_第2张图片
1、新建包

2、写controller
(1)、EmpController

package cn.qiu.controller;

import cn.qiu.entity.Emp;
import cn.qiu.service.EmpService;
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 javax.annotation.Resource;
import java.util.List;

@Controller
public class EmpController {
    @Resource
    EmpService empService;

    @RequestMapping("/a")
    public String a(){
        return "emp";
    }

    @ResponseBody
    @RequestMapping("/aa")
    public List aa(){
        return empService.findAll();
    }
}

(2)、AddEmpController

package cn.qiu.controller;

import cn.qiu.service.AddEnpService;
import org.apache.ibatis.annotations.Param;
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;

@Controller
public class AddEmpController {
    @Autowired
    AddEnpService addEnpService;
    @ResponseBody
    @RequestMapping("addEmp")
    public void addEmp(@Param("name") String name,@Param("sex") String sex,@Param("age") int age,
                       @Param("address") String address){
        addEnpService.addEmp(name,sex,age,address);

    }
}

(3)、DeleteEmpController

package cn.qiu.controller;

import cn.qiu.service.DeleteEmpService;
import org.apache.ibatis.annotations.Param;
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;

@Controller
public class DeleteEmpController {

    @Autowired
    DeleteEmpService deleteEmpService;

    @ResponseBody
    @RequestMapping("/delete")
    public void deleteByName(@Param("name") String name){
        System.out.println("controller"+name);
        deleteEmpService.deleteByName(name);
    }
}

3、写service
(1)、EmpService

package cn.qiu.service;

import cn.qiu.entity.Emp;

import java.util.List;

public interface EmpService {
    public List findAll();
}

(2)、AddEmpDao

package cn.qiu.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AddEmpDao {
    public void addEmp(String name,String sex,int age,String address);
}

(3)、DeleteEmpDao

package cn.qiu.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DeleteEmpDao {
    void deleteByName(String name);
}

4、写serviceImpl
(1)、EmpServiceImpl

package cn.qiu.service.impl;

import cn.qiu.dao.EmpDao;
import cn.qiu.entity.Emp;
import cn.qiu.service.EmpService;
import org.springframework.stereotype.Service;

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

@Service(value = "userService")
public class EmpServiceImpl implements EmpService {
    @Resource
    EmpDao empDao;

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

(2)、AddEmpServiceImpl

package cn.qiu.service.impl;

import cn.qiu.dao.AddEmpDao;
import cn.qiu.service.AddEnpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AddEmpServiceImpl implements AddEnpService {
    @Autowired
    AddEmpDao addEmpDao;
    @Override
    public void addEmp(String name, String sex, int age, String address) {
        addEmpDao.addEmp(name,sex,age,address);
    }
}

(3)、DeleteEmpServiceImpl

package cn.qiu.service.impl;

import cn.qiu.dao.DeleteEmpDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeleteEmpServiceImpl implements cn.qiu.service.DeleteEmpService {
    @Autowired
    DeleteEmpDao deleteDao;
    @Override
    public void deleteByName(String name) {
        System.out.println("serviceImpl"+name);
        deleteDao.deleteByName(name);
    }
}

5、写Dao
(1)、EmpDao

package cn.qiu.dao;

import cn.qiu.entity.Emp;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface EmpDao {
    public List findAll();
}

(2)、AddEmpDao

package cn.qiu.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AddEmpDao {
    public void addEmp(String name,String sex,int age,String address);
}

(3)、DeleteEmpDao

package cn.qiu.dao;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface DeleteEmpDao {
    void deleteByName(String name);
}

6、写Mapper
(1)、AddEmpMapper




    

(2)、AddEmpMapper




    
        insert into person values(#{0},#{1},#{2},#{3})
    

(3)、DeleteEmpMapper




    
        delete from person where name=#{name}
    

四、新建数据库和表

CREATE TABLE `person` (
  `name` varchar(10) DEFAULT NULL,
  `sec` varchar(10) DEFAULT NULL,
  `agg` int(10) DEFAULT NULL,
  `addre` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `person` VALUES ('qiuuuu', 'm', '18', 'china');
INSERT INTO `person` VALUES ('chi', 'f', '18', 'china');
INSERT INTO `person` VALUES ('james', 'm', '30', 'usa');
INSERT INTO `person` VALUES ('kebi', 'm', '38', 'usa');
INSERT INTO `person` VALUES ('zhangsan', 'm', '10', 'china');
INSERT INTO `person` VALUES ('zhang', 'f', '35', 'china');

五、新建springboot配置文件application.yml

#设置Tomcat端口,默认8080
server.port=8081
#设置项目ContextPath
#server.context-path=/
#设置Tomcat编码
server.tomcat.uri-encoding=UTF-8
#设置视图解析器路径
spring.mvc.view.prefix=/WEB-INF/JSP/
#设置视图解析器后缀
spring.mvc.view.suffix=.jsp

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=qiuhongchijuan12
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#配置.xml文件路径(mapper文件位置)
mybatis.mapper-locations=classpath:mapper/*.xml
#配置模型路径(实体类的位置)
mybatis.type-aliases-package=cn.qiu.entity

六、写静态页面



    
        
        
            
        
        
    
    
        
Name Sex age address

七、测试

1、查询person表中所有数据
http://127.0.0.1:8081/html/a.html

SpringBoot + MyBatis 实现对员工的增删改查_第3张图片

2、删除表中名字为zhansan的信息
http://127.0.0.1:8081/addEmp?name=zhangsan
3、添加
http://127.0.0.1:8081/addEmp?name=zhang&&sex=f&&age=35&&address=china