MyBatis的一对多和多对一

该博客展示了如何使用MyBatis进行数据库操作,包括配置SqlSessionFactory,创建实体类,定义DAO接口及其XML映射文件。通过MybatisUtil工具类获取SqlSession,实现多对一(Fxx与Clas)和一对多(Clas与Fxx)的关系映射查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

util包

package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    private static SqlSessionFactory sessionFactory;
    static {
        String resource="MyBatis.xml";
        try {
            InputStream is= Resources.getResourceAsStream(resource);
            sessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSession(){
        return sessionFactory.openSession();
    }
    public static void closeSession(SqlSession session){
        if (session!=null){
            session.close();
        }
    }
}

 MyBatis.xml    注意该xml文件需要放在src下面里面的实体类包路径dao包路径需要,以及数据库名称和密码需要改成你自己的

<?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>
        <package name="pojo"/>
    </typeAliases>
    <environments default="cs">
        <environment id="cs">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/fx01"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="dao/FxxDao.xml"></mapper>
    </mappers>
</configuration>

pojo包的两个实体类

package pojo;

public class Fxx {
    private int id;
    private String name;
    private String pwd;
    private int clasId;
    private Clas clas;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getClasId() {
        return clasId;
    }

    public void setClasId(int clasId) {
        this.clasId = clasId;
    }

    public Clas getClas() {
        return clas;
    }

    public void setClas(Clas clas) {
        this.clas = clas;
    }

    public Fxx(int id, String name, String pwd, int clasId, Clas clas) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.clasId = clasId;
        this.clas = clas;
    }

    @Override
    public String toString() {
        return "Fxx{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", clasId=" + clasId +
                ", clas=" + clas +
                '}';
    }

    public Fxx() {
    }

    public Fxx(int id, String name, String pwd, int clasId) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.clasId = clasId;
    }
}
package pojo;

import java.util.List;

public class Clas {
    private int clasId;
    private String clasName;
    private List<Fxx> fxxList;

    public int getClasId() {
        return clasId;
    }

    public void setClasId(int clasId) {
        this.clasId = clasId;
    }

    public String getClasName() {
        return clasName;
    }

    public void setClasName(String clasName) {
        this.clasName = clasName;
    }

    public List<Fxx> getFxxList() {
        return fxxList;
    }

    public void setFxxList(List<Fxx> fxxList) {
        this.fxxList = fxxList;
    }

    public Clas() {
    }

    @Override
    public String toString() {
        return "Clas{" +
                "clasId=" + clasId +
                ", clasName='" + clasName + '\'' +
                ", fxxList=" + fxxList +
                '}';
    }

    public Clas(String clasName, List<Fxx> fxxList) {
        this.clasName = clasName;
        this.fxxList = fxxList;
    }

    public Clas(int clasId, String clasName, List<Fxx> fxxList) {
        this.clasId = clasId;
        this.clasName = clasName;
        this.fxxList = fxxList;
    }

    public Clas(String clasName) {
        this.clasName = clasName;
    }

    public Clas(List<Fxx> fxxList) {
        this.fxxList = fxxList;
    }
}

dao包下的接口

package dao;

import pojo.Clas;
import pojo.Fxx;

import java.util.List;

public interface FxxDao {
    //多对一
    public List<Fxx> findAll();
    //一对多
    public Clas findByClasId(int clasId);
}

dao包下的xml

<?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="dao.FxxDao">
    <resultMap id="fxx" type="Fxx">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <result property="pwd" column="pwd"></result>
        <result property="clasId" column="clasId"></result>
        <association property="clas" javaType="Clas" resultMap="clases"></association>
    </resultMap>
    <resultMap id="clases" type="Clas">
        <result property="clasId" column="clasId"></result>
        <result property="clasName" column="clasName"></result>
    </resultMap>


    <resultMap id="claslist" type="Clas">
        <result property="clasId" column="clasId"></result>
        <result property="clasName" column="clasName"></result>
        <collection property="fxxList" ofType="Fxx">
            <result property="id" column="id"></result>
            <result property="name" column="name"></result>
            <result property="pwd" column="pwd"></result>
            <result property="clasId" column="clasId"></result>
        </collection>
    </resultMap>

    <select id="findAll" resultMap="fxx">
        select f.*,c.clasName from fxx f, clas c where  f.clasId=c.clasId
    </select>
    <select id="findByClasId" resultMap="claslist">
        select f.*,c.clasName from fxx f, clas c where  f.clasId=c.clasId and f.clasId=#{clasId}
    </select>
</mapper>

test包下的测试类

package test;

import dao.FxxDao;
import org.apache.ibatis.session.SqlSession;
import pojo.Clas;
import pojo.Fxx;
import util.MybatisUtil;

import java.util.List;

public class Test {
    public static void main(String[] args) {
        test1();
        test2();
    }

    private static void test2() {
        SqlSession session = MybatisUtil.getSession();
        Clas ci = session.getMapper(FxxDao.class).findByClasId(1);
        System.out.println(ci.getClasName());
        for(Fxx f:ci.getFxxList()){
            System.out.println(f.getName());
        }
    }

    private static void test1() {
        SqlSession session = MybatisUtil.getSession();
        List<Fxx> fxxList = session.getMapper(FxxDao.class).findAll();
        for (Fxx f:fxxList){
            System.out.println(f.getName()+f.getClas().getClasId());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值