一、使用注解配置映射器 动态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:
1 public class StudentTest { 2 private static Logger logger = Logger.getLogger(StudentTest.class); 3 4 private SqlSession sqlSession=null; 5 private StudentMapper studentMapper=null; 6 7 @Before 8 public void setUp() throws Exception { 9 sqlSession=SqlSessionFactoryUtil.openSession(); 10 studentMapper=sqlSession.getMapper(StudentMapper.class); 11 } 12 13 @After 14 public void tearDown() throws Exception { 15 sqlSession.close(); 16 } 17 18 @Test 19 public void testInsertStudent() { 20 logger.info("测试insertStudent"); 21 Student stu = new Student("琪琪", 12); 22 int count = studentMapper.inertStudent(stu); 23 sqlSession.commit(); 24 } 25 26 @Test 27 public void testUpdateStudent() { 28 logger.info("测试updateStudent"); 29 Student stu = new Student("琪琪琪琪", 13); 30 stu.setId(16); 31 int count = studentMapper.updateStudent(stu); 32 sqlSession.commit(); 33 } 34 35 @Test 36 public void testDeleteStudent() { 37 logger.info("测试删除学生"); 38 int count = studentMapper.deleteStudent(16); 39 sqlSession.commit(); 40 } 41 42 @Test 43 public void testGetStudentById() { 44 logger.info("根据id查找学生"); 45 Student student = studentMapper.getStudentById(1); 46 System.out.println(student); 47 } 48 49 @Test 50 public void testFindStudents() { 51 logger.info("根据条件查找学生"); 52 Map<String,Object> map = new HashMap<String,Object>(); 53 // map.put("name", "%a%"); 54 map.put("age", 14); 55 List<Student> students = studentMapper.findStudents(map); 56 for(Student student: students){ 57 System.out.println(student); 58 } 59 } 60 }
时间: 2024-10-22 09:17:11