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

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.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 创建启动类

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 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

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 {

    @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

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


http://www.kler.cn/a/381988.html

相关文章:

  • 解释下什么是面向对象?面向对象和面向过程的区别?
  • springboot中使用gdal将表中的空间数据转shapefile文件
  • [bug]java导出csv用Microsoft Office Excel打开乱码解决
  • 目标检测-R-CNN
  • 3D架构图软件 iCraft Editor 正式发布 @icraftplayer-react 前端组件, 轻松嵌入3D架构图到您的项目,实现数字孪生
  • Android Https和WebView
  • springcloud各组件说明
  • ctfshow(89,90,92,93)--PHP特性--intval函数
  • 【RDD算子的分类】RDD的转换算子和触发算子(行为算子)、以及各个算子的作用,对比sql中的关键字
  • 【系统架构设计师(第2版)】十一、未来信息综合技术
  • Hearts of Iron IV 之 Archive Modification
  • HTML+javaScript+CSS
  • 【个人记录】screen和tmux
  • “农田奇迹:如何用遥感技术实现作物分类与产量精准估算“
  • Java:获取HttpServletRequest请求参数
  • PostgreSQL 到 PostgreSQL 数据迁移同步
  • 计算机网络——网络层导论
  • 云渲染与汽车CGI图像技术优势和劣势
  • 用 Python 写了一个天天酷跑(附源码)
  • 【docker入门】docker的安装
  • vue3中setup的作用是什么?
  • ELK实现加载多个配置日志文件
  • Jest进阶:测试 Vue 组件
  • WAPI认证过程如何实现?
  • stm32引脚PB3、PB4、PA15作为普通IO口用时,需要先解除调试端口复用
  • 基于SSM+uniapp的营养食谱系统+LW参考示例