Application. Yml
mybatis:
mapper-locations: classpath:mapper/*.xml # represents all XML files in the resources/mapper directory
Copy the code
Create mapper. Java and mapper. XML
Mapper. Java and mapper. XML are created in pairs
// EmployeeMapper.java
/ /... Omit package name and import
public interface EmployeeMapper {
List<Employee> findAll(a);
}
Copy the code
<! -- EmployeeMapper.xml -->
<! DOCTYPEmapper PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.example.dao.EmployeeMapper">
<resultMap id = "Employee" type = "com.example.pojo.Employee">
<result property = "id" column = "id"/>
<result property = "name" column = "name"/>
<result property = "role" column = "role"/>
</resultMap>
<select id = "findAll" resultMap = "Employee">
SELECT * FROM payroll
</select>
</mapper>
Copy the code
Add MapperScan annotations
@SpringBootApplication
@MapperScan("com.example.payroll.dao") // replace the package with * mapper. Java in parentheses
public class PayrollApplication {
public static void main(String[] args) { SpringApplication.run(PayrollApplication.class, args); }}Copy the code