开发者学堂课程【SpringBoot快速掌握 - 核心技术:整合 MyBatis(二)-配置版 MyBatis】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/612/detail/9277
整合 MyBatis(二)-配置版 MyBatis
一、配置版MyBatis
/@Mapper或者@ MapperScan将接口扫描装配到容器中public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http: //mybatis. org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com. atguigu.springboot .mapper . EmployeeMapper">
<!--public Employee getEmpById(Integer id);
public void insertEmp( Employee employee);-->
<select id= "getEmpById" resultType="com . atguigu. springboot . bean. Employee">
SELECT * FROM employee WHERE id=#{id}
</select>
<insert id="insertEmp">
INSERT INTO employee(lastName , email, gender,d_ id) VALUES (#{lastName}, #{email}, #{gender}, #{dId
</insert>
</mapper>
//指定这是一个操作数据库的mapper
//@Mapper
public interface DepartmentMapperk
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty ="id")
@Insert("insert into department(department_name) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set department_name=#{departmentName} where id=#{id}"
public int updateDept(Department department);
@Autowired
EmployeeMapper employeeMapper;
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable("id") Integer id){
return departmentMapper.getDeptById(id);
@GetMapping("/dept")
public Department insertDept(Department department){
departmentMapper.insertDept(department);
return department;
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return employeeMapper.getEmpById(id);
配置文件版
mybatis:
config-location:-classpath:mybatis/mybatis-config.xml
指定全局配置文件的位置
mapper-locations?
classpath:mybatis/mapper/*.xml
指定 sq1映射文件的位置