星光下的赶路人star的个人主页
碰到好的欢喜的东西,总是要留得一份清淡余地,才会有中正的缘分
文章目录
- 1、业务开发
- 1.1 前端页面
- 1.2 MVC
- 1.3Bean
- 1.4 Dao(Mapper)
- 1.5 Service
- 1.6 Controller
- 2、整合MyBatis
- 2.1 依赖整合
- 2.2 配置整合
- 2.3 使用日志打印SQL
- 3、常用注解
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 mapperPUBLIC "-//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@Autowiredprivate EmployeeMapper employeeMapper;@Overridepublic Employee getEmployeeById(Integer id) {System.out.println("操作之前do something....");Employee employee = employeeMapper.getEmployeeById(id);System.out.println("操作之后do something....");return employee;}@Overridepublic void insertEmployee(Employee employee) {System.out.println("操作之前do something....");employeeMapper.insertEmployee(employee);System.out.println("操作之后do something....");}@Overridepublic void updateEmployee(Employee employee) {System.out.println("操作之前do something....");employeeMapper.updateEmployee(employee);System.out.println("操作之后do something....");}@Overridepublic void deleteEmployeeById(Integer id) {System.out.println("操作之前do something....");employeeMapper.deleteEmployeeById(id);System.out.println("操作之后do something....");}@Overridepublic List<Employee> getAll() {System.out.println("操作之前do something....");List<Employee> all = employeeMapper.getAll();System.out.println("操作之后do something....");return all;}
}
1.6 Controller
@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: mydbtype: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://hadoop104:3306/Mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8username: rootpassword: "000000"driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:mapper-locations: classpath*:/mappers/*Mapper.xmlconfiguration:mapUnderscoreToCamelCase: true
2.3 使用日志打印SQL
在application.yml中添加如下配置,其中格式为logging.level.mapper接口所在包名=日志级别
所以红色部分请替换为自己的mapper所在包名。
logging:level:com:atguigu:springbootdemo:mapper: debug
3、常用注解
注解 | 常见标注位置 | 作用 |
---|---|---|
@Controller | 控制器类 | 标识一个控制器类 |
@Service | 服务类 | 标识一个业务模型类 |
@ResponseBody | 方法 | 将方法的返回值作为响应体返回,如果返回值为字面量则直接返回,如果为非字面量则转为json后返回 |
@RequestBody | 参数 | 用于将前台传入的json格式数据转换为Bean或Map |
@RestController | 控制器类 | 作用等于@Controller+为此类的所有方法都默认添加@ResponseBody注解 |
@Autowired | 属性 | 从容器中找到和此属性类型一致的Bean找到就赋值,找不到就报错 |
@RequestMapping | 方法 | 指定当前方法所处理的url |
@GetMapping | 方法 | 指定当前方法只处理指定的url的Get请求 |
@PostMapping | 方法 | 指定当前方法只处理指定url的Post请求 |
您的支持是我创作的无限动力
希望我能为您的未来尽绵薄之力
如有错误,谢谢指正;若有收获,谢谢赞美