【无废话版】SpringBoot系列:整合MyBatis框架

环境

开发工具:IntelliJ IDEA 2020.1.1 x64
数据库:MySqL 5.7

一,创建SpringBoot项目引入相关jar

pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <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.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二,配置application.yml

注意:Springboot默认配置文件是application.properties 可以修改为yml结尾(application.yml),比较简洁,当然也可以不用换,根据个人开发喜好。

spring:
  profiles:
    active: dev

  多环境配置,可以根据开发环境不同选择不同的配置文件,结构为application-  {profile}.yml,其中{profile} 是你的环境标识。

application-dev.yml(开发环境)
application-test.yml(测试环境)
application-uat.yml(预发布)
application-pro.yml(生产环境)

这里我按开发环境,选择dev配置文件,进行MyBatis相关配置

server:
  port: 8080 #端口配置
  servlet:
    context-path: /demo #项目路径配置
#配置数据库
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml


#showSql
logging:
  level:
    com:
      example:
        mapper : debug



创建数据库表,添加一些假数据进行测试

 

三,项目包结构的创建和创建测试类

Admin类

@Data
public class Admin {
  private  Integer admin_id;
  private  String username;
  private  String password;
}

AdminMapper类

@Mapper
@Repository
public interface AdminMapper {

    /**
     * 根据id查询用户
     */
    public Admin getAdminById(Integer id);
}

 

adminMapper.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="pojo">
    <select id="listStudent" resultType="Student">
        select * from  student
    </select>
</mapper>

AdminService类

@Service
public class AdminService {

    @Autowired
    private  AdminMapper adminMapper;

    public Admin getAdminById(Integer id){
        return adminMapper.getAdminById(id);
    }
}

先不急着启动测试,可以写个单元测试类测试本地数据能否获取到,Springboot自带的一个单元测试类很好用,直接注入Service对象即可使用。

测试类

@SpringBootTest
class Demo11122ApplicationTests {

    @Autowired
    private AdminService adminService;
    @Test
    void contextLoads() {
        Admin adminById = adminService.getAdminById(1);
        System.out.println(adminById);
    }

}

输出结果

四,Web端测试

Controller类

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

  @GetMapping("getAdmin/{id}")
    public  String getAdmin(@PathVariable int id){
         return adminService.getAdminById(id).toString();
  }
}

启动测试

-----不积跬步,无以至千里,不积小流,无以成江海。学习在于积累,量变引起质变。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

study@lin017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值