码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis单向一对一

时间:2018-02-20 21:18:47      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:uil   编写   assert   eal   tran   eve   default   null   配置信息   

一、假定一个父母只有一个孩子,那父母和孩子对应关系为1对1。

二、

        1、添加依赖包,参考上篇文章 Mybatis+mysql入门使用

        2、初始化数据库和初始数据,以mysql为例

技术分享图片
DROP DATABASE IF EXISTS moy_mybatis;
CREATE DATABASE moy_mybatis CHARACTER SET UTF8;
USE moy_mybatis;

DROP TABLE  IF EXISTS t_children;
CREATE TABLE t_children(
  children_id INT(11),
  children_name VARCHAR(50) DEFAULT NULL,
  children_birthday DATE DEFAULT NULL,
  parent_id INT(11) NOT NULL,
  PRIMARY KEY (`children_id`)
);

DROP TABLE  IF EXISTS t_parent;
CREATE TABLE t_parent(
  id INT(11)  ,
  name VARCHAR(50) DEFAULT NULL,
  birthday DATE DEFAULT NULL,
--   children_id INT(11) NOT NULL,
  PRIMARY KEY (id)
);

INSERT INTO t_children(children_id,children_name,children_birthday,parent_id)
VALUES (1,‘一号熊孩子‘,now(),1);
INSERT INTO t_children(children_id,children_name,children_birthday,parent_id)
VALUES (2,‘二号熊孩子‘,now(),2);

INSERT INTO t_parent(id,name,birthday)
VALUES (1,‘一号父母‘,now());
INSERT INTO t_parent(id,name,birthday)
VALUES (2,‘二号父母‘,now());
View Code

        3、新建实体类Children和Parent

技术分享图片
package com.moy.mybatis3.entity;

import java.util.Date;

/**
 * [Project]:moy-gradle-project  <br/>
 * [Email]:moy25@foxmail.com  <br/>
 * [Date]:2018/2/19  <br/>
 * [Description]:  <br/>
 *
 * @author YeXiangYang
 */
public class Children {

    private int childrenId;
    private String childrenName;
    private Parent parent;
    private Date childrenBirthday;

    public int getChildrenId() {
        return childrenId;
    }

    public void setChildrenId(int childrenId) {
        this.childrenId = childrenId;
    }

    public String getChildrenName() {
        return childrenName;
    }

    public void setChildrenName(String childrenName) {
        this.childrenName = childrenName;
    }

    public Date getChildrenBirthday() {
        return childrenBirthday;
    }

    public void setChildrenBirthday(Date childrenBirthday) {
        this.childrenBirthday = childrenBirthday;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Children{" +
                "childrenId=" + childrenId +
                ", childrenName=‘" + childrenName + ‘\‘‘ +
                ", parent=" + parent +
                ", childrenBirthday=" + childrenBirthday +
                ‘}‘;
    }
}
View Code
技术分享图片
package com.moy.mybatis3.entity;

import java.util.Date;
import java.util.List;

/**
 * [Project]:moy-gradle-project  <br/>
 * [Email]:moy25@foxmail.com  <br/>
 * [Date]:2018/2/19  <br/>
 * [Description]:  <br/>
 *
 * @author YeXiangYang
 */
public class Parent {

    private int id;
    private String name;
    private Date birthday;
    
    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Parent{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", birthday=" + birthday +
                ‘}‘;
    }
}
View Code

        4、新建实体对应配置文件Children.xml和Parent.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.moy.mybatis3.mapper.ChildrenMapper">
    <!--单向1对1 配置方法一 开始-->
    <!--<resultMap id="ChildrenResultMap" type="Children">-->
        <!--<id property="childrenId" column="children_id"/>-->
        <!--<result property="childrenName" column="children_name"/>-->
        <!--<result property="childrenBirthday" column="children_birthday"/>-->
        <!--<association property="parent" column="parent_id"-->
                     <!--resultMap="com.moy.mybatis3.mapper.ParentMapper.ParentResultMap"/>-->
    <!--</resultMap>-->
    <!--单向1对1 配置方法一 结束-->

    <!--单向1对1 配置方法二 开始-->
    <resultMap id="ChildrenResultMap" type="Children">
        <id property="childrenId" column="children_id"/>
        <result property="childrenName" column="children_name"/>
        <result property="childrenBirthday" column="children_birthday"/>
        <association property="parent" column="parent_id" javaType="Parent">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="birthday" column="birthday"/>
        </association>
    </resultMap>
    <!--单向1对1 配置方法二 结束-->
    <select id="queryChildrenAndParent" parameterType="int" resultMap="ChildrenResultMap">
        SELECT p.*,c.* FROM t_Parent p , t_Children c WHERE p.id = c.parent_id and c.children_Id = #{id}
    </select>
