Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Result(property = "parent", column = "parent_id", javaType = Parent.class, one = @One(select = "com.mybatis.parent.findById")),
- Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
- ### Error querying database. Cause: java.lang.IllegalArgumentException:
- Mapped Statements collection does not contain value for com.mybatis.parent.findById
- ### The error may exist in com/mybatis/child/ChildMapper.java (best guess)
- ### The error may involve com.mybatis.child.ChildMapper.findById
- ### The error occurred while handling results
- ### SQL: select c.id, c.name, c.age, c.parent_id from child c where c.id = ?
- ### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mybatis.parent.findById
- public class Child {
- private Long id;
- private String name;
- private Integer age;
- private Parent parent;
- public Child() {
- }
- //Getters and Setters
- @Mapper
- public interface ChildMapper {
- @Select("select c.id, c.name, c.age, c.parent_id from child c where c.id = #{id}")
- @Results(value = {
- @Result(property = "parent", column = "parent_id", javaType = Parent.class, one = @One(select = "findParentById")),
- })
- Child findById(Long id);
- @Select("select p.id, p.name, p.age from parent p where p.id = #{id}")
- Parent findParentById(Long id);
- }
- @Repository
- public class ChildRepository {
- private ChildMapper childMapper;
- public ChildRepository(ChildMapper childMapper) {
- this.childMapper = childMapper;
- }
- public Child findByid(Long id){
- return this.childMapper.findById(id);
- }
- }
- public class Parent {
- private Long id;
- private String name;
- private Integer age;
- public Parent() {
- }
- //Getters and Setters
- @Mapper
- public interface ParentMapper {
- @Select("select p.id, p.name, p.age from parent p where p.id = #{id}")
- Parent findById(Long id);
- }
- @Repository
- public class ParentRepository {
- private ParentMapper parentMapper;
- public ParentRepository(ParentMapper parentMapper) {
- this.parentMapper = parentMapper;
- }
- public Parent findByid(Long id){
- return this.parentMapper.findById(id);
- }
- }
- @SpringBootApplication
- public class ParentChildApplication implements CommandLineRunner{
- public static void main(String[] args) {
- SpringApplication.run(ParentChildApplication.class, args);
- }
- @Autowired
- ChildRepository childRepository;
- @Autowired
- ParentRepository parentRepository;
- @Override>
- public void run(String... strings) throws Exception {
- Child child = this.childRepository.findByid(2L);
- Parent parent = this.parentRepository.findByid(1L);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement