One-to-one query of mybatis advanced mapping

  • The most annoying thing about learning mybatis is sql language, which is really a great pain for me. They all say that mybatis is easy to learn. I think it's all the words that Daniel can say. It seems that I have to nibble away the knowledge related to database. Well, I'm in the process of mybatis today...
  • It takes time to figure out the relationship between tables and tables in the database. Unfortunately, I am still trying to figure it out, only a small part of it can be studied from a small point of view. Here are only four tables (dizzy enough to change for a while). They are User, orders, order details, items.
CREATE TABLE orders(
    id INT(11) KEY AUTO_INCREMENT,
    user_id INT(11) NOT NULL,
    number VARCHAR(32),
    createtime DATETIME,
    note VARCHAR(100)
);

CREATE TABLE orderdetail(
    id INT(11) KEY AUTO_INCREMENT,
    orders_id INT(11) NOT NULL,
    items_id INT(11),
    items_num INT(11)
);
CREATE TABLE items(
    id INT(11) KEY AUTO_INCREMENT,
    NAME VARCHAR(32) NOT NULL,
    price FLOAT(10,1),
    detail TEXT,
    pic VARCHAR(512),
    createtime DATETIME
);

The contents of the table are not perfect. If you know how to perfect the God, please hold out your hand, hope to point out your younger brother. Thank you very much. I will never forget to point out when I practice Shengong.

  • For one-to-one queries, the first thing you have to do is write sql statements
Select 
orders.*,
user.username,
user.sex,
user.address from orders,
user where orders.user_id=user.id

The Association connection here is an internal link, because there is only one data queried by the foreign key, so the internal link can be used for association here.

  • Now that I have finished writing the sql statement, I have to analyze the configuration of the xml file.
<?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.itsky.day02.OrdersMapperCustom">
    <select id="findOrdersUser"  resultType="com.itsky.day02.OrdersCustom">
            Select 
            orders.*,
            user.username,
            user.sex,
            user.address from orders,
            user where orders.user_id=user.id

    </select>
    </mapper>

id is the unique identifier, parameterType is the parameter type of the input, because there is no parameter input involved, so parametercan not; resultType is the return value type, this is very exquisite, because ordinary pojo is not able to meet the requirements, so here we have to redefine a pojo class, the parameters and The query data wants to match, so this pojo can inherit a database corresponding class with more parameters and add those data with fewer parameters manually. Here's an OrdersCustom class.

public class OrdersCustom extends Orders{
    private String  username;
    private String sex;
    private String address;
    public String getUsername() {
        return username;
    }

In this way, all the data should be included. By the way, there's another namespace that doesn't mention that the parameters here pass in the interface address.

  • Write out the interface class now
public interface OrdersMapperCustom {
    public List<OrdersCustom>findOrderUser();

}

It provides a query method.

  • Finally, let's test it. (Feels like I'm going to doubt my life. Every time I get to the test, there will always be some inexplicable mistakes.)
@Before
    public void setUp() throws Exception {
        // 1. Read configuration files
        String resource = "applicationContext.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 2. Create SqlSessionFactory based on configuration file
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testForFindOrdersUser() {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom .class);
        List<OrdersCustom>list=ordersMapperCustom .findOrderUser();
        System.out.println(list);


    }
  • Result
    log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    []
    No prompt error, no data in each table, mybatis master please give more advice, I am still a little rookie... It would be better if we could give some good information.

Keywords: Mybatis log4j SQL Database

Added by xinnex on Sun, 07 Jul 2019 04:53:49 +0300