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

MyBatis:注解简化一对一关系

程序员文章站 2022-04-22 08:01:53
...

上期

注解式是真的好用!

一对一的关系:客户拥有一张身份证,而身份证也只有一个客户。

 

/*
Navicat MySQL Data Transfer

Source Server         : hc
Source Server Version : 50723
Source Host           : localhost:3306
Source Database       : cn

Target Server Type    : MYSQL
Target Server Version : 50723
File Encoding         : 65001

Date: 2018-11-07 15:32:01
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `person`
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `perId` int(11) NOT NULL AUTO_INCREMENT,
  `perName` varchar(255) DEFAULT NULL,
  `cardId` int(11) NOT NULL,
  PRIMARY KEY (`perId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES ('1', 'haha', '1');
INSERT INTO `person` VALUES ('2', '维尔瓦', '2');
INSERT INTO `person` VALUES ('3', '阿三', '3');
INSERT INTO `person` VALUES ('4', '32阿瑟东', '4');
INSERT INTO `person` VALUES ('5', '安抚', '5');

MYsql中的表,copy后导入数据库即可。

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.43</version>
        </dependency>

pom.xml 

package com.hc.model;

public class Person {
    private int perId;
    private String perName;
    private Card card;

    public Person() {
    }

    public Person(String perName) {
        this.perName = perName;
    }

    public int getPerId() {
        return perId;
    }

    public void setPerId(int perId) {
        this.perId = perId;
    }

    public String getPerName() {
        return perName;
    }

    public void setPerName(String perName) {
        this.perName = perName;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

}

Person类

 

package com.hc.model;

public class Card {
    private int cardId;
    private String cardName;
    private Person person;

    public Card() {
    }

    public Card(String cardName) {
        this.cardName = cardName;
    }

    public int getCardId() {
        return cardId;
    }

    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

卡类

package com.hc.mapper;

import com.hc.model.Person;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface PersonMapper {
    @Select("select * from Person")
    @Results({
            @Result(property = "card",column = "cardId",one = @One(select = "com.hc.mapper.CardMapper.selectCardByCid"))
    })
    public List<Person> allPerson();

    @Select("select * from Person where cardId=#{cardId}")
    public Person selectPersonByCid(int cardId);

}

person的MApper

 

package com.hc.mapper;

import com.hc.model.Card;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CardMapper {
    @Select("select * from Card where cardId=#{cardId}")
    public Card selectCardByCid(int cardId);

    @Select("select * from Card")
    @Results({
            @Result(id = true,property = "cardId",column = "cardId"),
//            关联 id 必须
            @Result(property = "person",column = "cardId",one = @One(select = "com.hc.mapper.PersonMapper.selectPersonByCid"))
    })
    public List<Card> allCard();
}

卡的Mapper

 

<?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.hc.model"/>
    </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/cn?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper class="com.hc.mapper.UserMapper"/>
        <mapper class="com.hc.mapper.PersonMapper"/>
        <mapper class="com.hc.mapper.CardMapper"/>
        <mapper class="com.hc.mapper.AuthorMapper"/>
        <mapper class="com.hc.mapper.BookMapper"/>
        <mapper class="com.hc.mapper.CourseMapper"/>
        <mapper class="com.hc.mapper.StudentMapper"/>
    </mappers>

</configuration>

mybatis-config.xml

package com.hc.test;

import com.hc.mapper.CardMapper;
import com.hc.mapper.PersonMapper;
import com.hc.model.Card;
import com.hc.model.Person;
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;

public class TestPerson {

    private SqlSession session;


    @Before
    public void before() {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("myBatis-config.xml"));
        session = factory.openSession();


    }

    @Test
    public void testCard() {
        CardMapper cardMapper = session.getMapper(CardMapper.class);
        for (Card card : cardMapper.allCard()) {
            System.out.println(card.getCardId() + "\t《" + card.getCardName() + "》");
            Person person = card.getPerson();
            System.out.println(person.getPerId() + "\t" + person.getPerName());
            System.out.println("");
        }
    }

    @Test
    public void testPerson() {
        PersonMapper personMapper;
        personMapper = session.getMapper(PersonMapper.class);
        for (Person person : personMapper.allPerson()) {
            System.out.println(person.getPerId() + "\t" + person.getPerName());
            Card card = person.getCard();
            System.out.println(card.getCardId() + "\t《" + card.getCardName() + "》");
            System.out.println("");
        }
    }

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

}

单元测试

 

 

相关标签: java