Home  >  Article  >  Database  >  How SpringBoot introduces mybatis and connects to Mysql database

How SpringBoot introduces mybatis and connects to Mysql database

PHPz
PHPzforward
2023-05-26 12:07:061806browse

Create a SpringBoot project

No need to go into details, introduce MyBaties, MySql dependencies

How SpringBoot introduces mybatis and connects to Mysql database

##Create mysql table

CREATE TABLE sp_users(
	`id` INT PRIMARY KEY,
	`username` VARCHAR(30),
	`age` INT
);

Just now This error kept appearing from the beginning, which made me doubt my life. The result was that ',' could not be added to the last line. Things are different and people are different.

How SpringBoot introduces mybatis and connects to Mysql database

INSERT INTO sp_users(id,`username`,`age`) VALUES(1,"张三",11);
INSERT INTO sp_users(id,`username`,`age`) VALUES(2,"李四",21);
INSERT INTO sp_users(id,`username`,`age`) VALUES(3,"游坦之",800);

How SpringBoot introduces mybatis and connects to Mysql database

Writing entity classes

How SpringBoot introduces mybatis and connects to Mysql database

Because Lombok is introduced, it is directly Used

package com.you.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class user {
    private int id;
    private String username;
    private int age;
}

to configure application.yaml

#datasource

spring:
datasource:
url: jdbc:mysql:///springboot?serverTimezone= UTC
username: root
password: your password
driver-class-name: com.mysql.cj.jdbc.Driver

Configure Mapper

Method 1, create UserMapper interface

##

package com.you.mapper;
import com.you.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
    @Select("select * from sp_users")
    public List<User> findAll();
}
How SpringBoot introduces mybatis and connects to Mysql databaseConfigure test class

package com.you.boot;
import com.you.boot.domain.User;
import com.you.boot.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BootMybatis03ApplicationTests {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testFindAll()
    {
        List<User> list = userMapper.findAll();
        System.out.println(list);
    }
}
How SpringBoot introduces mybatis and connects to Mysql databaseRendering diagram

How SpringBoot introduces mybatis and connects to Mysql databaseMethod 2 XML file configuration

Writing xml file, namespace must copy the full path, Copy/Copy Reference

<?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.you.boot.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select * from sp_users
    </select>
</mapper>
How SpringBoot introduces mybatis and connects to Mysql database When writing the UserXmlMapper interface, the purpose of @Repository is to solve the problem of becoming popular within the test class, although the popularity does not affect the running of the program.

package com.you.boot.mapper;
import com.you.boot.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
    public List<User> findAll();
}
How SpringBoot introduces mybatis and connects to Mysql databaseConfigure yaml, *Mapper value is all xml files with Mapper suffix

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

type-aliases-package: com.you.boot.domain

Write test class

package com.you.boot;
import com.you.boot.domain.User;
import com.you.boot.mapper.UserMapper;
import com.you.boot.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BootMybatis03ApplicationTests {
    @Autowired
    private UserXmlMapper userXmlMapper;
    @Test
    public void testFindAll2()
    {
        List<User> list = userXmlMapper.findAll();
        System.out.println(list);
    }
}
How SpringBoot introduces mybatis and connects to Mysql database Effect

The above is the detailed content of How SpringBoot introduces mybatis and connects to Mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete