一、使用注解配置映射器 动态sql:
用的并不是很多,了解下;
Student.java 实体bean:
package com.cy.model; public class Student{
private Integer id;
private String name;
private Integer age; public Student(){ } public Student(String name, Integer age){
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 + "]";
} }
com.cy.mapper下:
StudentDynaSqlProvider.java:
StudentMapper.java:
package com.cy.mapper; import java.util.Map; import org.apache.ibatis.jdbc.SQL; import com.cy.model.Student; public class StudentDynaSqlProvider { public String inertStudent(final Student student){
return new SQL(){
{
INSERT_INTO("t_student");
if(student.getName()!=null){
VALUES("name", "#{name}");
}
if(student.getAge()!=null){
VALUES("age", "#{age}");
}
}
}.toString();
} public String updateStudent(final Student student){
return new SQL(){
{
UPDATE("t_student");
if(student.getName()!=null){
SET("name=#{name}");
}
if(student.getAge()!=null){
SET("age=#{age}");
}
WHERE("id=#{id}");
}
}.toString();
} public String deleteStudent(){
return new SQL(){
{
DELETE_FROM("t_student");
WHERE("id=#{id}");
}
}.toString();
} public String getStudentById(){
return new SQL(){
{
SELECT("*");
FROM("t_student");
WHERE("id=#{id}");
}
}.toString();
} public String findStudents(final Map<String, Object> map){
return new SQL(){
{
SELECT("*");
FROM("t_student");
StringBuffer sb = new StringBuffer();
if(map.get("name")!=null){
sb.append(" and name like '"+map.get("name")+"'");
}
if(map.get("age")!=null){
sb.append(" and age ="+map.get("age"));
}
if(!sb.toString().equals("")){
WHERE(sb.toString().replaceFirst("and", ""));
}
}
}.toString();
}
}
StudentMapper.java:
public interface StudentMapper { //插入
@InsertProvider(type=StudentDynaSqlProvider.class,method="inertStudent")
public int inertStudent(Student student); //更新
@UpdateProvider(type=StudentDynaSqlProvider.class,method="updateStudent")
public int updateStudent(Student stu); //删除
@DeleteProvider(type=StudentDynaSqlProvider.class,method="deleteStudent")
public int deleteStudent(int id); //根据id查找学生
@SelectProvider(type=StudentDynaSqlProvider.class,method="getStudentById")
public Student getStudentById(Integer id); //查询所有学生
@SelectProvider(type=StudentDynaSqlProvider.class,method="findStudents")
public List<Student> findStudents(Map<String,Object> map); }
测试代码:
StudentTest.java:
public class StudentTest {
private static Logger logger = Logger.getLogger(StudentTest.class); private SqlSession sqlSession=null;
private StudentMapper studentMapper=null; @Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
} @After
public void tearDown() throws Exception {
sqlSession.close();
} @Test
public void testInsertStudent() {
logger.info("测试insertStudent");
Student stu = new Student("琪琪", 12);
int count = studentMapper.inertStudent(stu);
sqlSession.commit();
} @Test
public void testUpdateStudent() {
logger.info("测试updateStudent");
Student stu = new Student("琪琪琪琪", 13);
stu.setId(16);
int count = studentMapper.updateStudent(stu);
sqlSession.commit();
} @Test
public void testDeleteStudent() {
logger.info("测试删除学生");
int count = studentMapper.deleteStudent(16);
sqlSession.commit();
} @Test
public void testGetStudentById() {
logger.info("根据id查找学生");
Student student = studentMapper.getStudentById(1);
System.out.println(student);
} @Test
public void testFindStudents() {
logger.info("根据条件查找学生");
Map<String,Object> map = new HashMap<String,Object>();
// map.put("name", "%a%");
map.put("age", 14);
List<Student> students = studentMapper.findStudents(map);
for(Student student: students){
System.out.println(student);
}
}
}