ok了家人们,今天继续学习spring boot,let's go
六**.SpringBoot实现SSM整合**
6.1****创建工程,导入静态资源
html
<!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
<?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****编写配置文件
java #服务器端口号 server: port: 8080 #数据库信息配置 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource application: name: / #配置mapper的映射文件的位置 mybatis: configuration: map-underscore-to-camel-case: true mapper-locations: classpath:mapper/*.xml type-aliases-package: com.cjx.pojo
yml文件注意书写方式,详情可以看上篇
6.4****创建启动类
java
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****创建实体类
java
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;
}
java
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
java
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
<?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">
<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
java
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);
}
java
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 {
@Autowired
private EmpMapper empMapper;
@Override
public Emp findEmpById(Integer id) {
return empMapper.findEmpById(id);
}
@Override
public List<Emp> findAllEmp() {
return empMapper.findAllEmp();
}
@Override
public Integer saveEmp(Emp emp) {
return empMapper.saveEmp(emp);
}
@Override
public Integer updateEmp(Emp emp) {
return empMapper.updateEmp(emp);
}
@Override
public Integer deleteEmpById(Integer id) {
return empMapper.deleteEmpById(id);
}
}
6.8编写Controller
java
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 {
@Autowired
private 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