其他不贅敘了,引入MyBaties、MySql依賴
CREATE TABLE sp_users( `id` INT PRIMARY KEY, `username` VARCHAR(30), `age` INT );
剛開始一直出現這個錯誤,弄的我懷疑人生,結果是最後一行不能加',' ,物是人非。
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);
因為引進了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
# url: jdbc:mysql:///springboot?serverTimezone= UTC#datasource
datasource:
spring:
datasource:
datasource
spring:
driver-class-name: com.mysql.cj.jdbc.Driver
Mapper
Mapperbc.jdbc.Driver
Mapper
Mapperbc.方式一,建立UserMapper介面
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); } }
#方法二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檔
mybatis:
mapper-locations: classpath: mapper/*Mapper.xmltype-aliases-package: com.you.boot.domain
####寫測試類別############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中文網其他相關文章!