mybatis_高级

注解方式:

不需要配置文件

@select等注解直接放在接口mapper里

多表操作:

@Select("select * from user1")
    @Results({
            @Result(column = "name",property="name"),
            @Result(
                    property = "us2", //被包含对象的变量名
                    javaType = user2.class, //被包含对象实际数据类型
                    column = "id",  //根据user1中查出的id来查询user2表
                    /*
                        one:一对一固定写法
                        select属性:指定调用哪个接口的哪个方法
                     */
                    one = @One(select = "org.example.mapper.user2Mapper.selectByID")
            )
    })
    public abstract List<user1> one_one();
    @Select("select *from user2")
    public List<user2> all();
    @Select("select *from class")
    @Results({
            @Result(column = "name",property = "name"),
            @Result(
                    property = "std",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "org.example.mapper.user2Mapper.select_one_duo")

            )

    })
    public List<classs> one_duo();
    @Select("SELECT DISTINCT s.id,s.name,s.age FROM students s,stu_or sc where sc.sid=s.id")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age"),
            @Result(
                    property = "c",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "org.example.mapper.user2Mapper.select_duo_duo")


            )
    })
    public List<student> duo_duo();
}
public interface user2Mapper {
    @Select("select *from user2 where ids=#{ids}")
    public List<user2> selectByID(int ids);
    @Select("select *from students where cid=#{cid}")
    public  List<student> select_one_duo(int cid);
    @Select("select c.id,c.name from stu_or sc,course c where sc.cid=cid and sc.sid=#{id}")
    public List<course> select_duo_duo(int id);
}

构建sql语句

posted @ 2023-02-03 17:41  会秃头的小白  阅读(8)  评论(0编辑  收藏  举报