首页 > 其他分享 >spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis多表连接——association一对一

spring boot——spring boot的基本配置——spring boot整合mybatis——本地实例运行——MyBatis多表连接——association一对一

时间:2023-01-07 19:12:42浏览次数:53  
标签:多表 int spring boot id example import org public

本地数据库登录:

 

C:\Users\lenovo>cd C:\Program Files\mysql8\mysql-8.0.29-winx64\mysql-8.0.29-winx64\bin

C:\Program Files\mysql8\mysql-8.0.29-winx64\mysql-8.0.29-winx64\bin>mysql -u root -p

Enter password: ******

 

 

 

 

 

 

创建2个表,并插入数据:

CREATE TABLE `user2` (
  `id` int ,
  `name` varchar(255) ,
  `age` int 
) ;


insert into user2( id,name,age ) values ( 41,"wang",25);


CREATE TABLE `account` (
  `id` int ,
  `uid` int ,
  `money` int 
) ;

insert into account( id,uid,money) values(1,41,100);


select u.id,name,age,money from user2 u,account a where u.id = a.uid;

 

 

 

 

 

 

user2:

package org.example.entity;

public class User2
{
    private int id;
    private String name;
    private int age;

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    private Account account;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}

 

 

 

 

 

 

 

account:

package org.example.entity;

public class Account
{
    private int id;
    private int uid;
    private int money;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}

 

 

 

 

 

 

 

 

 

user2mapper文件:

<?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="org.example.mapper.User2Mapper">

    <resultMap id="yiDuYi" type="org.example.entity.User2">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <association property="account" column="id" javaType="org.example.entity.Account">
            <id property="id" column="id"></id>
            <result property="uid" column="uid"></result>
            <result property="money" column="money"></result>
        </association>
    </resultMap>

    <!--查询所有数据-->
    <select id="findAll" resultMap="yiDuYi">
        select u.id,name,age,money from user2 u,account a where u.id = a.uid
    </select>



</mapper>

 

 

 

 

 

 

 

 

 

user2mapper接口:

package org.example.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.example.entity.User2;
import java.util.List;

@Mapper
public interface User2Mapper
{
    List<User2> findAll();

}

 

 

 

 

 

service接口:

package org.example.service;

import org.example.entity.User2;
import java.util.List;

public interface User2Service
{
    List<User2> findAll();


}

 

 

 

 

 

实现service接口:

package org.example.service.Impl;

import org.example.entity.User2;
import org.example.mapper.User2Mapper;
import org.example.service.User2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User2ServiceImpl implements User2Service
{

    @Autowired
    User2Mapper otherUserMapper;


    public List<User2> findAll()
    {

        return otherUserMapper.findAll();

    }


}

 

 

 

 

 

 

 

 

 

控制器:

package org.example.controller;

import org.example.entity.User2;
import org.example.service.User2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class User2Controller
{
    @Autowired
    private User2Service otherService;

    @GetMapping(value = "/31")
    public List<User2> home31()
    {
        // 查询所有用户
        List<User2> users = otherService.findAll();

        for (User2 user : users)
        {
            System.out.println(user.getId()+"  "+user.getName()+"  "+user.getAge());
            System.out.println(user.getAccount().getMoney());
        }

        return users;
    }




}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

执行:

 

 

 

 

 

 

 

标签:多表,int,spring,boot,id,example,import,org,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17033288.html

相关文章