• Mybatis配置一对多的关联关系(五)


      问题:是查询一个部门中的员工?

    一、web项目构架

          

    二、lib文件的jar

        

    三、配置大小配置和该工具类

      1大配置mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
         
         <!-- 别名的定制 -->
         <typeAliases>
            <!-- 按类型名定制别名  -->
            <!--   <typeAlias type="cn.happy.entity.Student" alias="Student"/> -->
            
            <!-- 拿当前指定包下的简单类名作为别名  -->
            <package name="cn.happy.entity"/>
         </typeAliases>
     
    
        <environments default="development">
        <environment id="development">
        
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
            <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
            <property name="username" value="T2"/>
            <property name="password" value="T2"/>
          </dataSource>
        </environment>
      </environments>
        <mappers>
            <mapper resource="cn/happy/dao/DeptDAO.xml" />
        </mappers>
    </configuration>

    2小配置

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="cn.happy.dao.IDeptDAO">
      
      <resultMap type="Dept" id="deptMapper">
         <id property="deptno" column="deptno"/>
         <result property="deptname" column="deptname"/>
         <collection property="emps" ofType="Emp">
            <id property="empno" column="empno"/>
            <result property="empname" column="empname"/>
         </collection>
      </resultMap>
      
    
     <!-- one2many -->
      
      <select id="getDeptById" resultMap="deptMapper">
         select dept.deptno,deptname,empno,empname from dept,emp
         where dept.deptno=emp.deptno and dept.deptno=#{deptno}
      </select>
      
      
    </mapper>

    3工具类

    package cn.happy.util;
    
    import java.io.IOException;
    import java.io.Reader;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    /**
     * 工具类
     * @author Happy
     *
     */
    public class MybatisUtil {
        private static String config="mybatis-config.xml";
        static Reader reader;
        static{
            try {
                reader= Resources.getResourceAsReader(config);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        //提供一个可以获取到session的方法
        public static SqlSession getSession() throws IOException{
            
            System.out.println("22222"+factory);
            //弊病,就是工厂是
               // 1.1 openSession到底做了什么
               SqlSession session = factory.openSession();
               System.out.println("3333");
                return session;
        }
    }

    四、配置实体类

    1Emp

    package cn.happy.entity;
    /**
     * 员工
     * @author Administrator
     *
     */
    public class Emp {
        private Integer empno;
        private String empname;
        private Integer empage;
        public Integer getEmpno() {
            return empno;
        }
        public void setEmpno(Integer empno) {
            this.empno = empno;
        }
        public String getEmpname() {
            return empname;
        }
        public void setEmpname(String empname) {
            this.empname = empname;
        }
        public Integer getEmpage() {
            return empage;
        }
        public void setEmpage(Integer empage) {
            this.empage = empage;
        }
        
    }

    2Dept

    package cn.happy.entity;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * 部门
     * @author Administrator
     *
     */
    public class Dept {
        private Integer deptno;
        private String deptname;
        //配置一对多的关系
        private Set<Emp> emps=new HashSet<Emp>();
        
        public Set<Emp> getEmps() {
            return emps;
        }
        public void setEmps(Set<Emp> emps) {
            this.emps = emps;
        }
        public Integer getDeptno() {
            return deptno;
        }
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
        public String getDeptname() {
            return deptname;
        }
        public void setDeptname(String deptname) {
            this.deptname = deptname;
        }
        
        
    }

    五接口

    package cn.happy.dao;
    
    import cn.happy.entity.Dept;
    
    public interface IDeptDAO {
    
        //根据部门编号查询该部门
            public Dept getDeptById(Integer id);
    }

    六测试

    package cn.happy.test;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Before;
    import org.junit.Test;
    
    import cn.happy.dao.IDeptDAO;
    import cn.happy.entity.Dept;
    
    import cn.happy.util.MybatisUtil;
    
    public class MyTest {
        IDeptDAO dao;
        @Before
        public void initData() throws IOException{
            SqlSession session = MybatisUtil.getSession();
             dao = session.getMapper(IDeptDAO.class);
        }
    
    
        @Test
        public void getDeptById() throws IOException{
            Dept dept = dao.getDeptById(2);
            System.out.println(dept);
        }
    
    }
  • 相关阅读:
    BZOJ 1046: [HAOI2007]上升序列(LIS)
    BZOJ 1001: [BeiJing2006]狼抓兔子(最短路)
    UVa 12299 RMQ with Shifts(线段树)
    NOIP2010提高组] CODEVS 1069 关押罪犯(并查集)
    [NOIP2001提高组]CODEVS1014 Car的旅行路线(最短路)
    scp 远程文件拷贝命令
    mysql yum源安装极速
    ssh pubkey免密登陆远程主机
    paramiko linux pip18.1
    python3 linux
  • 原文地址:https://www.cnblogs.com/yejiaojiao/p/6198023.html
Copyright © 2020-2023  润新知