使用mybaits一对多表查询

使用mybaits进行表的一对多表查询:

1.先在数据库里面写两张表,User(用户表),Post(数据表)

        post表里面要实现Serializable(序列化)

user表的实体类代码:

public class User {
    private int id;
    private String name;
    private int age;
    private String sex;
    private List<Post> posts;
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public List<Post> getPosts() {
        return posts;
    }
    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }

Post实体类的代码:

public class Post implements Serializable {
    public int getPost_id() {
        return post_id;
    }
    public void setPost_id(int post_id) {
        this.post_id = post_id;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public String getTent() {
        return tent;
    }
    public void setTent(String tent) {
        this.tent = tent;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    private int post_id;
    private User user;
    private String tent;
    private String time;
}

2.在编写代码的工具里面,编写用户表和数据表相对应的实体类,编写user表的映射文件。

     特别注意:<resultMap id="给ID名" type=“实体类”>

  <result column="数据库字段名" property=“实体类的属性” jdbcType="数据库字段类型">

</resultMap>

<mapper namespace="com.zhiyuan.model">
    <resultMap type="Post" id="resultPostsMap">
    <result property="post_id" column="post_id"/>
    <result property="tent" column="tent"/>
    <result property="time" column="time"/>
    <association property="user" javaType="User">
    <id  property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
    </association>
    </resultMap>

      <select id="getPosts" resultMap="resultPostsMap" parameterType="int">
      SELECT u.*,p.*
    FROM user u, post p
    WHERE u.id=p.userid AND p.post_id=#{post_id} ;
      </select>
</mapper>


3.编写config配置文件。

 特别注意:这章的内容要配置三个文件;一个是主配置文件config,其他两个就是实体类的映射文件user.xml。

 <typeAliases> 这个标签内容,它就是用于定义一个 JavaBean 类的别名

<configuration>
    <typeAliases>
        <typeAlias alias="user" type="com.zhiyuan.model.User"/>
        <typeAlias alias="post" type="com.zhiyuan.model.Post"/>
    </typeAliases>
    
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/work" />
                <property name="username" value="root" />
                <property name="password" value="panmin" />
    </dataSource>
    </environment>
    </environments>
    <mappers>
    <mapper resource="com/zhiyuan/model/User.xml"/>
    </mappers>
</configuration>

4.编写测试类。

public class Mapping {
    private static Reader reader;
    private static SqlSessionFactory sqlSessionFactory;
    
    static{
        try {
            reader=Resources.getResourceAsReader("config.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    public static void main(String[] args) {
        SqlSession session=sqlSessionFactory.openSession();
        int post_id=1;
        Post post=session.selectOne("com.zhiyuan.model.getPosts",post_id);
        System.out.println("tent:"+post.getTent());
        System.out.println("time:"+post.getTime());
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mybatis是一种流行的Java持久层框架,可以对数据库进行操作。在mybatis中,实现一对多关联查询可以使用两种方式:嵌套查询和嵌套结果映射。嵌套查询是指在主查询中执行子查询来获取关联对象的数据,而嵌套结果映射是指在主查询的结果映射中包含关联对象的映射。 对于嵌套查询,可以使用select标签在主查询中执行子查询。例如,假设有一个班级和学生的关联关系,可以使用如下的SQL语句进行一对多关联查询: ``` <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <select id="getBanjiWithStudents" resultType="Banji"> SELECT * FROM Banji WHERE id = #{id} </select> <select id="getStudentsByBanjiId" resultType="Student"> SELECT * FROM Student WHERE banji_id = #{id} </select> </mapper> ``` 然后可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询。 对于嵌套结果映射,可以使用association和collection标签来进行配置。例如: ``` <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> ``` 然后,在Mapper接口中定义对应的方法: ``` public interface BanjiMapper { Banji getBanjiWithStudents(int id); } ``` 在配置文件中进行映射: ``` <mapper namespace="com.example.BanjiMapper"> <resultMap id="banjiResultMap" type="Banji"> <id property="id" column="id"/> <result property="name" column="name"/> <collection property="students" ofType="Student"> <id property="id" column="student_id"/> <result property="name" column="student_name"/> </collection> </resultMap> <select id="getBanjiWithStudents" resultMap="banjiResultMap"> SELECT b.id, b.name, s.id as student_id, s.name as student_name FROM Banji b LEFT JOIN Student s ON b.id = s.banji_id WHERE b.id = #{id} </select> </mapper> ``` 同样可以通过调用`getBanjiWithStudents`方法来进行一对多关联查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值