mysql many to one_Mybatis one-to-many(一对多)

CREATE TABLE `teacher` (

`tid` int(11) NOT NULL AUTO_INCREMENT,

`tname` varchar(100) DEFAULT NULL,

PRIMARY KEY (`tid`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

CREATE TABLE `student` (

`sid` int(11) NOT NULL AUTO_INCREMENT,

`sname` varchar(100) DEFAULT NULL,

`teacherid` int(11) DEFAULT NULL,

PRIMARY KEY (`sid`),

KEY `ftid` (`teacherid`),

CONSTRAINT `ftid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`tid`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

package org.abin.lee.bean;

public class Student implements java.io.Serializable{

private static final long serialVersionUID = 2664268794619785937L;

private int sid;

private String sname;

private int teacherId;

private Teacher teacher;

public Student() {

}

public int getSid() {

return sid;

}

public void setSid(int sid) {

this.sid = sid;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

public Teacher getTeacher() {

return teacher;

}

public void setTeacher(Teacher teacher) {

this.teacher = teacher;

}

public int getTeacherId() {

return teacherId;

}

public void setTeacherId(int teacherId) {

this.teacherId = teacherId;

}

}

package org.abin.lee.bean;

import java.util.ArrayList;

import java.util.List;

public class Teacher implements java.io.Serializable{

private static final long serialVersionUID = -7053173500969534203L;

private int tid;

private String tname;

private List student=new ArrayList();

public Teacher() {

}

public int getTid() {

return tid;

}

public void setTid(int tid) {

this.tid = tid;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

public List getStudent() {

return student;

}

public void setStudent(List student) {

this.student = student;

}

}

StudentMapper.xml(org.abin.lee.bean)

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT LAST_INSERT_ID() AS ID

INSERT INTO student(sname,teacherId)

VALUES(#{sname},#{teacherId})

SELECT * FROM student

WHERE sid=#{sid}

SELECT * FROM student

WHERE teacherId=#{teacherId}

TeacherMapper.xml(org.abin.lee.bean)

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

SELECT @@IDENTITY AS ID

INSERT INTO teacher(tname)

VALUES(#{tname})

SELECT *

FROM teacher

WHERE tid=#{id}

package org.abin.lee.dao;

import java.util.List;

import org.abin.lee.bean.Student;

public interface StudentDao {

void insertStudent(Student student);

List getStudent(int id);

List getStudentById(int teacherId);

}

package org.abin.lee.dao;

import org.abin.lee.bean.Teacher;

public interface TeacherDao {

void insertTeacher(Teacher teacher);

Teacher getTeacher(int id);

}

package org.abin.lee.dao.impl;

import java.util.List;

import org.abin.lee.bean.Student;

import org.abin.lee.dao.StudentDao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao{

public List getStudent(int id) {

List list=null;

String hql="select o from Student o where o.id="+id;

try {

list=(List)this.getHibernateTemplate().find(hql);

} catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List getStudentById(int teacherId) {

List list=null;

String hql="select o from Student o where o.id="+teacherId;

try {

list=(List)this.getHibernateTemplate().find(hql);

} catch (Exception e) {

e.printStackTrace();

}

return list;

}

public void insertStudent(Student student) {

try {

this.getHibernateTemplate().save(student);

} catch (Exception e) {

e.printStackTrace();

}

}

}

package org.abin.lee.dao.impl;

import org.abin.lee.bean.Teacher;

import org.abin.lee.dao.TeacherDao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TeacherDaoImpl extends HibernateDaoSupport implements TeacherDao{

public Teacher getTeacher(int id) {

Teacher teacher=null;

String hql="select o from Teacher o where o.id="+id;

try {

teacher=(Teacher)this.getHibernateTemplate().find(hql);

this.getHibernateTemplate().flush();

} catch (Exception e) {

e.printStackTrace();

}

return teacher;

}

public void insertTeacher(Teacher teacher) {

try {

this.getHibernateTemplate().save(teacher);

this.getHibernateTemplate().flush();

} catch (Exception e) {

e.printStackTrace();

}

}

}

package org.abin.lee.service;

import java.util.List;

import org.abin.lee.bean.Student;

public interface StudentService {

void insertStudent(Student student);

List getStudent(int id);

List getStudentById(int teacherId);

}

package org.abin.lee.service;

import org.abin.lee.bean.Teacher;

public interface TeacherService {

void insertTeacher(Teacher teacher);

Teacher getTeacher(int id);

}

package org.abin.lee.service.impl;

import java.util.List;

import org.abin.lee.bean.Student;

import org.abin.lee.dao.StudentDao;

import org.abin.lee.service.StudentService;

public class StudentServiceImpl implements StudentService{

private StudentDao studentDao;

public List getStudent(int id) {

return this.studentDao.getStudent(id);

}

public List getStudentById(int teacherId) {

return this.studentDao.getStudentById(teacherId);

}

public void insertStudent(Student student) {

this.studentDao.insertStudent(student);

}

public StudentDao getStudentDao() {

return studentDao;

}

public void setStudentDao(StudentDao studentDao) {

this.studentDao = studentDao;

}

}

package org.abin.lee.service.impl;

import org.abin.lee.bean.Teacher;

import org.abin.lee.dao.TeacherDao;

import org.abin.lee.service.TeacherService;

public class TeacherServiceImpl implements TeacherService{

private TeacherDao teacherDao;

public Teacher getTeacher(int id) {

return this.teacherDao.getTeacher(id);

}

public void insertTeacher(Teacher teacher) {

this.teacherDao.insertTeacher(teacher);

}

public TeacherDao getTeacherDao() {

return teacherDao;

}

public void setTeacherDao(TeacherDao teacherDao) {

this.teacherDao = teacherDao;

}

}

applicationContext-mapper.xml(org.abin.lee.spring)

class="org.mybatis.spring.mapper.MapperFactoryBean">

value="org.abin.lee.dao.StudentDao" />

class="org.mybatis.spring.mapper.MapperFactoryBean">

value="org.abin.lee.dao.TeacherDao" />

applicationContext-resource.xml(org.abin.lee.spring)

class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

value="com.mysql.jdbc.Driver">

class="org.mybatis.spring.SqlSessionFactoryBean">

value="classpath:mybatis-config.xml" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

applicationContext-service.xml(org.abin.lee.spring)

class="org.abin.lee.service.impl.StudentServiceImpl">

class="org.abin.lee.service.impl.TeacherServiceImpl">

mybatis-config.xml(直接放在src目录下面)

Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

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 list=this.studentService.getStudentById(4);

Student stu=new Student();

Teacher tea=new Teacher();

for(int i=0;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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值