Spring boot MyBatis pair of one by one-to-many

Source: Internet
Author: User
Tags aliases

Pox configuration

        <!--MyBatis -        <Dependency>            <groupId>Org.mybatis.spring.boot</groupId>            <Artifactid>Mybatis-spring-boot-starter</Artifactid>            <version>1.1.1</version>        </Dependency>        <!--MySQL -        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>        </Dependency>        <!--Jack-json -        <Dependency>            <groupId>Org.codehaus.jackson</groupId>            <Artifactid>Jackson-mapper-asl</Artifactid>            <version>1.9.13</version>        </Dependency>

Directory structure

Database structure

Basic Configuration

Spring:   DataSource:    url:jdbc:mysql://localhost:3306/bet    Driver-class-name:com.mysql.jdbc.driver    username:root    password:rootmybatis:  mapper-locations:classpath:mapper/*.xml  Type-aliases-package:com.bo

Mapper-locations:mybatis configuration file Scan path

Type-aliases-package: Database Mapping Relationship object scan path (not set if required in <resultmap type= "Com.bo.User" id= "Bookresult" > Type Write full path)

Bo

public class User {    private Integer ID;    Private String name;    Private String sex;    Private list<book> Books;            Get set ...     .. }    
public class book {    private String ID;    Private String name;        Get set ...   ...}

Usermapper.xml

A user has more than one book

<?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 "><Mappernamespace= "Com.dao.UserDao">    <Resultmaptype= "Com.bo.User"ID= "Userresult">        <ID Property= "id"column= "ID" />        <result Property= "Name"column= "U_name" />    </Resultmap>    <Resultmaptype= "Com.bo.User"ID= "USERRESULT1">        <ID Property= "id"column= "ID" />        <result Property= "Name"column= "U_name" />        <Collection Property= "Books"OfType= "Com.bo.Book">            <ID Property= "id"column= "b_id" />            <result Property= "Name"column= "NAME" />        </Collection>    </Resultmap>    <SelectID= "Getuserall"Resultmap= "Userresult">SELECT * from user</Select>    <SelectID= "Getuserbyid"Resultmap= "USERRESULT1">SELECT a.*,b.id as b_id,b.name from User A, books B WHERE a.id=b.u_id and a.id = #{id}</Select></Mapper>

Focus, if the user table and Book table, the primary key name, such as the example is the ID, then 1 to many cases, you will find that the query book object will have only one data, so SQL use alias to handle, the result is normal!

The third line to the bottom of the code above

DAO interface

 Package Com.dao; Import java.util.List; Import Com.bo.User;  Public Interface Userdao {    public list<user> Getuserall ();           Public User Getuserbyid (Integer ID);}

Test tests

 Packagecom.test;Importjava.util.List;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner;ImportCom.bo.Book;ImportCom.bo.User;ImportCom.dao.UserDao; the @RunWith (Springrunner.class) @SpringBootTest Public classTest1 {@AutowiredPrivateUserdao Userdao; @Test Public voidTest () {List<User> list =Userdao.getuserall ();  for(User u:list) {System.out.println (U.getid ()+":"+u.getname ()); } System.out.println ("-----------------"); User u= Userdao.getuserbyid (1); System.out.println (U.getid ()+":"+u.getname ()); List<Book> books =U.getbooks ();        System.out.println (Books.size ());  for(book book:books) {System.out.println (Book.getname ()); }    }}

Results

Spring boot MyBatis pair of one by one-to-many

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.