星光下的赶路人star的个人主页
碰到好的欢喜的东西,总是要留得一份清淡余地,才会有中正的缘分
根据提供的web页面吗,完成对Employee的增删改查功能
前端页面不需要懂,只需要拷贝到Resource目录下的static目录中就可以啦
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>员工操作title>
head>
<body>
<form action="/emp" >
<input type="radio" name="op" value="select">查询
<input type="radio" name="op" value="insert">新增
<input type="radio" name="op" value="delete">删除
<input type="radio" name="op" value="update">更新br>
id: <input name="id" value=""> br>
name:<input name="lastname" value=""> br>
gender:<input name="gender" value=""> br>
email:<input name="email" value=""> br>
<input value="操作" type="submit">
form>
<a href="/getAllEmp">查询所有员工a>
body>
html>
MVC(model view controller)是一个web应用开发时要遵守的开发规范
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee
{
private Integer id;
private String lastName;
private String gender;
private String email;
}
@Mapper
public interface EmployeeMapper {
//增删改查
Employee getEmployeeById(Integer id);
void insertEmployee(Employee employee);
void updateEmployee(Employee employee);
void deleteEmployeeById(Integer id);
// 查询所有
List<Employee> getAll();
}
为每个方法提供对应的sql语句:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhm.springwebdemo.mappers.EmployeeMapper">
<select id="getEmployeeById" resultType="com.zhm.springwebdemo.beans.Employee">
select * from employee where id = #{fjhaoleilfhaiolfja;}
select>
<select id="getAll" resultType="com.zhm.springwebdemo.beans.Employee">
select * from employee
select>
<delete id="deleteEmployeeById">
delete from employee where id=#{id}
delete>
<insert id="insertEmployee">
insert into employee(last_name,gender,email) values(#{lastName},#{gender},#{email})
insert>
<update id="updateEmployee">
update employee set last_name=#{lastName} , gender=#{gender} ,email=#{email}
where id = #{id}
update>
mapper>
public interface EmployeeService {
//必须至少提供5种方法对应用户的5种操作
//增删改查
Employee getEmployeeById(Integer id);
void insertEmployee(Employee employee);
void updateEmployee(Employee employee);
void deleteEmployeeById(Integer id);
// 查询所有
List<Employee> getAll();
}
@Service
public class EmployeeServiceImpl implements EmployeeService {
//声明Dao
@Autowired
private EmployeeMapper employeeMapper;
@Override
public Employee getEmployeeById(Integer id) {
System.out.println("操作之前do something....");
Employee employee = employeeMapper.getEmployeeById(id);
System.out.println("操作之后do something....");
return employee;
}
@Override
public void insertEmployee(Employee employee) {
System.out.println("操作之前do something....");
employeeMapper.insertEmployee(employee);
System.out.println("操作之后do something....");
}
@Override
public void updateEmployee(Employee employee) {
System.out.println("操作之前do something....");
employeeMapper.updateEmployee(employee);
System.out.println("操作之后do something....");
}
@Override
public void deleteEmployeeById(Integer id) {
System.out.println("操作之前do something....");
employeeMapper.deleteEmployeeById(id);
System.out.println("操作之后do something....");
}
@Override
public List<Employee> getAll() {
System.out.println("操作之前do something....");
List<Employee> all = employeeMapper.getAll();
System.out.println("操作之后do something....");
return all;
}
}
@RestController
public class EmployeeController
{
// employeeService= new EmployeeServiceImpl()
@Autowired // = 自动从容器中找标注了次注解 的类型的对象,找到就赋值
private EmployeeService employeeService;
@RequestMapping(value = "/emp")
public Object handle1(String op,Integer id,String lastname,String gender,String email){
//封装数据模型
Employee employee = new Employee(id, lastname, gender, email);
switch (op){
case "select": if (id == null){
return "必须传入员工id!";
}else {
Employee e = employeeService.getEmployeeById(id);
return e == null ? "查无此人!" : e;
}
case "insert" : employeeService.insertEmployee(employee);
return "操作完成!";
case "update": if (id == null){
return "必须传入员工id!";
}else {
employeeService.updateEmployee(employee);
return "操作完成!";
}
case "delete": if (id == null){
return "必须传入员工id!";
}else {
employeeService.deleteEmployeeById(id);
return "操作完成!";
}
default: return "请正确操作";
}
}
@RequestMapping(value = "/getAllEmp")
public Object handle2(){
List<Employee> all = employeeService.getAll();
return all;
}
}
搭建一个SpringBoot的web工程,之后在pom.xml中添加如下依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
在application.yml(如果没有,可以自己建,要建在项目的Resource路径下)中增加数据库环境配置信息
spring:
datasource:
name: mydb
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://hadoop104:3306/Mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: "000000"
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath*:/mappers/*Mapper.xml
configuration:
mapUnderscoreToCamelCase: true
在application.yml中添加如下配置,其中格式为logging.level.mapper接口所在包名=日志级别
所以红色部分请替换为自己的mapper所在包名。
logging:
level:
com:
atguigu:
springbootdemo:
mapper: debug
注解 | 常见标注位置 | 作用 |
---|---|---|
@Controller | 控制器类 | 标识一个控制器类 |
@Service | 服务类 | 标识一个业务模型类 |
@ResponseBody | 方法 | 将方法的返回值作为响应体返回,如果返回值为字面量则直接返回,如果为非字面量则转为json后返回 |
@RequestBody | 参数 | 用于将前台传入的json格式数据转换为Bean或Map |
@RestController | 控制器类 | 作用等于@Controller+为此类的所有方法都默认添加@ResponseBody注解 |
@Autowired | 属性 | 从容器中找到和此属性类型一致的Bean找到就赋值,找不到就报错 |
@RequestMapping | 方法 | 指定当前方法所处理的url |
@GetMapping | 方法 | 指定当前方法只处理指定的url的Get请求 |
@PostMapping | 方法 | 指定当前方法只处理指定url的Post请求 |
您的支持是我创作的无限动力
希望我能为您的未来尽绵薄之力
如有错误,谢谢指正;若有收获,谢谢赞美