1、导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
2、application.yml文件中配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
3、在启动类的同级创建文件夹
项目目录如下图所示
在mapper文件夹下创建**Mapper接口类
import java.util.List; import java.util.Map; public interface UserMapper { List<Map> userQuery(); }
在entity文件夹下创建实体类
import java.io.Serializable; public class UserEntity implements Serializable { private Integer id; private String userName; private String userPwd; public UserEntity() { } public UserEntity(Integer id, String userName, String userPwd) { this.id = id; this.userName = userName; this.userPwd = userPwd; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }
在service文件夹下创建service的接口以及实现方法,要在service的实现类上加@Service注解
service接口
import java.util.List; public interface UserService { List userQuery(); } service接口实现类
import com.sysh.mapper.UserMapper; import com.sysh.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper mapper; @Override public List userQuery() { return mapper.userQuery(); } }
在controller文件夹下创建访问接口
import com.sysh.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired UserService service; @RequestMapping("/user") public List hello(){ return service.userQuery(); } }
4、在resources文件下创建mapper文件
在resources文件下创建com/****/mapper/***Mapper.xml文件
注意:**Mapper文件路径和在Java文件下创建的**Mapper接口的路径一致
5、启动tomcat在浏览器地址栏输入http://localhost:8089/user即可访问