1.繁琐:配置文件的书写本身繁琐,需要掌握的内容比较多
2.不直观:配置文件和接口直接只是名称相同,对应起来比较麻烦.
@Insert("insert into student(sname,birthday,ssex,classid) "
+ "values(#{sname},#{birthday},#{ssex},#{classid})")
@Options(keyProperty = "sid",useGeneratedKeys = true)
public int addStu(Student s);
@SelectKey ( statement = "select max(newsid) + 1 as newsId from news", keyProperty = "newsId", resultType = int.class, before = true )
@Insert(...)
public void addNews (News news);
@Delete(“delete from student where sid= #{sId} ”)
public int delStudent(int sid);
@Update(“update student set ssex =#{ssex},classid = #{classid})
public int updateStudent(Student s);
@Select("select * from student")
public List<Student> findStudents();
方法5:@Param 方式
@Select("select * from student where ssex=#{xingbie} limit #{weizhi},#{sizepage}")
public List<Student> findStudentsBysexpage(
@Param("xingbie") String sex,
@Param("weizhi") int curpage,
@Param("sizepage") int sizepage
);
@Results(id = "stuMap" , value= {
@Result(column = "sid", property = "sid"),
@Result(column = "sname", property = "sname"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "ssex", property = "ssex"),
@Result(column = "classid", property = "classid")
})
@Select("select * from student")
public List<Student> findStudent();
一对一:@one
@Results(id = "stuMap" , value= {
@Result(column = "sid", property = "sid"),
@Result(column = "sname", property = "sname"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "ssex", property = "ssex"),
@Result(column = "classid", property = "classid"),
//一对一
@Result(property = "bj",column = "classid" ,
one = @One(select = "com.ape.mapper.ClassMapper.findBanjiList")
)
})
@Select("select * from student")
public List<Student> findStudentAndClass();
一对多:@many
@Results(id= "class_map" , value = {
@Result(column="classid",property="classid"),
//一对多
@Result(column = "classid",property = "slist",
many = @Many(select = "com.ape.mapper.StudentMapper.findStudentByclassid")
)
})
@Select("select * from class")
public List<Banji> findBanjiAndStu();
1)脚本sql:XML配置方式的动态SQL,是用< script >的方式把它照搬过来,用注解来实现。适用于xml配置转换到注解配置
//脚本SQL
@Select("")
public List findStudents(Student s);
2)方法中构建sql:
//在方法中构建动态SQL
@UpdateProvider(type = StudentSql.class, method = "xiugaiStuSql")
public int updateStudentFun(Student s);
//内部类
class StudentSql{
public String xiugaiStuSql(Student s) {
String sql = "update student set ";
if (s.getSsex() !=null) {
sql += ",ssex =#{ssex}";
}
if (s.getClassid() != 0) {
sql += ",classid =#{classid}";
}
sql += " where sid= #{sid}";
sql = sql.replaceFirst(",", " ");
return sql;
}
}
3)SQL 语句构造器:实现动态 SQL 的内容(推荐)
//SQL构造器
@SelectProvider(type = StudentSql.class , method = "findGZQ")
public List<Student> findStudentsGZQ(Student s);
@InsertProvider(type = StudentSql.class , method = "gzqADD")
public int addStudent(Student s);
@UpdateProvider(type = StudentSql.class , method = "gzqUpdate")
public int updateStudent(Student s);
@DeleteProvider(type = StudentSql.class , method = "gzqDel")
public int delStudent(int sid);
public String findGZQ(Student s) {
return new SQL() {
{
SELECT("*");
FROM("student");
if (s.getSsex() != null) {
WHERE("ssex = #{ssex}");
}
if (s.getClassid() != 0) {
OR();
WHERE("classid = #{classid}");
}
}
}.toString();
}
public String gzqADD(Student s) {
return new SQL() {
{
INSERT_INTO("student");
if (s.getSname() !=null) {
VALUES("sname","#{sname}");
}
if (s.getBirthday() !=null) {
VALUES("birthday","#{birthday}");
}
if (s.getSsex() !=null) {
VALUES("ssex","#{ssex}");
}
if (s.getClassid() !=0) {
VALUES("classid","#{classid}");
}
}
}.toString();
}
public String gzqUpdate(Student s) {
return new SQL() {
{
UPDATE("student");
if (s.getSname() !=null) {
SET("sname = #{sname}");
}
if (s.getBirthday() !=null) {
SET("birthday=#{birthday}");
}
if (s.getSsex() !=null) {
SET("ssex=#{ssex}");
}
if (s.getClassid() !=0) {
SET("classid= #{classid}");
}
WHERE("sid = #{sid}");
}
}.toString();
}
public String gzqDel(int sid) {
return new SQL() {
{
DELETE_FROM("student");
WHERE("sid = #{sid}");
}
}.toString();
}
}