@Insert 新增
@Insert("insert into student(sname,birthday,ssex,classid)"
+ "values(#{sname},#{birthday},#{ssex},#{classid})")
@Options(keyProperty = "sid", useGeneratedKeys = true)
public int addStudent(Student s);
@Delete 删除
@Delete("delete from student where sid = #{v}")
public int delStudent(int sid);
@Update 修改
@Update("update student set sname=#{sname},birthday=#{birthday},"
+ "ssex=#{ssex},classid = #{classid} where sid = #{sid}")
public int updateStudent(Student s);
@Selete 查询
@Select("select * from student")
public List<Student> findAllStudent();
@Param 方式 传递多参
@Select("select * from student where ssex = #{xingbie} limit #{weizhi},#{sizepage}")
public List<Student> findStudentBySexPage(
@Param("xingbie") String sex,
@Param("weizhi") int curpage,
@Param("sizepage") int sizepage
);
@Results 结果映射
@Result(column = "smid", property = "smid")
@Results 复用
@Results(id = "smMap", value = {
@Result(column = "smid", property = "smid"),
@Result(column = "sm_name", property = "smname"),
@Result(column = "smsex",property = "smsex")
})
语法
@one(select="需要对应的方法的路径")
@Results({
@Result(column = "classid", property = "classid"),
@Result(property = "bj",column = "classid",
one = @One(select = "com.ape.mapper.BanjiMapper.findBanjiByClassid") )
})
@Select("select * from student")
public List<Student> findStudentAndClass();
语法
@Many(select="需要对应的方法的路径")
@Results(id = "bj_stu_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> findAllBanjiAndStu();
在sql语句中加入标签,按照之前sqlmap中的动态sql的样式书写
// 脚本Sql
@Select("")
public List<Student> findStudent(Student s);
在方法中构建动态SQL
@SelectProvider(type = StudentSql.class, method = "chaxunStuSql" )
public List<Student> findStudentFun(Student s);
class StudentSql{
public String chaxunStuSql(Student s) {
String sql = "select * from student where 1=1 ";
if(s.getSsex() != null) {
sql += " and ssex = #{ssex}";
}
if(s.getClassid() != 0) {
sql += " and classid = #{classid}";
}
return sql;
}
}
方法中的方法名要与@SelectProvider中的method参数一致,type对应都是内部类的类名
使用SQL构造器
构造器的结构:
return new SQL(){
{
}
}.toString();
@SelectProvider(type = StudentSql.class, method = "gzqchaxun")
public List<Student> findStudentGZQ(Student s);
@UpdateProvider(type = StudentSql.class, method = "gzqxiugai")
public int updateStudentGZQ(Student s);
@InsertProvider(type = StudentSql.class, method = "gzqtianjia")
@Options(keyProperty = "sid",useGeneratedKeys = true)
public int addStudentGZQ(Student s);
@DeleteProvider(type = StudentSql.class, method = "gzqshanchu")
public int delStudentGZQ(int sid);
class StudentSql{
public String gzqchaxun(Student s) {
return new SQL() {
{
// SELECT("sid");
// SELECT("sname");
// SELECT("birthday,ssex,classid");
SELECT("*");
FROM("student");
if(s.getSsex() != null) {
WHERE("ssex = #{ssex}");
}
if(s.getClassid() != 0) {
OR();
WHERE("classid = #{classid}");
}
}
}.toString();
}
public String gzqxiugai(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 gzqtianjia(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 gzqshanchu(int sid) {
return new SQL() {
{
DELETE_FROM("student");
WHERE("sid = #{v}");
}
}.toString();
}
}