MyBatis Mapper接口的配置需要完成以下幾個步驟:
public interface UserMapper {
User getUserById(int id);
}
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<select id="getUserById" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
<!-- mybatis-config.xml -->
<configuration>
<mappers>
<package name="com.example.mapper"/>
</mappers>
</configuration>
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.getUserById(id);
}
}
這樣就完成了MyBatis Mapper接口的配置。注意,Mapper接口的命名規范和XML文件的命名規范需要按照約定來做,以便MyBatis能夠正確地進行映射和配置。