关于java:springboot和mybatis整合

39次阅读

共计 6046 个字符,预计需要花费 16 分钟才能阅读完成。

我的项目目录

src/main/resourcesapplication.yml:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/blog1?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
mybatis:
  type-aliases-package: app.repository
  mapper-locations: classpath:/mapping/*.xml

mapping/userMapper.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="app.repository.UserMapper">
    <insert id="insert" parameterType="app.model.User" keyColumn="id">
        insert into user (id,username,password)
        values (#{id},#{username},#{password})
    </insert>
</mapper>

app/controller/UserController;

import app.model.User;
import app.service.imp.UserServiceImp;
import app.utils.ResultEnum;
import app.utils.ResultInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource
    private UserServiceImp userServiceImp;

    @PostMapping("/insert")
    public ResultInfo insert(User user){int num = userServiceImp.insert(user);
        System.out.println("n:"+num);
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setMsg(ResultEnum.SUCCESS_CODE.getMsg());
        return resultInfo;
    }
}

app/model/User;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 
 * </p>
 *
 * @author huang
 * @since 2023-05-03
 */
@Getter
@Setter
@Data
public class User {
    private Integer id;
    private String username;
    private String password;
}

app/repository/UserMapper.java

import app.model.User;

public interface UserMapper {public int insert(User user);
}

app/serviceUser;


import app.model.User;

public interface UserService {public int insert(User user);
}

UserServiceImppackage;

import app.model.User;
import app.repository.UserMapper;
import app.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImp implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int insert(User user) {int num = userMapper.insert(user);
        return num;
    }
}

app/utils

ResultEnum.java;

public enum ResultEnum {SUCCESS_CODE(200,"申请胜利"),
    SUCCESS_FAIL(500,"申请失败");

    private Integer code;
    private String msg;

    ResultEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {return code;}

    public void setCode(Integer code) {this.code = code;}

    public String getMsg() {return msg;}

    public void setMsg(String msg) {this.msg = msg;}
}

ResultInfo;

public class ResultInfo {
    private Integer code=200;
    private String msg;
    private Object result;

    public Integer getCode() {return code;}

    public void setCode(Integer code) {this.code = code;}

    public String getMsg() {return msg;}

    public void setMsg(String msg) {this.msg = msg;}

    public Object getResult() {return result;}

    public void setResult(Object result) {this.result = result;}
}

app/config/SwaggerConfig;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//@Configuration 是通知 Spring Boot 须要加载这个配置类;@EnableSwagger2 是启用 Swagger2.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {return new Docket(DocumentationType.SWAGGER_2).
                select().
                apis(RequestHandlerSelectors.
                        basePackage("app.controller")). // 扫码
                        paths(PathSelectors.any()).build().apiInfo(new ApiInfoBuilder().title("springboot blog app").version("1.0").build());
    }
}
Applicationpackage app;

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

@SpringBootApplication
@MapperScan("app.repository")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);
    }
}

启动数据库

创立数表

关上 postman

代码 debug

查问数据

<?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>org.project</groupId>
<artifactId>blog1</artifactId>
<version>1.0-SNAPSHOT</version>

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <!--web 启动的 jar 包, 父包的子包 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 测试依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--Mybatis 整合 springboot 的起步依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.2</version>
    </dependency>
    <!--MySQL 的驱动依赖,点击就能够查看,此时默认的是 8.0 的,也能够依据本人应用的 mysql 版本号,来指定版本号
    -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.29</version>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency> 
    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>1.9.2</version>
    </dependency>
    <!-- 模板引擎 -->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.30</version>
    </dependency>
</dependencies>

</project>

正文完
 0