欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用mybatis建立一对一的关系

程序员文章站 2022-04-22 08:00:54
...

使用Hibernate用惯了的朋友,不防试着使用mybatis,mybatis比Hibernate使用起来会更加舒畅,不信我们来看看

☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟☟
 1)一对一关系配置

以人和身份证为例

实体类MyUser.java和MyCard.java

MyUser.java

package com.zking.entity;

public class MyUser{
    private int uid;//用户编号
    private String uname;//用户名
    private MyCard myCard;//用户身份证


    public MyUser() {
    }

    public MyUser(int uid, String uname) {
        this.uid = uid;
        this.uname = uname;
    }

    public MyUser(int uid, String uname, MyCard myCard) {
        this.uid = uid;
        this.uname = uname;
        this.myCard = myCard;
    }

    public MyCard getMyCard() {
        return myCard;
    }

    public void setMyCard(MyCard myCard) {
        this.myCard = myCard;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    @Override
    public String toString() {
        return "MyUser{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                '}';
    }
}
MyCard.java
package com.zking.entity;

public class MyCard {
    private int uid;//身份证对应的uid
    private String cnum;//身份证号

    public MyCard() {
    }

    public MyCard(int uid, String cnum) {
        this.uid = uid;
        this.cnum = cnum;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getCnum() {
        return cnum;
    }

    public void setCnum(String cnum) {
        this.cnum = cnum;
    }

    @Override
    public String toString() {
        return "MyCard{" +
                "uid=" + uid +
                ", cnum='" + cnum + '\'' +
                '}';
    }
}
MyUser.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="com.zking.dao.MyUserDao">
    <!--resultMap必须写在上面,这里配置你的返回类型-->
   <resultMap id="myUserMap" type="com.zking.entity.MyUser">
       <id property="uid" column="uid"></id>
       <result property="uname" column="uname"></result>
       <!--配对的对象在这里设置用associatoin-->
       <association property="myCard" javaType="com.zking.entity.MyCard">
           <id property="uid" column="uid"></id>
           <result property="cnum" column="cnum"></result>
       </association>
   </resultMap>
    <!--查询的sql,这里的意思是以前的getsession的createQuery方法在这里写-->
    <!--返回的结果有关系配置于是返回的结果改为resultMap-->
    <select id="getUserAndCardById" parameterType="int" resultMap="myUserMap">
        SELECT u.*,c.cnum from myuser u,mycard c where u.uid=c.uid AND u.uid=#{uid}
    </select>
</mapper>

还有一步

在resources中要配置你的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>
        <package name="com.zking.entity"></package>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/zking/entity/MyUser.xml"/>
    </mappers>
</configuration>


测试

import com.zking.dao.*;
import com.zking.dto.MyUserDto;
import com.zking.entity.*;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

public class TestCRUD {

    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    @Before
    public void before(){
        //读取配置文件,获取SQLSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            //通过SQLSessionFactory获取SQLSession
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    @After
    public void after(){
        sqlSession.commit();
    }

public void test1(){
        MyUserDao myUserDao=sqlSession.getMapper(MyUserDao.class);
//        MyUserDto myUserDto=myUserDao.getUserAndCardById(15);
        MyUser myUser=myUserDao.getUserAndCardById(15);
        System.out.println(myUser+myUser.getMyCard().getCnum());
    }
}