这一节讲述使用注解方式实现动态sql。所谓的动态sql就是动态的拼接sql语句,之前讲过使用xml文件方式进行配置,这里讲述使用注解方式进行动态拼接。
1.新建工程MyBatisPro04,按照之前的配置配置好。
2.我们这里只使用Student一个类。新建Student类:
package com.test.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
3.新建接口StudentMapper:
package com.test.mappers;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;
import com.test.model.Student;
public interface StudentMapper {
public int insertStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(int id);
public Student getStudentById(Integer id);
public List<Student> findStudents(Map<String,Object> map);
}
这里有插入、修改、删除、查找单个、查找所有学生的方法接口。
3.要想使用注解的方式实现动态s