domain/User.class
package com.spring.boot.domain;
public class User {
private int id;
private String username;
private String password;
// 略过set、get、toString
}
mapper/UserMapper
package com.spring.boot.mapper;
import com.spring.boot.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from t_user")
List<User> findAll();
}
主启动类略过
application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=UTC #时区
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
TestApplication.class
import com.spring.boot.Application;
import com.spring.boot.domain.User;
import com.spring.boot.mapper.UserMapper;
import com.spring.boot.mapper.UserXmlMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest(classes = Application.class) //主启动类
@RunWith(SpringRunner.class) //junit测试类
public class TestApplication {
@Autowired
UserMapper userMapper;
@Test
public void findAllTest() {
List<User> users = userMapper.findAll();
System.out.println(users);
}
}