30-annotation-sql

  
dao

public interface IStudentDao {
    // 1. 注解的首字母是大写的。因为注解是Java中类层级的成员之一。类层级成员:类、接口、枚举,及注解。
    // 2. 对于数组类型的属性赋值,使用{元素,元素,……}形式
    // 3. 若数组类型属性只有一个元素值,那么,{}可以省略
    // 4. 若一个注解只使用到了一个属性,且为value属性,那么这个属性名value可以省略
    
    @Insert(value={"insert into student(name,age,score) values(#{name}, #{age}, #{score})"})
    void insertStudent(Student student);
    
    @Insert(value={"insert into student(name,age,score) values(#{name}, #{age}, #{score})"})
    @SelectKey(statement="select last_insert_id()", keyProperty="id", before=false, resultType=int.class)
    void insertStudentCatcheId(Student student);
    
    @Delete("delete from student where id=#{xxx}")
    void deleteById(int id);
    
    @Update("update student set name=#{name}, age=#{age}, score=#{score} where id=#{id}")
    void updateStudent(Student student);
    
    @Select("select id,name,age,score from student where id=#{ooo}")
    Student selectStudentById(int id);
}

provider

public class MySqlProvider {
    
    public String getSelectSql() {
        return "select id,name,age,score from student";
    }
    
    public String getSelectSql2() {
        return "select id,name,age,score from student where id=#{ooo}";
    }
    
    public String getSelectSql3(Student student) {
        
        StringBuffer sql = new StringBuffer(); 
        
        sql.append("select id,name,age,score from student where 1=1");
        if(student.getName() != null && !student.getName().equals("")) {
            sql.append(" and name like '%' #{name} '%'");
        }
        if(student.getAge() > 0) {
            sql.append(" and age > #{age}");
        }
        if(student.getScore() > 0) {
            sql.append(" and score < #{score}");
        }
        
        return sql.toString();
    }
    
    public String getSelectSql4() {
        return  "select id,name,age,score from student where name like '%' #{ccname} '%' and age > #{ccage} and score < #{ccscore}";
    }
    
    public String getInsertSql() {
        return  "insert into student(name,age,score) values(#{name}, #{age}, #{score})";
    }
    
    public String getDeleteSql() {
        return  "delete from student where id=#{xxx}";
    }
    
    public String getUpdateSql() {
        return  "update student set name=#{name},age=#{age},score=#{score} where id=#{id}";
    }
    
    public String getUpdateSql2(Student student) {
        StringBuffer sql = new StringBuffer();
        sql.append("update student set id=#{id}");
        
        if(student.getName() != null && !student.getName().equals("")) {
            sql.append(",name=#{name}");
        }
        
        if(student.getAge() > 0) {
            sql.append(",age=#{age}");
        }
        
        if(student.getScore() > 0) {
            sql.append(",score=#{score}");
        }
        
        sql.append(" where id=#{id}");
        return  sql.toString();
    }
    
    public String getUpdateSql3(Student student) {
        // new SQL(){} 表示创建一个SQL类的子类对象,只不过这个子类叫什么名称不知道,没有指定。
        // 所以,这种写法是匿名内部类的写法。
        return  new SQL(){
            
            // 下面的代码在何时会被执行?
            // 当当前类(SQL类的子类)的无参构造器被调用时,会自动执行这段{}代码。
            // 在类中的{}称为实例代码块
            {
                this.UPDATE("student");
                this.SET("id=#{id}");
                if(student.getName() != null && !student.getName().equals("")) {
                    this.SET("name=#{name}");
                }
                
                if(student.getAge() > 0) {
                    this.SET("age=#{age}");
                }
                
                if(student.getScore() > 0) {
                    this.SET("score=#{score}");
                }
                
                this.WHERE("id=#{id}");
            }
        }.toString();
        
    }
    
    
    
    
    
    
    
    
    
}

 

posted @ 2018-11-05 23:52  深沉有点事  阅读(190)  评论(0编辑  收藏  举报