接口的讲解

在这里之前我想童鞋们都学习过了springmvc。mybatis-plus。Springboot等一些框架

那么下面我们就整合这些框架

我们通过写crud这些接口

写接口的第一步就是引入pom文件
在pom文件里引入一下几种依赖

引入父级工程
thymeleaf导入模版工具类
SpringMVCjar包文件
热部署工具
lombok插件
测试包
引入jdbc包
引入数据库驱动
spring整合mybatis-plus
负责项目打包部署

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

    <!--引入父级工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>
    <artifactId>jiekou</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>



    <dependencies>
        <!--thymeleaf导入模版工具类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--SpringMVCjar包文件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!--lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--测试包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--引入jdbc包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--引入数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--spring整合mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
    </dependencies>

    <!--负责项目打包部署-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

pom文件引入依赖之后那么下一步就需要配置数据库 的连接以及我们配置的端口

server:
  port: 8090

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: wyproot123

  #整合SpringMVC
  thymeleaf:
    #设置页面前缀
    prefix: classpath:/templates/
    #设置页面后缀
    suffix: .html
    #是否使用缓存
    cache: false

mybatis-plus:
  type-aliases-package: com.jt.pojo
  #type-aliases-package: com.jt
  mapper-locations: classpath:/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

#添加MP日志  打印执行的sql
logging:
  level:
    com.jt.mapper: debug

mapper文件

<?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.jt">



</mapper>

首先我们要写一个实体类pojo,注意这里的属性一定要和数据库的表列相对应

package com.jt.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;


import java.io.Serializable;



@Data
@Accessors(chain = true)
@TableName("user")  //保证数据安全性和有效性必须序列化
public class User implements Serializable {

    @TableId(type = IdType.AUTO) //主键自增
    private Integer id;
    private String name;
    private Integer num;
   
}

下一步我们就要写dao层结构

我们因为使用的是mybatis-plus所以我们在这里写的代码比较少这也就是框架的好处

package com.jt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.pojo.User;


/**
 * @author :王闫彭
 * @date : 2023/5/28
 */
public interface UseMapper extends BaseMapper<User> {
}

service层结构

首先写service接口

package com.jt.service;

import com.jt.pojo.User;

import java.util.List;

public interface UserService {
    List<User> findAll();

    void insertUser(User user);

    void updateUser(User user);

    void deleteUserById(Integer id);
}

service的实现类

package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    //mp的查询
    @Override
   public List<User> findAll() {

      return userMapper.selectList(null);
   }
    //MP的方式实现入库
    @Override
    public void insertUser(User user) {
       
        userMapper.insert(user);
    }

    //mp的更新
    @Override
    public void updateUser(User user) {

        userMapper.updateById(user);
    }

    //mp的删除
    @Override
    public void deleteUserById(Integer id) {

        userMapper.deleteById(id);
    }
}

controller层结构

package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;




    @GetMapping("/findUser")
    @ResponseBody
    public List<User> findAjaxUser(){

        return userService.findAll();
    }


    

  
    @RequestMapping("/useraa/{id}/{name}/{num}")
    @ResponseBody
    public String insertUseraa(User user){

        userService.insertUser(user);
        return "添加成功";
    }



    @RequestMapping("/userbb/{id}/{name}")
    @ResponseBody
    public String updateUseraa(User user){

        userService.updateUser(user);
    
        return "更新成功";
    }





    @RequestMapping("/usercc/{id}")
    @ResponseBody
    public String deleteUsercc(@PathVariable Integer id){

        userService.deleteUserById(id);
        //重新发起请求..
        return "删除成功";
    }


}

启动类

package com.jt;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.jt.mapper")

public class SpringBootRun {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootRun.class,args);
    }
}

你可能感兴趣的:(mybatis,java,spring,boot)