一、导入如下模块
二、创建包
注意:resource是下的com.xyh.mapper的创建方式不是以“.”分割,而是以“/”分割 com/xyh/mapper
三、创建实体类,mapper接口,xml文件
注意:每个mapper接口上都要配置@Mapper的注解,表示为mapper类,还有一种方式,就是在启动类上配置@MapperScan
四、配置文件
在配置datasource的基础上配置
#整合mybatis
mybatis:
#实体类取别名
type-aliases-package: com.xyh.Pojo
#mapper地址
mapper-locations: classpath:com/xyh/mapper/*.xml
五、Controller
这里我直接调用了mapper接口,实际项目中Controller需要调用Service
package com.xyh.Controller;
import com.xyh.Pojo.User;
import com.xyh.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("selall")
public List<User> selall() {
//查询全部
List<User> querylist = userMapper.querylist();
return querylist;
}
}
六、运行
运行localhost:8080/selall 就可以输出user表中的所有信息