• 3.关联查询和属性文件


    1.创建javaweb项目MyBatis_Part3并在项目的WebRoot下的WEB-INF下的lib中加入如下jar文件

    mybatis-3.2.3.jar

    ojdbc14.jar

    2.在src下创建configuarion.xml主配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
     3 <configuration>
     4 
     5   <!-- 1.引用jdbc链接数据库的属性文件 -->
     6   <properties resource="jdbc.properties"/>
     7   
     8   <!-- 2.类路径的别名 -->
     9   <typeAliases>
    10     <typeAlias type="com.entity.Classes" alias="Classes"/>
    11     <typeAlias type="com.entity.Teacher" alias="Teacher"/>
    12     <typeAlias type="com.entity.Student" alias="Student"/>
    13   </typeAliases>
    14   
    15   <!-- 3.事务和数据源的配置 -->
    16   <environments default="development">
    17     <environment id="development">
    18       <transactionManager type="jdbc"/>
    19       <dataSource type="POOLED">
    20         <property name="driver" value="${driver}"/>
    21         <property name="url" value="${url}"/>
    22         <property name="username" value="${username}"/>
    23         <property name="password" value="${password}"/>
    24       </dataSource>
    25     </environment>
    26   </environments>
    27   <!-- 4.注册映射文件 -->
    28   <mappers>
    29   <!-- 有映射文件时resource属性=映射文件的路径 -->
    30   <!-- 使用注解没有映射文件时class属性=映射文件的路径 -->
    31   <mapper url="com."/>
    32    
    33   </mappers>
    34   
    35 </configuration>
    configuration.xml

    3.在src下创建jdbc.properties属性文件

    1 driver=oracle.jdbc.driver.OracleDriver
    2 url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    3 username=holly
    4 password=sys
    jdbc.properties

    4.在src下创建mybatis_Part3.txt说明文件

     1 1.mybaits主配置文件
     2 (1)环境根节点s
     3 (2)环境根节点
     4 (3)jdbc管理事务
     5 (4)dataSource数据源
     61》driver,url,username,password
     7 (5)注册映射文件
     8 
     9 2.mybaits的映射文件
    10 (1)查询
    11 <select id="和映射接口方法名" parameterType="" resultType="">
    12 
    13 (2)插入
    14 <insert id="" paramenterType="">
    15 
    16 (3)修改
    17 <update id="" paramenterType="">
    18 
    19 
    20 (3)删除
    21 <delete id="" paramenterType="">
    22 
    23 
    24 3.步骤
    25 (1)在myeclipce 里倒入mybatis的dtd文件
    26 (2)创建项目
    27 (3)在项目的src下创建主配置文件
    28 (4)在com.mapper包下创建映射文件
    29 (5)编辑主配置文件
    30 (6)编辑映射接口
    31 (7)编辑映射文件
    32 (8)编辑接口实现类
    33 无代理
    34 selectList 查所有
    35 selectOne  查单条
    36 insert
    37 update
    38 delete
    39 
    40 有代理
    41 利用反射获取接口对象,然后调用接口方法
    42 
    43 
    44 4.一对多,一对一,链接数据库的属性文件,resultMap的配置
    45 --老师表
    46 CREATE TABLE teacher(
    47      tid number PRIMARY KEY, 
    48      tname VARCHAR2(20)
    49  );
    50 --班级表
    51  CREATE TABLE classes(
    52 cid number PRIMARY KEY,
    53 cname VARCHAR2(20),
    54 teacherid number,
    55 constraint fk_class_teacherid foreign key(teacherid)
    56 references teacher(tid));
    57 
    58 --学生表
    59 CREATE TABLE student(
    60 sid number PRIMARY KEY,
    61 sname VARCHAR2(20),
    62 classid number,
    63 constraint fk_student_classid foreign key(classid) 
    64 references classes(cid));
    65 
    66 --创建序列
    67  create sequence seq_teacher;
    68 
    69  create sequence seq_classes;
    70 
    71  create sequence seq_student;
    72 
    73 --添加数据
    74 INSERT INTO teacher VALUES(seq_teacher.nextval, 'holly');
    75 INSERT INTO teacher VALUES(seq_teacher.nextval, '石头');
    76 
    77 INSERT INTO class VALUES(seq_classes.nextval,'TB13', 1);
    78 INSERT INTO class VALUES(seq_classes.nextval,'TB24', 2);
    79 
    80 INSERT INTO student VALUES(seq_student.nextval,'陈阳',1);
    81 INSERT INTO student VALUES(seq_student.nextval,'王佳俊',1);
    82 INSERT INTO student VALUES(seq_student.nextval,'黄逸舟',1);
    83 INSERT INTO student VALUES(seq_student.nextval,'张浩',2);
    84 INSERT INTO student VALUES(seq_student.nextval,'张冉',2);
    85 INSERT INTO student VALUES(seq_student.nextval,'孙秋云',2);
    86 
    87 --修改表名
    88 alter table class rename to classes;
    Mybatis_Part3.txt

    5.在src下com.entity包下创建Student.java文件

     1 package com.entity;
     2 
     3 public class Student {
     4     private int sid;
     5     private String sname;
     6     
     7     public Student() {
     8     }
     9     
    10     public Student(int sid, String sname) {
    11         this.sid = sid;
    12         this.sname = sname;
    13     }
    14     public int getSid() {
    15         return sid;
    16     }
    17     public void setSid(int sid) {
    18         this.sid = sid;
    19     }
    20     public String getSname() {
    21         return sname;
    22     }
    23     public void setSname(String sname) {
    24         this.sname = sname;
    25     }
    26 
    27     @Override
    28     public String toString() {
    29         return "Student [sid=" + sid + ", sname=" + sname + "]";
    30     }
    31     
    32     
    33     
    34 
    35 }
    Student.java

    6.在src下com.entity包下创建Teacher.java文件

     1 package com.entity;
     2 
     3 public class Teacher {
     4     private int tid;
     5     private String tname;
     6     
     7     public Teacher() {
     8     }
     9     public Teacher(int tid, String tname) {
    10         this.tid = tid;
    11         this.tname = tname;
    12     }
    13     public int getTid() {
    14         return tid;
    15     }
    16     public void setTid(int tid) {
    17         this.tid = tid;
    18     }
    19     public String getTname() {
    20         return tname;
    21     }
    22     public void setTname(String tname) {
    23         this.tname = tname;
    24     }
    25     @Override
    26     public String toString() {
    27         return "Teacher [tid=" + tid + ", tname=" + tname + "]";
    28     }
    29     
    30     
    31 
    32 }
    Teacher.java

    7.在src下com.entity包下创建Classes.java文件

     1 package com.entity;
     2 
     3 import java.util.List;
     4 
     5 public class Classes {
     6     private int cid;
     7     private String cname;
     8     private Teacher teacher; //一对一
     9     private List<Student> students; //一对多
    10     
    11     public Classes() {
    12     }
    13     public Classes(int cid, String cname, Teacher teacher) {
    14         this.cid = cid;
    15         this.cname = cname;
    16         this.teacher = teacher;
    17     }
    18     
    19     public Classes(int cid, String cname, Teacher teacher,
    20             List<Student> students) {
    21         super();
    22         this.cid = cid;
    23         this.cname = cname;
    24         this.teacher = teacher;
    25         this.students = students;
    26     }
    27     public List<Student> getStudents() {
    28         return students;
    29     }
    30     public void setStudents(List<Student> students) {
    31         this.students = students;
    32     }
    33     public int getCid() {
    34         return cid;
    35     }
    36     public void setCid(int cid) {
    37         this.cid = cid;
    38     }
    39     public String getCname() {
    40         return cname;
    41     }
    42     public void setCname(String cname) {
    43         this.cname = cname;
    44     }
    45     public Teacher getTeacher() {
    46         return teacher;
    47     }
    48     public void setTeacher(Teacher teacher) {
    49         this.teacher = teacher;
    50     }
    51     @Override
    52     public String toString() {
    53         return "Classes [cid=" + cid + ", cname=" + cname + ", students="
    54                 + students + ", teacher=" + teacher + "]";
    55     }
    56     
    57 
    58 }
    Classes.java

     8.在src下com.util包下创建MyBaitsUtil.java文件

     1 package com.util;
     2 
     3 import java.io.IOException;
     4 import java.io.Reader;
     5 
     6 import org.apache.ibatis.io.Resources;
     7 import org.apache.ibatis.session.SqlSession;
     8 import org.apache.ibatis.session.SqlSessionFactory;
     9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    10 
    11 public class MyBaitsUtil {
    12     private static SqlSessionFactory sqlSessionFactory;
    13     
    14     static{
    15         try {
    16             Reader reader=Resources.getResourceAsReader("configuration.xml");
    17             sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); 
    18         } catch (IOException e) {
    19             e.printStackTrace();
    20         }
    21         
    22     }
    23     /**
    24      * 
    25      * @param isCommit true表示执行完操作提交事务,false表示执行完不自动提交事务
    26      * @return
    27      */
    28     public static SqlSession getSqlSession(boolean isCommit){
    29         return sqlSessionFactory.openSession(isCommit);
    30     }
    31 
    32 }
    MyBaitsUtil.java

    9.在src下com.mapper包下创建StudentMapper.java文件

     1 package com.mapper;
     2 
     3 import java.util.List;
     4 
     5 import com.entity.Student;
     6 
     7 public interface StudentMapper {
     8     List<Student> findAll();
     9 
    10 }
    StudentMapper.java

    10.在src下com.mapper包下创建StudentMapper.xml文件

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3 <mapper namespace="com.mapper.StudentMapper">
    4   <select id="findAll" resultType="Student">
    5      select * from student
    6   </select>
    7 </mapper>
    StudentMapper.xml

    11.在src下com.mapper包下创建TeacherMapper.java文件

     1 package com.mapper;
     2 
     3 import java.util.List;
     4 
     5 import com.entity.Teacher;
     6 
     7 public interface TeacherMapper {
     8     List<Teacher> findAll();
     9 
    10 }
    TeacherMapper.java

    12.在src下com.mapper包下创建TeacherMapper.xml文件

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3 <mapper namespace="com.mapper.TeacherMapper">
    4   <select id="findAll" resultType="Teacher">
    5     select * from teacher
    6   </select>
    7 </mapper>
    TeacherMapper.xml

    13.在src下com.mapper包下创建ClassesMapper.java文件

     1 package com.mapper;
     2 
     3 import java.util.List;
     4 
     5 import com.entity.Classes;
     6 
     7 public interface ClassesMapper {
     8      List<Classes> findAll();
     9      
    10 }
    ClassesMapper.java

    14.在src下com.mapper包下创建ClassesMapper.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.mapper.ClassesMapper">
     4   <!-- 1.三表联查的sql,返回的map -->
     5   <select id="findAll" resultMap="ClassResultMap">
     6    select * from student s,teacher t,classes c 
     7      where c.teacherid=t.tid and s.classid=c.cid
     8   </select>
     9   
    10   <!-- 2.配置三表联查返回的map -->
    11   <!-- id的值等于select节点的resultMap的值,type表示集合类型 -->
    12   <resultMap id="ClassResultMap" type="Classes" >
    13      <!-- classes表主键配置 ,实体类里的属性字段和数据库表字段映射-->
    14      <id property="cid" column="cid"/>
    15      
    16      <!-- classes普通字段配置 -->
    17      <result property="cname" column="cname"/>
    18      
    19      <!-- 一对一:对象的配置 -->
    20      <association property="teacher" column="teacherid" javaType="Teacher">
    21        <!-- teacher表主键id配置 -->
    22        <id property="tid" column="tid"/>
    23        
    24        <!-- teacher表普通字段的配置 -->
    25        <result property="tname" column="tname"/>
    26      </association>
    27      
    28      <!-- 一对多:list集合的配置,ofType集合中的对象类型 -->
    29      <collection property="students" ofType="Student">
    30        <!-- student表主键id配置 -->
    31        <id property="sid" column="sid"/>
    32        
    33        <!-- student表普通字段的配置 -->
    34        <result property="sname" column="sname"/>
    35      </collection>
    36   </resultMap>
    37 </mapper>
    ClassesMapper.xml

    15.在src下com.mapper.impl包下创建ClassesMapperImpl.java文件

     1 package com.mapper.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.session.SqlSession;
     6 
     7 import com.entity.Classes;
     8 import com.mapper.ClassesMapper;
     9 import com.util.MyBaitsUtil;
    10 
    11 public class ClassesMapperImpl implements ClassesMapper {
    12     /**
    13      * 查询所有
    14      */
    15     public List<Classes> findAll() {
    16         //获取sqlSession
    17         SqlSession sqlSession=MyBaitsUtil.getSqlSession(false);
    18         //使用有代理,动态创建ClassesMapper对象
    19         ClassesMapper mapper=sqlSession.getMapper(ClassesMapper.class);
    20         //调用映射接口方法==代理对象调用底层的selectList实现
    21         List<Classes> list=mapper.findAll();
    22         //关闭session
    23         sqlSession.close();
    24         //返回结果
    25         return list;
    26     }
    27 
    28 }
    ClassesMapperImpl.java

    16.在src下com.test包下创建Test.java文件

    package com.test;
    
    import java.util.List;
    
    import com.entity.Classes;
    import com.mapper.ClassesMapper;
    import com.mapper.impl.ClassesMapperImpl;
    
    public class Test {
        public static void main(String[] args) {
            ClassesMapper mapper=new ClassesMapperImpl();
            List<Classes> list=mapper.findAll();
            if(list!=null){
                for (Classes classes : list) {
                    System.out.println(classes);
                }
            }
        }
    
    }
    Test.java
  • 相关阅读:
    html5_css 3 学习指南__转
    MySQL常见故障处理手册_转
    MYSQL出错代码列表——转
    Redhat 6环境下安装Oracle 12c的方法
    Wireshark入门:分析DHCP协议的运行
    重命名Oracle数据库的表空间(Renaming a Tablespace)
    Oracle DB 分区特性概述 Overview of Partitions
    Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)
    Supporting Connected Routes to Subnet Zero
    Secondary IP Addressing
  • 原文地址:https://www.cnblogs.com/holly8/p/5794105.html
Copyright © 2020-2023  润新知