ok了家人们,今天继续学习spring boot,let‘s go
六.SpringBoot实现SSM整合
6.1 创建工程,导入静态资源
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h2>根据ID查询员工数据</h2>
<input type="button" value="点我" onclick="fn01()"/><h2>查询所有员工数据</h2>
<input type="button" value="点我" onclick="fn02()"/><h2>更新员工数据</h2>
<input type="button" value="点我" onclick="fn03()"/><h2>修改员工数据</h2>
<input type="button" value="点我" onclick="fn04()"/><h2>删除员工数据</h2>
<input type="button" value="点我" onclick="fn05()"/><script src="/js/axios-0.18.0.js"></script><script>function fn01(){axios.get("http://localhost:8080/emp/findEmpById?empId=1").then(function(response){console.log(response.data);});}function fn02(){axios.get("http://localhost:8080/emp/findAllEmp").then(function(response){console.log(response.data);});}function fn03(){axios.post("http://localhost:8080/emp/saveEmp",{"empName":"lh","empSalary":5000.00}).then(function(response){console.log(response.data);});}function fn04(){axios.put("http://localhost:8080/emp/updateEmp",{"empName":"lh","empSalary":5000.00,"empId":1}).then(function(response){console.log(response.data);});}function fn05(){axios.delete("http://localhost:8080/emp/deleteEmpById?empId=20 ").then(function(response){console.log(response.data);});}
</script>
</body>
</html>
6.2 引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.cjx</groupId><artifactId>springboot_ssm</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--所有的springboot工程需要继承的父工程--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.10.RELEASE</version></parent><dependencies><!--web开发的相关依赖 场景启动器依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><!-- SpringBoot应用打包插件--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
6.3 编写配置文件
#服务器端口号 server:port: 8080#数据库信息配置 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 123456type: com.alibaba.druid.pool.DruidDataSourceapplication:name: /#配置mapper的映射文件的位置 mybatis:configuration:map-underscore-to-camel-case: truemapper-locations: classpath:mapper/*.xmltype-aliases-package: com.cjx.pojo
yml文件注意书写方式,详情可以看上篇
6.4 创建启动类
package com.cjx;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication
@EnableTransactionManagement//开启事务支持
public class Application {public static void main(String[] args){SpringApplication.run(Application.class, args);}
}
6.5 创建实体类
package com.cjx.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {private Integer empId;private String empName;private Double empSalary;
}
package com.cjx.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {private int code;//响应状态码private String msg;//响应消息private Object data;//响应数据
}
如果你想写的更好可以将状态码再编写一个实体类,去响应
6.6 编写Mapper
package com.cjx.mapper;import com.cjx.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface EmpMapper {//根基id查询员工信息Emp findEmpById(Integer id);//查询所有员工List<Emp> findAllEmp();//更新员工信息Integer saveEmp(Emp emp);//修改员工信息Integer updateEmp(Emp emp);//删除员工信息Integer deleteEmpById(Integer id);}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cjx.mapper.EmpMapper"><select id="findEmpById" resultType="Emp">select * from t_emp where emp_id=#{empId}</select><select id="findAllEmp" resultType="Emp">select * from t_emp</select><insert id="saveEmp" parameterType="Emp">insert into t_emp values(null,#{empName},#{empSalary})</insert><update id="updateEmp" parameterType="Emp">update t_emp set emp_name=#{empName},emp_salary=#{empSalary} where emp_id=#{empId}</update><delete id="deleteEmpById" parameterType="Integer">delete from t_emp where emp_id=#{empId}</delete>
</mapper>
这个也可以直接在empMapper类中直接用注解写sql语句,不用映射配置文件
6.7 编写Service
package com.cjx.service;import com.cjx.pojo.Emp;import java.util.List;public interface EmpService {//根基id查询员工信息public Emp findEmpById(Integer id);//查询所有员工信息public List<Emp> findAllEmp();//更新员工信息public Integer saveEmp(Emp emp);//修改员工信息public Integer updateEmp(Emp emp);//删除员工信息public Integer deleteEmpById(Integer id);
}
package com.cjx.service.impl;import com.cjx.mapper.EmpMapper;
import com.cjx.pojo.Emp;
import com.cjx.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import java.util.List;@Service
@Transactional
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;@Overridepublic Emp findEmpById(Integer id) {return empMapper.findEmpById(id);}@Overridepublic List<Emp> findAllEmp() {return empMapper.findAllEmp();}@Overridepublic Integer saveEmp(Emp emp) {return empMapper.saveEmp(emp);}@Overridepublic Integer updateEmp(Emp emp) {return empMapper.updateEmp(emp);}@Overridepublic Integer deleteEmpById(Integer id) {return empMapper.deleteEmpById(id);}
}
6.8 编写Controller
package com.cjx.controller;import com.cjx.pojo.Emp;
import com.cjx.pojo.Result;
import com.cjx.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/emp")
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping("/findEmpById")public Result findEmpById(Integer empId){Emp emp = empService.findEmpById(empId);if (emp != null){return new Result(200,"查询成功",emp);}else{return new Result(50001,"查询失败",null);}}@GetMapping("/findAllEmp")public Result findAllEmp(){List<Emp> empList = empService.findAllEmp();if (empList != null){return new Result(200,"查询成功",empList);}else{return new Result(50001,"查询失败",null);}}@PostMapping("/saveEmp")public Result saveEmp(@RequestBody Emp emp){Integer row = empService.saveEmp(emp);if (row == 1){return new Result(200,"查询成功",emp);}else{return new Result(50001,"查询失败",null);}}@PutMapping("/updateEmp")public Result updateEmp(@RequestBody Emp emp){Integer row = empService.updateEmp(emp);if (row == 1){return new Result(200,"查询成功",emp);}else{return new Result(50001,"查询失败",null);}}@DeleteMapping("/deleteEmpById")public Result deleteEmpById(Integer empId){Integer row = empService.deleteEmpById(empId);if (row == 1){return new Result(200,"查询成功",null);}else{return new Result(50001,"查询失败",null);}}
}
ok了家人们,明天见byebye