[ibatis]ダイナミックマッピング

1559 ワード

原文の住所:
http://exceptioneye.iteye.com/blog/1054387
student.xmlのコード
<select id="dynamicSelectStudent" parameterClass="Student"
		resultMap="studentResult">
		select ID, NAME, PASSWORD, BIRTHDAY, CREATE_TIME from student
		<dynamic prepend="WHERE">
			<isNotEmpty prepend="AND" property="name">
				NAME like '%$name$%'
			</isNotEmpty>
			<isNotEmpty prepend="AND" property="password">
				PASSWORD like '%$password$%'
			</isNotEmpty>
			
		</dynamic>
	</select>
TestString.javaでテストコード
package test.wendellup;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wendellup.biz.IStudentService;
import com.wendellup.entity.Student;

public class TestSpring {
	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
		IStudentService iStudentService = (IStudentService) ac.getBean("studentServiceImpl");
//		System.out.println(iStudentService);
		Student student = new Student();
		student.setName("a");
		student.setPassword("a");
		List<Student> studentList = iStudentService.dynamicSelectStudent(student);
		for(Student stu : studentList){
		    System.out.println(stu);
		}
	}
}