mybatis连查表的不同方式1【记录——1】

mybatis 多级表查询

首先了解
1. MyBatis怎么的传入参数:通过属性parameterType=“参数类型”
1. 1. 基本数据类型:int、string、long、Date; 1. 2. 复杂数据类型:类(JavaBean、Integer等)和Map
2. 如何获取参数中的值:
2.1 基本数据类型:#{参数} 获取参数中的值
![参数名](https://img-blog.csdnimg.cn/20201222191834917.png)![接口参数\](https://img-blog.csdnimg.cn/20201222191900169.png)](https://img-blog.csdnimg.cn/20201222191924220.png)

2.2 复杂数据类型:#{属性名} ,map中则是#{key}

3.查询【接口实现】。多对一
每个部门有着多个员工。是不是懵逼(如果你看了下面的话)。

<?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="com.dao.EmpDao"> <!--命名空间 namespace-->
<!--    插入标签 来查询员工和员-->
    <select id="selectManyToOne" resultMap="empDept" >
           select e.*,d.* from emp e inner join dept d on e.deptno=#{i}
    </select>
<!--    自定义类型的定义-->
    <resultMap id="empDept" type="com.vo1.Emp">
<!--        id:指的是主键 property实体类的属性 column表中的列名 373714850-->
        <id property="empno" column="empno"></id>
        <result property="ename" column="ename"></result>
        <result property="job" column="job"></result>
        <result property="sal" column="sal"></result>
<!--        定义多对一 映射文件-->
        <association property="dept" column="detpno" javaType="com.vo1.Dept">
            <id property="deptno" column="deptno"></id>
            <result property="dname" column="dname"></result>
            <result property="loc" column="loc"></result>
        </association>
    </resultMap>
</mapper>

4.定义一对多
一个部门下有着多个员工

<?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="com.dao.DeptDao"> <!--命名空间 namespace-->
<!--    插入标签-->
<!--    部门下面的内容-->
  <select id="selectManyToMany" resultMap="deptDept">
        select d.*,e.*
        from dept d left join emp e
        on d.deptno=e.deptno
--         where e.sal =0
  </select>
<!--    自定义类型定义-->
<resultMap id="deptDept" type="com.vo1.Dept" >
    <id property="deptno" column="deptno"></id>
    <result property="dname" column="dname"></result>
    <result property="loc" column="loc"></result>
<!--    定义一对多的关系-->
    <collection property="emps" ofType="com.vo1.Emp">
        <id property="empno" column="empno"></id>
        <result property="ename" column="ename"></result>
        <result property="job" column="job"></result>
        <result property="sal" column="sal"></result>
    </collection>
</resultMap>
</mapper>






通过注解来执行多表查询

[想要知道来这边](https://www.jianshu.com/p/c82143e2494e)
























暂未明白!!!!!!!!!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
<!--    包扫描-->
<!--    <context:component-scan base-package="com"></context:component-scan>-->
<!--    2数据源 相当于config.xml那个数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/text"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
<!--    3连接工厂 来访问数据源和映射文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
    </bean>
<!--    4扫描dao 找到mapper映射文件的关联接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.dao"></property>
    </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值