关于java:Mybatispuls配置-多表查询

29次阅读

共计 2313 个字符,预计需要花费 6 分钟才能阅读完成。

mybatis-puls

mybatis-plus 依赖
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
application.yml 配置
server:
  port: 8003
spring:
  datasource:
    url: jdbc:mysql:/IP 地址 // 数据库?serverTimezone=GMT%2B8&CharacterEncoding=utf8
    username: 账号
    password: 明码
mybatis-plus:
# 扫描 xml 文件
  mapper-locations: classpath:/mapper/*.xml
  configuration:
# 控制台打印执行 SQL 语句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类扫描 mapper
@SpringBootApplication
// 扫描
@MapperScan("com.jsd.mapper")
public class Springcloud03Application {public static void main(String[] args) {SpringApplication.run(Springcloud03Application.class, args);
    }

}

pojo

User

@Data
// 数据库表名
@TableName("user")
public class User implements Serializable {
    private Integer Id;
    private String Name;
    private Integer Age;
    private String Email;
// 对象
    private Message message;
// 嵌套汇合
    private List<Message> messageList;

}

Message

@Data
@TableName("message")
public class Message implements Serializable {
    private Integer Id;
    private String Message;
}
UserMapper 接口
// 继承 BaseMapper<>
public interface UserMapper extends BaseMapper<User>{List<User> selectAll();

    List<User> selectLists();}
UserMapper.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.jsd.mapper.UserMapper">
    <resultMap id="User" type="com.jsd.pojo.User">
        <id column="id" property="Id"></id>
        <result column="name" property="Name"></result>
        <result column="age" property="Age"></result>
        <result column="email" property="Email"></result>
        <association property="message" javaType="com.jsd.pojo.Message">
            <id column="id" property="Id"></id>
            <result column="message" property="Message"></result>
        </association>
    </resultMap>
    <resultMap id="UserList" type="com.jsd.pojo.User">
        <id column="id" property="Id"></id>
        <result column="name" property="Name"></result>
        <result column="age" property="Age"></result>
        <result column="email" property="Email"></result>
        <collection property="messageList" ofType="com.jsd.pojo.Message">
            <id column="id" property="Id"></id>
            <result column="message" property="Message"></result>
        </collection>
    </resultMap>
    <select id="selectAll" resultMap="User">
        select
        a.id,a.name,a.age,a.email,b.message
        from user a join message b
        on a.id=b.id
    </select>
    <select id="selectLists" resultMap="UserList">
        select
        a.id,a.name,a.age,a.email,b.message
        from user a join message b
        on a.id=b.id
    </select>
</mapper>

正文完
 0