mybatis的配置文件是configuation.xml是配置文件,主要是配置jdbc(用来创建sessionfactory)以及映射文件的别名,
一对多:
<mapper namespace="com.yiibai.userMaper"> <!-- User 级联文章查询 方法配置 (一个用户对多个文章) --> <resultMap type="User" id="resultUserMap"> <result property="id" column="user_id" /> <result property="username" column="username" /> <result property="mobile" column="mobile" /> <collection property="posts" ofType="com.yiibai.pojo.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" parameterType="int"> SELECT u.*,p.* FROM user u, post p WHERE u.id=p.userid AND id=#{user_id} </select> </mapper> #########################
<collection property="posts" ofType="com.yiibai.pojo.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> collection对多,column = userid 是对于 posts表来说的关联user的字段名称 ################################### 多对一
<resultMap type="Post" id="resultPostsMap"> <result property="id" column="post_id" /> <result property="title" column="title" /> <result property="content" column="content" /> <association property="user" javaType="User"> <id property="id" column="userid"/> <result property="username" column="username"/> <result property="mobile" column="mobile"/> </association> </resultMap> MyBatis使用RowBounds实现的分页是逻辑分页,也就是先把数据记录全部查询出来,然在再根据 offset 和 limit 截断记录返回。
时间: 2024-11-14 00:54:47