No need to go into details, introduce MyBaties, MySql dependencies
##Create mysql tableCREATE 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);
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; }
#datasourceConfigure Mapper Method 1, create UserMapper interfacespring:
datasource:
url: jdbc:mysql:///springboot?serverTimezone= UTC
username: root
password: your password
driver-class-name: com.mysql.cj.jdbc.Driver
##
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); } }
Method 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>
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(); }
mapper-locations: classpath: mapper/*Mapper.xmltype-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); } }
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!