<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    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> student=new ArrayList<Student>();
     
     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<Student> getStudent() {
      return student;
     }

     public void setStudent(List<Student> student) {
      this.student = student;
     }

     
    }





    StudentMapper.xml(org.abin.lee.bean)
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "<mapper namespace="org.abin.lee.dao.StudentDao">
      <parameterMap type="Student" id="parameterStudentMap">
        <parameter property="sid"/>
        <parameter property="sname"/>
        <parameter property="teacherId"/>
      </parameterMap>
       
      <insert id="insertStudent"    parameterMap="parameterStudentMap">
        <selectKey    keyProperty="sid" resultType="int" order="AFTER">
          SELECT LAST_INSERT_ID() AS ID
        </selectKey>
        INSERT INTO student(sname,teacherId)
        VALUES(#{sname},#{teacherId})
      </insert>   
       
      <resultMap type="Student" id="StudentMap">
        <result property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <result property="teacherId" column="teacherId"/>
        <association property="teacher" javaType="Teacher" column="teacherId" select="org.abin.lee.dao.TeacherDao.getTeacher"/>
      </resultMap>   
      <select id="getStudent" resultMap="StudentMap" parameterType="int">
        SELECT * FROM student
        WHERE sid=#{sid}
      </select>
       
      <select id="getStudentById" resultMap="StudentMap" parameterType="int">
        SELECT * FROM student
        WHERE teacherId=#{teacherId}
      </select>
     
     
    </mapper>  



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

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "
    <mapper namespace="org.abin.lee.dao.TeacherDao">
      <parameterMap type="Teacher" id="parameterTeacherMap">
        <parameter property="tid"/>
        <parameter property="tname"/>
      </parameterMap>

      <insert id="insertTeacher" parameterMap="parameterTeacherMap">
        <selectKey    keyProperty="tid" resultType="int" order="AFTER">
          SELECT @@IDENTITY AS ID
        </selectKey>
        INSERT INTO teacher(tname)
        VALUES(#{tname})
      </insert>       
       
      <resultMap type="Teacher" id="resultTeacherMap">
        <result property="tid" column="tid"/>
        <result property="tname" column="tname"/>
        <collection property="student" column="sid" select="org.abin.lee.dao.StudentDao.getStudentById"/>
      </resultMap>
       
      <select id="getTeacher" resultMap="resultTeacherMap" parameterType="int">
        SELECT *
        FROM teacher
        WHERE tid=#{id}
      </select>         
      
    </mapper>



    package org.abin.lee.dao;

    import java.util.List;

    import org.abin.lee.bean.Student;

    public interface StudentDao {
     void insertStudent(Student student);
     List<Student> getStudent(int id);
     List<Student> 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<Student> getStudent(int id) {
      List<Student> list=null;
      String hql="select o from Student o where o.id="+id;
      try {
       list=(List<Student>)this.getHibernateTemplate().find(hql);
      } catch (Exception e) {
       e.printStackTrace();
      }
      return list;
     }

     public List<Student> getStudentById(int teacherId) {
      List<Student> list=null;
      String hql="select o from Student o where o.id="+teacherId;
      try {
       list=(List<Student>)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<Student> getStudent(int id);
     List<Student> 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<Student> getStudent(int id) {
      return this.studentDao.getStudent(id);
     }

     public List<Student> 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)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="
     xmlns:xsi=" xmlns:context=" xmlns:aop=" xmlns:tx=" xsi:schemaLocation="
               
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/context

       <!--創建數據映射器,數據映射器必須為接口-->
     <bean id="studentDao"
      class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
      <property name="mapperInterface"
       value="org.abin.lee.dao.StudentDao" />
     </bean>
     
     <bean id="teacherDao"
      class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
      <property name="mapperInterface"
       value="org.abin.lee.dao.TeacherDao" />
     </bean>


    </beans>






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

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="
     xmlns:xsi=" xmlns:context=" xmlns:aop=" xmlns:tx=" xsi:schemaLocation="
               
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/context


     <!--配置數據源 -->
     <bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
      <property name="driverClassName"
       value="com.mysql.jdbc.Driver">
      </property>
      <property name="url" value="jdbc:mysql://localhost:3306/abin"></property>
      <property name="username" value="root"></property>
      <property name="password" value=""></property>
      <property name="maxActive" value="100"></property>
      <property name="maxIdle" value="30"></property>
      <property name="maxWait" value="500"></property>
      <property name="defaultAutoCommit" value="true"></property>
     </bean>

        <!-- 創建SqlSessionFactory,同時指定數據源-->
     <bean id="sqlSessionFactory"
      class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configLocation"
       value="classpath:mybatis-config.xml" />
     </bean>

     <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
     </bean>


    </beans>







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

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="
     xmlns:xsi=" xmlns:context=" xmlns:aop=" xmlns:tx=" xsi:schemaLocation="
               
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                http://www.springframework.org/schema/context

     <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;
     }
     
    }



    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲免费日韩无码系列| 野花高清在线观看免费3中文| 亚洲天天做日日做天天欢毛片| 日韩一区二区a片免费观看 | 国产区在线免费观看| 亚洲中文字幕无码爆乳| 久久久久亚洲av无码专区喷水| 亚洲中文字幕无码一区| 四虎1515hm免费国产| 妞干网在线免费观看| a拍拍男女免费看全片| 桃子视频在线观看高清免费视频| 一二三区免费视频| 免费人成动漫在线播放r18| 极品色天使在线婷婷天堂亚洲 | 国内精品免费麻豆网站91麻豆| 成人无码a级毛片免费| 久久久久女教师免费一区| 人人爽人人爽人人片A免费| 美女一级毛片免费观看| 国产成人综合久久精品亚洲| 亚洲精品国产精品| 大桥未久亚洲无av码在线| 亚洲中文字幕无码mv| 亚洲中文无码mv| 亚洲sm另类一区二区三区| 亚洲成a人片在线不卡一二三区 | 久久99九九国产免费看小说| 精品免费人成视频app| 亚洲一区免费在线观看| 67194国产精品免费观看| 精品一区二区三区免费毛片爱| 日本免费一区二区三区| 4399影视免费观看高清直播| 91免费人成网站在线观看18| 最近2022中文字幕免费视频| 免费成人激情视频| 啦啦啦中文在线观看电视剧免费版 | 在线中文高清资源免费观看| 永久免费看bbb| 亚洲精品动漫人成3d在线|