Mybatis series (14) – Notes – many to one

Time:2020-10-31

Step 1: run first, see the effect, and then learn
Step 2: imitation and troubleshooting
Step 3: many to one based on XML
Step 4: many to one effect
Step 5: categorymapper
Step 6: productmapper
Step 7: mybatis- config.xml
Step 8: test the class

Step 1: run first, see the effect, and then learn

As the old rule, first download the executable items in the download area (click to enter), configure and run them, and then learn what steps to do to achieve this effect.

Step 2: imitation and troubleshooting

After making sure that the runnable project runs correctly, follow the steps of the tutorial and imitate the code again.
It is inevitable that there is code discrepancy in the simulation process, which leads to the failure to get the expected running resultsright key(can run the project) and your own code to locate the problem.
In this way,Learning is effective and troubleshooting is efficientIt can obviously improve the learning speed and cross the threshold of learning.

It is recommended to use diffmerge software for folder comparison. Compare your own project folder with my runnable project folder.
This software is very powerful, you can know which two files in the folder are wrong, and mark them out clearly
Here is a green installation and use tutorial: diffmerge download and use tutorial

One to three: XML based approach

This knowledge point is modified on the basis of XML many to one, so as to realize one to many annotation

Step 4: many to one effect

Mybatis series (14) - Notes - many to one

Step 5: category mapper

The categorymapper interface is added to provide the get method

package com.how2java.mapper;

import org.apache.ibatis.annotations.Select;

import com.how2java.pojo.Category;

public interface CategoryMapper {
    @Select(" select * from category_ where id = #{id}")
    public Category get(int id);

}

Step 6: productmapper

Add the productmapper interface and provide the list method

package com.how2java.mapper;

import java.util.List;

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 com.how2java.pojo.Product;

public interface ProductMapper {
    @Select(" select * from product_ ")
    @Results({
        @Result(property="category",column="cid",[email protected](select="com.how2java.mapper.CategoryMapper.get"))
    })
    public List<Product> list();
}

Step 7: mybatis- config.xml

Add the mapping of category mapper and product mapper in lines 22 and 23

<?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.how2java.pojo"/>
    </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/how2java?characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/how2java/pojo/Category.xml"/>
        <mapper class="com.how2java.mapper.CategoryMapper"/>
        <mapper class="com.how2java.mapper.ProductMapper"/>
    </mappers>
</configuration>

Step 8: test the class

Run the test class and see the effect as shown in the figure

Mybatis series (14) - Notes - many to one

package com.how2java;

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

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 com.how2java.mapper.ProductMapper;
import com.how2java.pojo.Product;

public class TestMybatis {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        List<Product> ps= mapper.list();
        for (Product p : ps) {
            System.out.println (the corresponding classification of P + "\ t is: T" + p.getcategory(). Getname());
        }
        session.commit();
        session.close();
    }
}

For more information, click to learn: https://how2j.cn/k/mybatis/mybatis-annotation-many-to-one/1095.html