Springboot connects to the database through mybatis

One, create a project

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

2. Introduce dependencies

       <!--web核心依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
	   <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

		<!--mysql数据库驱动,用于连接数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

Third, the directory structure

Insert picture description here

Usercontroller.java:

package com.example.demo.mybatis.userController;

import com.example.demo.mybatis.dao.Userdao;
import com.example.demo.mybatis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    
    
    @Autowired
    Userdao userdao;

    @RequestMapping("/findAll")
    public List<User> query(){
    
    
        return userdao.findAll();
    }
}

User.java:
(used to receive the results returned by the database)

package com.example.demo.mybatis.model;

public class User {
    
    
    private String name;
    private String stuendId;
    private String teacherName;
    private String subject;
    private String grade;
    private String schoolName;
    private String age;
    private String fraction;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getStuendId() {
    
    
        return stuendId;
    }

    public void setStuendId(String stuendId) {
    
    
        this.stuendId = stuendId;
    }

    public String getTeacherName() {
    
    
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
    
    
        this.teacherName = teacherName;
    }

    public String getSubject() {
    
    
        return subject;
    }

    public void setSubject(String subject) {
    
    
        this.subject = subject;
    }

    public String getGrade() {
    
    
        return grade;
    }

    public void setGrade(String grade) {
    
    
        this.grade = grade;
    }

    public String getSchoolName() {
    
    
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
    
    
        this.schoolName = schoolName;
    }

    public String getAge() {
    
    
        return age;
    }

    public void setAge(String age) {
    
    
        this.age = age;
    }

    public String getFraction() {
    
    
        return fraction;
    }

    public void setFraction(String fraction) {
    
    
        this.fraction = fraction;
    }
}

Userdao.java:

package com.example.demo.mybatis.dao;

import com.example.demo.mybatis.model.User;
import java.util.List;

public interface Userdao {
    
    
    List<User> findAll();
}

Userdaoimpl.java:

package com.example.demo.mybatis.dao.impl;

import com.example.demo.mybatis.dao.Userdao;
import com.example.demo.mybatis.mapper.Usermapper;
import com.example.demo.mybatis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class Userdaoimpl implements Userdao {
    
    
    @Autowired
    Usermapper usermapper;

    @Override
    public List<User> findAll(){
    
    
        return usermapper.findAll();
    }
}

Usermapper.java:
(need to use @Mapper annotation, otherwise the startup class SpringBoot cannot be scanned)

package com.example.demo.mybatis.mapper;

import com.example.demo.mybatis.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface  Usermapper {
    
    
    List<User> findAll();
}

Usermapper.xml:
(1. The namespace needs to correspond to the interface using @Mapper 2. The
UserMapper.xml file name must be consistent with the interface using @Mapper
3. The id in the tag must be consistent with the method name in the @Mapper interface, And the parameters are the same
)

<?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.demo.mybatis.mapper.Usermapper">
    <select id="findAll" resultType="com.example.demo.mybatis.model.User">
        select * from tb_students;
    </select>
</mapper>

Configuration file application.yml:
(the url is your own mysql database ip and database name)

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/students?characterEncoding=utf8&useSSL=false
    username: root
    password: 123456

mybatis:
  mapper-locations: classpath:mapper/*.xml
  #开启驼峰命名
  configuration:
    map-underscore-to-camel-case: true

Start the class MybatisApplication.java:
(add a scan of @MapperScan)

package com.example.demo.mybatis;

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

@SpringBootApplication
@MapperScan("com.example.demo.mybatis.mapper")
public class MybatisApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(MybatisApplication.class, args);
    }
}

Fourth, the database table and data

create table tb_stusents(
     stuendId int not null,
     schoolName varchar(20),
     name varchar(5),
     age int,
     class varchar(4),
     grade varchar(3),
     subject varchar(3),
     fraction float,
     teacherName varchar(5)
 );
 
 insert into tb_stusents values (1,"南湖一中","张三",17,"高二","1班","语文",100.5,"刘梅");
 
 insert into tb_stusents values (2,"南湖一中","李四",17,"高三","2班","数学",99.5,"刘梅");

Five, start

Insert picture description here
Browser input: http://localhost:8081/user/findAll
Insert picture description here

Reference:
SpringBoot integration mybatis quick start

Guess you like

Origin blog.csdn.net/qq_33697094/article/details/110391474