package com.xin.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserLogin {
private int id;
private String username;
private String password;
}
UserCustomerMapper.xml
<?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.xin.mapper.UserCustomerMapper">
<resultMap id="BaseResultMap" type="com.xin.pojo.UserCustomer">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="cid" jdbcType="BIGINT" property="cid" />
<result column="uid" jdbcType="BIGINT" property="uid" />
</resultMap>
<resultMap type="com.xin.pojo.UserCustomer" id="ordersResultMap">
<id column="id" property="id" />
<result column="cid" property="cid" />
<result column="uid" property="uid" />
<!--表示进行关联查询单条记录 property:表示关联查询的结果存储在Orders类中的user属性中,javaType:表示关联查询的结果类型 -->
<association property="userLogin" javaType="com.xin.pojo.UserLogin">
<!--查询结果中的userid列对应关联的User对象的id属性 -->
<id column="id" property="id" />
<result column="user_name" property="username" />
<result column="u_password" property="password" />
</association>
</resultMap>
<!-- 关联查询 -->
<select id="findCustomerUser" resultMap="ordersResultMap" >
select * from UserLogin as ul LEFT JOIN UserCustomer as uc on ul.id=uc.id
</select>
<!--插入 UserCustomer-->
<insert id="insertUserCustomerList" parameterType="java.util.List">
INSERT INTO usercustomer
( cid, uid)
VALUES
<foreach collection ="usercList" item="user" separator =",">
(#{user.cid}, #{user.uid})
</foreach >
</insert>
<!--插入一条数据-->
<insert id="insertUser" keyProperty="id" useGeneratedKeys="true" parameterType="com.xin.pojo.UserCustomer">
insert into usercustomer(cid,uid)
values(#{cid},#{uid})
</insert>
</mapper>
package com.xin.mapper;
import com.xin.pojo.Correlation;
import com.xin.pojo.UserCustomer;
import com.xin.pojo.UserLogin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserCustomerMapper {
int insertUser(UserCustomer userCustomer );
int insertUserCustomerList(@Param("usercList") List<UserCustomer> usercList);
public List<UserCustomer> findCustomer();
//关联查询
public List<UserCustomer> findCustomerUser();
}
package com.xin.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserCustomer {
private int id;
private int cid;
private int uid;
//关联查询
private UserLogin userLogin;
}
package com.xin.mybatis;
import com.xin.mapper.CystomerMapper;
import com.xin.mapper.UserCustomerMapper;
import com.xin.mapper.UserLoginMapper;
import com.xin.pojo.UserCustomer;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.util.List;
/**
* @ClassName
* @Author hongxin
* @Date 2024/6/14 10:07
* @Version 1.0
**/
@MapperScan("com.xin.mapper")
@SpringBootTest
public class CorrelationTest {
@Autowired
DataSource dataSource;
@Autowired
UserLoginMapper userLoginMapper;
@Autowired
UserCustomerMapper userCustomerMapper;
@Autowired
CystomerMapper cystomerMapper;
// 关联查询 ok
@Test
public void findCustomerUser() {
List<UserCustomer> corrList = userCustomerMapper.findCustomerUser();
for (int i = 0; i < corrList.size(); i++) {
UserCustomer userCustomer = corrList.get(i);
System.out.println("=====userCustomer======="+userCustomer);
}
}
}
usercustomer.sql
标签:xin,关联,private,org,mybatis,import,查询,com,public From: https://blog.csdn.net/m0_70355065/article/details/139747732