@Insert ===> <insert>
@Select ===> <select>
@Delete ===> <delete>
@Update ===> <update>
级联查询
一对一级联查询实现:
需求:查询学生的同时将学生所属班级信息都查询出来。
@Select("select * from t_student where id=#{id}")
@Results(id = "stu",value = {
@Result(id = true,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "age",column = "age"),
@Result(property = "cid",column = "cid"),
@Result(
property = "classes",
column = "cid",
javaType = Classes.class,
one = @One(select = "com.woniuxy.dao.ClassesDao.findById",fetchType = FetchType.EAGER)
)
})
Student findById(Integer id);
@Select("select * from t_classes where id=#{id}")
Classes findById(Integer id);
动态SQL
通过provider的方式可以来实现动态SQL。
@InsertProvider ===> <insert>
@SelectProvider ===> <select>
@DeleteProvider ===> <delete>
@UpdateProvider ===> <update>
删除多个id的记录。
public class CommonsUtil {
public String getDeleteSql(@Param("ids") List<Integer> ids){
String sql="delete from t_student where id in (";
for (int i = 0; i < ids.size(); i++) {
if (i<ids.size()-1) sql+=ids.get(i)+",";
else sql+=ids.get(i)+")";
}
return sql;
}
}
@DeleteProvider(type = CommonsUtil.class,method = "getDeleteSql")
Integer deleteByIds(@Param("ids") List<Integer> ids);
@****Provider注解:第一个参数是产生SQL语句的类的字节码对象,第二个参数是产生SQL语句的方法名称。