1.需要的starter:
2.需要的 配置
# DB 配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=root
spring.datasource.username=root
# 必须配置时区,不然报错
spring.datasource.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
# 配置扫描实体类
mybatis.type-aliases-package=com.itheima.domain
# 配置扫描UserMapper.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#工程启动端口
server.port=9090
# redis 配置
#spring.redis.host=localhost
#spring.redis.port=6379
3.dao层
/**
* @author :gzy
* @date :Created in 2019/10/20
* @description :
* @version: 1.0
*/
@Repository
@Mapper//表明当前接口是一个Mapper,被Mybatis框架扫描
public interface UserDao {
List<User> findall();
}
注意:我这里用的## Free Mybatis plugin插件生成的mapper.xml文件。
生成的mapper.xml:
<?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.guo.lianxi1020.dao.UserDao">
<select id="findall" resultType="com.guo.lianxi1020.domain.User">
select * from users
</select>
</mapper>
3.server
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findall() {
return userDao.findall();
}
}
4.control
package com.guo.lianxi1020.control;
import com.guo.lianxi1020.domain.User;
import com.guo.lianxi1020.service.impl.UserServiceImpl;
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;
/**
* @author :gzy
* @date :Created in 2019/10/20
* @description :
* @version: 1.0
*/
@RestController
@RequestMapping("user")
public class UserControl {
@Autowired
private UserServiceImpl userService;
@RequestMapping("all")
public List<User> showAll(){
return userService.findall();
}
}