EmployeeMapper.class:
public interface EmployeeMapper{
public Employee getEmpById(Integer id)
public void insertEmp(Employee employee);
}
Employee.class:Employee的属性和get,set方法(少了无法在网页上显示)
EmployeeMapper接口:
public interface EmployeeMapper{
public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
}
mybatis-config.xml:mybatis的全局设置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>//设置驼峰命名法
</settings>
</configuration>
EmployeeMapper.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.lbl.springboot06.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.lbl.springboot06.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>
application.yml:配置xml的路径
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
最后:Controller.class:
@RestController//= @ResponseBody+@Controller
public class DeptController {
// @Autowired
// DepartmentMapper departmentMapper;
@Autowired//每个Mapper都需要一次自动注入
EmployeeMapper employeeMapper;
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return employeeMapper.getEmpById(id);
}
}