Xiaotang began to learn Spring Boot - (3) use mybatis to access data tables


或许直接叫他Spring Boot连数据库会更好啊哈哈哈哈哈哈

1. Process

Create new project

因为我们这一次要使用sql,所以重新创建一个项目吧,这次唯一不同的是,我们要额外选择几个依赖
insert image description here

Create data table

在里面随便添加一个数据就可以啦
insert image description here

Create return structure

因为我们mysql放回结构是一个list,所以我们需要定义一个新的属性来对这些值进行承接,并且属性和字段要相同,同时具备set和get方法
insert image description here

query function

这里面的Test1就是我们之前写好的结构
insert image description here
这里注意我们使用的是映射的方法,通过在.src/main/resources/mapper目录下创建一个xml文件,来作为我们方法的实现
insert image description here

method integration

因为我们到时候写Spring boot不可能只有一种函数,所以我们需要有一个类来对其进行一个集成,到时候我们通过网站访问的时候直接访问这个集成类就可以了
insert image description here

visit website

和之前一样,我们需要一个网址来访问我们的网站,来访问我们的网站,在这里我们去调用我们集成中的方法即可
insert image description here

configuration database

insert image description hereinsert image description here
我们同时还需要在我们全局里面配置相应属性,数据库的时区,驱动配置,以及账号密码,还有我们的需要引用的xml文件
insert image description here

success

insert image description here
insert image description here

Second, the project code

项目结构
insert image description here

Testlist.java

package com.example.test.controller;

import com.example.test.domain.Test1;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.test.service.TestService;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class Testlist {
    
    
    @Resource
    private TestService testService;
    @GetMapping("/list")
    public List<Test1> testlistall()
    {
    
    
        return testService.list();
    }
}

Test1.java

package com.example.test.domain;

public class Test1 {
    
    
    private Integer id;
    private String name;
    private  String password;

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getPassword() {
    
    
        return password;
    }
    public void setPassword(String password) {
    
    
        this.password = password;
    }
    @Override
    public String toString() {
    
    
        return "Test1{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

TestMapper.java

package com.example.test.mapper;
import com.example.test.domain.Test1;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
@Repository
public interface TestMapper {
    
    
    public List<Test1> findall();
}

TestService.java

package com.example.test.service;

import com.example.test.domain.Test1;
import com.example.test.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class TestService {
    
    
    @Autowired
    private TestMapper testMapper;
    public List<Test1> list(){
    
    
        return testMapper.findall();
    }
}

testmapper.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="com.example.test.mapper.TestMapper" >
    <select id="findall" resultType="com.example.test.domain.Test1">
        select `id`, `name`, `password` from `account`
    </select>
</mapper>

application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?characterEncoding=UTF8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:/mapper/**/*.xml

3. Common mistakes

Database connection failed

1. Configuration problem
If the database connection in the right frame is successful, but the connection error is displayed when running, please check spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?........
whether spring has been changed to our own database, and whether the port number is correct
insert image description here
2. The driver version problem
may have some treasures of mysql The version is relatively low, and it is not suitable for too new drivers

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary

The driver of jdbc in mysql5 is

com.mysql.jdbc.Driver

And mysql6 and above is

com.mysql.cj.jdbc.Driver

3. The time zone configuration is incorrect, please copy the time zone again

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的数据库名?characterEncoding=UTF8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true

4.xml configuration error
insert image description here

4. Summary

Relatively speaking, as long as we understand the logic inside, it is not difficult, basically these steps

1. Write the structure of the related table (convenient to put back the value later)
2. Write the method we want to implement (note here that we use xml to implement)
3. Add the method we wrote to all method sets ( Convenient for subsequent calls)
4. Write our control class and select the method to be used in our method set

The layers are interlocked, and our users use the exact opposite. In our control class, select all the methods to be called in the method set, and then push from top to bottom. For the other configuration, basically all files and data are given to us, and the problem is not very big.

Guess you like

Origin blog.csdn.net/weixin_52521533/article/details/123573870