一、pom.xml 配置
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- MySQL连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、application.yml 配置
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=UTF-8
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
三、创建DAO类做数据持久层的工作与数据库进行交互
@Mapper
public interface UsersDAO {
/**
* 重新users表中的所有数据
*
* @return 返回查看到的数据
*/
@Select("select * from users")
List<UsersModel> selectAll();
}
四、创建数据实体类
public class UsersModel{
private long usersId;
private String usersName,usersToken;
public long getUsersId() {
return usersId;
}
public void setUsersId(long usersId) {
this.usersId = usersId;
}
public String getUsersName() {
return usersName;
}
public void setUsersName(String usersName) {
this.usersName = usersName;
}
public String getUsersToken() {
return usersToken;
}
public void setUsersToken(String usersToken) {
this.usersToken = usersToken;
}
}
public class BaseModel {
public BaseModel(boolean state, String msg, Object object) {
this.state = state;
this.msg = msg;
this.object = object;
}
private boolean state;
private String msg;
private Object object;
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
五、创建Service类负责业务模块的逻辑应用设计
@Service
public class UsersService {
@Autowired
private UsersDAO usersDAO;
public List<UsersModel> selectAll(){
return usersDAO.selectAll();
}
}
六、创建Controller
@RestController
public class HomeController {
@Autowired
private UsersService usersService;
@RequestMapping(value = "/",method = RequestMethod.GET)
public BaseModel index(){
return new BaseModel(true,null,usersService.selectAll());
}
}
七、启动项目,发起请求。