知世故而不世故 是善良的成熟
文章目录
1、业务开发
根据提供的web页面吗,完成对Employee的增删改查功能
1.1 前端页面
前端页面不需要懂,只需要拷贝到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>
1.2 MVC
MVC(model view controller)是一个web应用开发时要遵守的开发规
范
1.3Bean
@Data @AllArgsConstructor @NoArgsConstructor public class Employee { private Integer id; private String lastName; private String gender; private String email; }
1.4 Dao(Mapper)
@Mapper public interface EmployeeMapper { //增删改查 Employee getEmployeeById(Integer id); void insertEmployee(Employee employee); void updateEmployee(Employee employee); void deleteEmployeeById(Integer id); // 查询所有 List<Employee> getAll(); }
为每个方法提供对应的sql语句:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace是dao层所在类的类路径--> <mapper namespace="com.zhm.springwebdemo.mappers.EmployeeMapper"> <!--每个dao层的方法名 对应这一个标签,标签名是方法名--> <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>
1.5 Service
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; } }
2、整合MyBatis
2.1 依赖整合
搭建一个SpringBoot的web工程,之后在pom.xml中添加如下依赖
<!--springBoot 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--德鲁伊连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--mysql驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
2.2 配置整合
在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
2.3 使用日志打印SQL
在application.yml中添加如下配置,其中格式为logging.level.mapper接口所在包名=日志级别
所以红色部分请替换为自己的mapper所在包名。
logging: level: com: atguigu: springbootdemo: mapper: debug
3、常用注解
您的支持是我创作的无限动力
希望我能为您的未来尽绵薄之力
如有错误,谢谢指正若有收获,谢谢赞美