一.核心依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
二.配置相关yml文件
server:
port: 8090
spring:
datasource:
# 使用阿里的Druid连接池
type: com.alibaba.druid.pool.DruidDataSource
# 填写你数据库的url、登录名、密码和数据库名
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
mybatis:
mapper-locations: classpath:mybatis/*.xml
type-aliases-package: com.msb.pojo
四.代码具体实现
①实体类:
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
private Integer uid;
private String uname;
private String upassword;
}
②业务层以及实现层
public interface UserService {
List<User> findAllUser();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List findAllUser() {
return userMapper.selectAll();
}
}
③mapper层以及核心sql语句层
@Mapper
public interface UserMapper {
List selectAll();
}
④控制层
@Controller
@RequestMapping(“user”)
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("findAllUser")
@ResponseBody
public List<User> findAllUser(){
return userService.findAllUser();
}
}
⑥结果:
在浏览器输入local host:8090/user/findAllUser