首頁 >資料庫 >mysql教程 >SpringBoot怎麼引進mybatis與連接Mysql資料庫

SpringBoot怎麼引進mybatis與連接Mysql資料庫

PHPz
PHPz轉載
2023-05-26 12:07:061877瀏覽

建立一個SpringBoot專案

其他不贅敘了,引入MyBaties、MySql依賴

SpringBoot怎麼引進mybatis與連接Mysql資料庫

建立mysql表

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

剛開始一直出現這個錯誤,弄的我懷疑人生,結果是最後一行不能加',' ,物是人非。

SpringBoot怎麼引進mybatis與連接Mysql資料庫

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);

SpringBoot怎麼引進mybatis與連接Mysql資料庫

寫實體類別

SpringBoot怎麼引進mybatis與連接Mysql資料庫

因為引進了Lombok,所以直接用了

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;
}

設定application.yaml

#datasource
spring:
  datasource:


datasource
spring:

  datasource:
#    url: jdbc:mysql:///springboot?serverTimezone= UTC

    username: root

    password: 你的密碼

    driver-class-name: com.mysql.cj.jdbc.Driver

SpringBoot怎麼引進mybatis與連接Mysql資料庫Mapper

Mapperbc.jdbc.Driver

SpringBoot怎麼引進mybatis與連接Mysql資料庫Mapper

Mapperbc.方式一,建立UserMapper介面

SpringBoot怎麼引進mybatis與連接Mysql資料庫

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();
}

設定測試類別

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);
    }
}
SpringBoot怎麼引進mybatis與連接Mysql資料庫效果圖

SpringBoot怎麼引進mybatis與連接Mysql資料庫#方法二XML檔案設定

編寫xml文件,namespace一定要複製全路徑,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>

編寫UserXmlMapper接口,@Repository的作用是解決在測試類別內爆紅,雖然爆紅不影響程式的運作。

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();
}

設定yaml,*Mapper值得是所有後綴是Mapper的xml檔SpringBoot怎麼引進mybatis與連接Mysql資料庫

mybatis:

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

  type-aliases-package: com.you.boot.domainSpringBoot怎麼引進mybatis與連接Mysql資料庫

####寫測試類別############
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);
    }
}
###效果############

以上是SpringBoot怎麼引進mybatis與連接Mysql資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除