Mybatis Plus a complete version of the actual combat teaching!

Recently, I used Mybatis Plus in my project. I am really not familiar with it. Add, delete, change and check are very convenient, but the project needs to use a one-to-many query, I have no idea, so I looked up the relevant information on the Internet. How to use Mybatis to do one-to-many.

Get into the business

Looking up relevant information, Mybatis can’t write annotations directly like JPA, so today we are simplifying the code to the maximum extent

First of all, let’s paste out the entity class which is a managed entity for a location

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@TableName(resultMap = "locationResultMap")
public class Location extends BaseEntity {

    private static final long serialVersionUID = 3986378000838034438L;
    /** * Parent node ID **/
    private String parentId;

    /** ** name **/
    private String name;

    /** ** id **/
    private String creatUserId;

    /** ** name of the founder **/
    private String createUserName;

    /** * Device type, 0 - consumption terminal, 1 - card issuing terminal **/
    private Integer type;

    /** * dependency ID, consumption terminal is McH_no, card issuing terminal is mech_code **/
    private String dependId;

    /** * note **/
    private String remark;

    /** ** full address name **/
    private String fullName;
    
    private List<Location> locations;

}

Copy the code

Transformation began

First of all, when we wrote Mybatis before, add to the receiving class, the entity class

 private List<LocationVO> locations;
Copy the code

And then you declare your resultMap in your mapper.xml file and then you configure your resultMap and you configure it directly from the whole code

<? The 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.zyzh.upp.mapper.LocationMapper"> <resultMap id="locationResultMap" type="com.zyzh.upp.bean.dto.LocationDTO"> <id column="id" property="id"></id> <result column="parent_id" property="parentId"></result> <result column="name" property="name"></result> <result column="creat_user_id" property="creatUserId"></result> <result column="create_user_name" property="createUserName"></result> <result column="type" property="type"></result> <result column="depend_id" property="dependId"></result> <result column="remark"  property="remark"></result> <result column="full_name" property="fullName"></result> <result column="remark" property="remark"></result> <collection property="locations" column="id" select="findByParentId"></collection> </resultMap> <select id="findByParentId" resultType="com.zyzh.upp.bean.dto.LocationDTO"> SELECT * FROM location location  WHERE location.parent_id=#{parentId} </select> </mapper>Copy the code

After you configure the Mapper, don’t forget to declare the location of the resultMap, i.e. annotate the entity

@TableName(value="location ",resultMap = "locationResultMap")
Copy the code

Note: If it is not the name of the class, add value, resultMap is the same as mapper. XML declaration ID

Now that the configuration is roughly written, it needs to be called

Take a look at my locationMapper.java interface

@Repository
public interface LocationMapper extends BaseMapper<Location> {}Copy the code

Note: be sure to implement baseMapper so that MyBatis Plus can be used and then write the implementation interface to implement our one-to-many interface

public interface ILocationService {
    /** * Query terminal location information based on dependId */
    List<Location> findLocationByDependId(String dependId);
parentId);
}

Copy the code

Look at the implementation class:

@Service
public class LocationServiceImpl implements ILocationService {

    @Autowired
    LocationMapper locationMapper;
    @Override
    public List<Location> findLocationByDependId(String dependId) {
        return locationMapper.selectList(newLambdaQueryWrapper<Location>() .eq(Location::getDependId,dependId) ); }}Copy the code

Then we write a test class to test it

@Slf4j
public class locationServiceImplTest extends WebNoneTestConf {

    @Autowired
    private LocationServiceImpl locationService;

    @Test
    public void Test(a){
        List<LocationDTO> locationByDependId = locationService.findLocationByDependId("98519001212");
        log.error("{}",locationByDependId); }}Copy the code

Here’s the result. I tested it with the interface

Success!

If you think it’s good, I hope you can give me a thumbs up. Thank you very much!

Your praise is my biggest encouragement!

The author’s words

Mutual respect, mutual progress, thank you very much for everyone’s selfless spirit. Can let us Chinese IT more and more progress! I am also willing to listen to the opinions and suggestions of more leaders with an open mind. I am your good friend Fan Yifan

A programmer who gets a little better every day!