mybaists 一对多理解

xiaoxiao2021-02-28  130

数据库 drop table user; -- ---------------------------- -- Table structure for `user` -- ---------------------------- CREATE TABLE `user` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `username` varchar(64) NOT NULL DEFAULT '',   `mobile` int(10) unsigned NOT NULL DEFAULT '0',   `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'yiibai', '100', '2015-09-23 20:11:23'); -- ---------------------------- -- Table structure for `post` -- ---------------------------- CREATE TABLE `post` (   `post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `userid` int(10) unsigned NOT NULL,   `title` varchar(254) NOT NULL DEFAULT '',   `content` text,   `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',   PRIMARY KEY (`post_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of post

-- ----------------------------

///

package org.com.model; import java.io.Serializable; public class Post implements Serializable{     /**      *      */     private static final long serialVersionUID = 1L;     private int id;     private User user;     private String title;     private String content;     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public User getUser() {         return user;     }     public void setUser(User user) {         this.user = user;     }     public String getTitle() {         return title;     }     public void setTitle(String title) {         this.title = title;     }     public String getContent() {         return content;     }     public void setContent(String content) {         this.content = content;     } }

//

package org.com.model; import java.io.Serializable; import java.util.List; public class User implements Serializable{     /**      *      */     private static final long serialVersionUID = 1L;     private int id;     private String username;     private String mobile;     private List<Post> posts;     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getUsername() {         return username;     }     public void setUsername(String username) {         this.username = username;     }     public String getMobile() {         return mobile;     }     public void setMobile(String mobile) {         this.mobile = mobile;     }     public List<Post> getPosts() {         return posts;     }     public void setPosts(List<Post> posts) {         this.posts = posts;     }     @Override     public String toString() {         return "User [id=" + id + ", name=" + username + "]";     } }

<?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"> <!-- 一堆多映射 一个人可以发表多个数据- 通过collection  --> <mapper namespace="org.com.dao.userMapper"> <!-- property 就是类的属性  ,后面接的是在数据库的的名字 -->              <resultMap type="User" id="resultUserMap">             <result property="id" column="user_id" />  <!-- 主键 -->             <result property="username" column="username" />             <result property="mobile" column="mobile" />             <!-- user_id 作为 Post 表的userid -->             <collection property="posts" ofType="org.com.model.Post" column="userid">                     <id property="id" column="post_id" javaType="int" jdbcType="INTEGER"/>   <!-- 主键 -->                 <result property="title" column="title" javaType="string" jdbcType="VARCHAR"/>                 <result property="content" column="content" javaType="string" jdbcType="VARCHAR"/>             </collection>                    </resultMap>                  <select id="getUser" resultMap="resultUserMap">             select u.*,p.*                     from user u,post p                         where u.id=p.userid and id=#{user_id}           </select>      </mapper>

源文件

http://www.yiibai.com/mybatis/mybatis-one2many.html

转载请注明原文地址: https://www.6miu.com/read-50392.html

最新回复(0)