</mapper>
View Code
技术分享图片
<?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.moy.mybatis3.mapper.ParentMapper">
    <!--单向1对1 配置方法一 开始-->
    <resultMap id="ParentResultMap" type="Parent">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="birthday" column="birthday"/>
    </resultMap>
    <!--单向1对1 配置方法一 结束-->
</mapper>
View Code

        5、编写接口ChildrenMapper

技术分享图片
package com.moy.mybatis3.mapper;

import com.moy.mybatis3.entity.Children;

/**
 * [Project]:moy-gradle-project  <br/>
 * [Email]:moy25@foxmail.com  <br/>
 * [Date]:2018/2/19  <br/>
 * [Description]:  <br/>
 *
 * @author YeXiangYang
 */
public interface ChildrenMapper extends BaseMapper<Children> {

    Children queryChildrenAndParent(int id);
}
View Code

        6、为了方便测试,编写一个获取SqlSession的工具类Mybatis3Utils

技术分享图片
package com.moy.mybatis3.utils;

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.Reader;
import java.util.Objects;

/**
 * [Project]:moy-gradle-project  <br/>
 * [Email]:moy25@foxmail.com  <br/>
 * [Date]:2018/2/19  <br/>
 * [Description]:  <br/>
 *
 * @author YeXiangYang
 */
public abstract class Mybatis3Utils {

    public static final SqlSessionFactory sqlSessionFactory;
    public static final ThreadLocal<SqlSession> sessionThread = new ThreadLocal<>();

    static {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getCurrentSqlSession() {
        SqlSession sqlSession = sessionThread.get();
        if (Objects.isNull(sqlSession)) {
            sqlSession = sqlSessionFactory.openSession();
            sessionThread.set(sqlSession);
        }
        return sqlSession;
    }

    public static void closeCurrentSession() {
        SqlSession sqlSession = sessionThread.get();
        if (Objects.nonNull(sqlSession)) {
            sqlSession.close();
        }
        sessionThread.set(null);
    }
}
View Code

        7、新建mybatis配置信息文件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.moy.mybatis3.entity"/>
    </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://127.0.0.1:3306/moy_mybatis?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/Parent.xml"></mapper>
        <mapper resource="mapper/Children.xml"></mapper>
    </mappers>
</configuration>
View Code

        8、编写测试类ChildrenMapperTest

技术分享图片
package com.moy.mybatis3.mapper;

import com.moy.mybatis3.entity.Children;
import com.moy.mybatis3.utils.Mybatis3Utils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * [Project]:moy-gradle-project  <br/>
 * [Email]:moy25@foxmail.com  <br/>
 * [Date]:2018/2/20  <br/>
 * [Description]:  <br/>
 *
 * @author YeXiangYang
 */
public class ChildrenMapperTest {

    SqlSession sqlSession;
    ChildrenMapper childrenMapper;

    @Before
    public void before() {
        sqlSession = Mybatis3Utils.getCurrentSqlSession();
        childrenMapper = sqlSession.getMapper(ChildrenMapper.class);
    }

    @After
    public void after() {
        Mybatis3Utils.closeCurrentSession();
    }
    @Test
    public void queryChildrenAndParent() {
        Children children = childrenMapper.queryChildrenAndParent(1);
        System.out.println(children);
    }
}
View Code

 

yexiangyang

moyyexy@gmail.com


 

Mybatis单向一对一

标签:uil   编写   assert   eal   tran   eve   default   null   配置信息   

原文地址:https://www.cnblogs.com/moy25/p/8455800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!