<bean id="studentService"
class="org.abin.lee.service.impl.StudentServiceImpl">
<property name="studentDao">
<ref bean="studentDao" />
</property>
</bean>
<bean id="teacherService"
class="org.abin.lee.service.impl.TeacherServiceImpl">
<property name="teacherDao">
<ref bean="teacherDao" />
</property>
</bean>
</beans>
mybatis-config.xml(直接放在src目錄下面)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"<configuration>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias type="org.abin.lee.bean.Student" alias="Student" />
<typeAlias type="org.abin.lee.bean.Teacher" alias="Teacher" />
</typeAliases>
<mappers>
<mapper resource="org/abin/lee/bean/StudentMapper.xml" />
<mapper resource="org/abin/lee/bean/TeacherMapper.xml" />
</mappers>
</configuration>
log4j.properties(直接放在src目錄下面)
log4j.rootLogger=INFO,stdout,logfile
//log4j.rootLogger=INFO,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=../logs/contacts.log
log4j.appender.logfile.MaxFileSize=2048KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} (%c) - %m%n
下面是測試代碼:
package org.abin.lee.test;
import junit.framework.TestCase;
import org.abin.lee.bean.Student;
import org.abin.lee.bean.Teacher;
import org.abin.lee.service.StudentService;
import org.abin.lee.service.TeacherService;
import org.junit.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStudent extends TestCase{
private StudentService studentService;
private TeacherService teacherService;
private ApplicationContext context;
@Before
public void setUp(){
String[] xml=new String[3];
xml[0]="org/abin/lee/spring/applicationContext-resource.xml";
xml[1]="org/abin/lee/spring/applicationContext-mapper.xml";
xml[2]="org/abin/lee/spring/applicationContext-service.xml";
context=new ClassPathXmlApplicationContext(xml);
}
public void testStudent(){
Teacher tea=new Teacher();
tea.setTname("steven");
teacherService=(TeacherService)context.getBean("teacherService");
this.teacherService.insertTeacher(tea);
Student stu=new Student();
stu.setSname("john");
stu.setTeacherId(tea.getTid());
studentService=(StudentService)context.getBean("studentService");
this.studentService.insertStudent(stu);
}
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public TeacherService getTeacherService() {
return teacherService;
}
public void setTeacherService(TeacherService teacherService) {
this.teacherService = teacherService;
}
}
package org.abin.lee.test;
import java.util.List;
import junit.framework.TestCase;
import org.abin.lee.bean.Student;
import org.abin.lee.bean.Teacher;
import org.abin.lee.service.StudentService;
import org.abin.lee.service.TeacherService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestFind extends TestCase{
private StudentService studentService;
private TeacherService teacherService;
private ApplicationContext context;
@Before
public void setUp(){
String[] xml=new String[3];
xml[0]="org/abin/lee/spring/applicationContext-resource.xml";
xml[1]="org/abin/lee/spring/applicationContext-mapper.xml";
xml[2]="org/abin/lee/spring/applicationContext-service.xml";
context=new ClassPathXmlApplicationContext(xml);
}
@Test
public void testFind(){
teacherService=(TeacherService)context.getBean("teacherService");
studentService=(StudentService)context.getBean("studentService");
List<Student> list=this.studentService.getStudentById(4);
Student stu=new Student();
Teacher tea=new Teacher();
for(int i=0;i<list.size();i++){
System.out.println("666");
stu=list.get(i);
System.out.println("sid="+stu.getSid()+",sname="+stu.getSname()+",teacherid="+stu.getTeacherId());
tea=stu.getTeacher();
System.out.println("tid="+tea.getTid()+",tname="+tea.getTname());
}
}
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public TeacherService getTeacherService() {
return teacherService;
}
public void setTeacherService(TeacherService teacherService) {
this.teacherService = teacherService;
}
}