当前位置: 首页 > news >正文

SpringBoot day 1105

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

http://www.lryc.cn/news/477214.html

相关文章:

  • MySQL 完整教程:从入门到精通
  • 【贝叶斯公式】贝叶斯公式、贝叶斯定理、贝叶斯因子,似然比
  • [libos源码学习 1] Liboc协程生产者消费者举例
  • Python OpenCV 图像改变
  • k8s按需创建 PV和创建与使用 PVC
  • 揭秘云计算 | 2、业务需求推动IT发展
  • 【系统面试篇】进程与线程类(2)(笔记)——进程调度、中断、异常、用户态、核心态
  • 基于MySQL的企业专利数据高效查询与统计实现
  • 热成像手机VS传统热成像仪:AORO A23为何更胜一筹?
  • Spring IoC——依赖注入
  • Linux 中,flock 对文件加锁
  • CentOS下载ISO镜像的方法
  • Node.js 入门指南:从零开始构建全栈应用
  • MYSQL 真实高并发下的死锁
  • Zookeeper 简介 | 特点 | 数据存储
  • 设计模式之结构型模式---装饰器模式
  • Android Pair
  • 华为荣耀曲面屏手机下面空白部分设置颜色的方法
  • 《C#语法一篇通》,有20万字,需8MB字节,宜48小时阅读,没准会继续完善
  • 嵌入式硬件工程师的职业发展规划
  • QT for android 问题总结(QT 5.15.2)
  • PyTorch实战-手写数字识别-MLP模型
  • (附项目源码)Java开发语言,基于Java的高校实验室教学管理系统的设计与开发 50,计算机毕设程序开发+文案(LW+PPT)
  • 【日常问题排查小技巧-连载】
  • elastic search查找字段的方法
  • MATLAB下的四个模型的IMM例程(CV、CT左转、CT右转、CA四个模型),附下载链接
  • 无人机之中继通信技术篇
  • 阳光保险隐忧浮现:业绩与股价双双而下,张维功能否力挽狂澜?
  • 【OJ题解】在字符串中查找第一个不重复字符的索引
  • 处理配对和拆分内容 |【python技能树知识点1~2 习题分析】