mapper.xml中
<select id="getUserInfo2" parameterType="int" resultMap="UserInfo2">
select u.*,i.* from user_info i join user u on i.user_id=u.user_id
where i.user_id=#{userId}
</select>
<resultMap type="UserInfo" id="UserInfo2">
<id column="user_info_id" property="userInfoId"/>
<result column="age" property="age" />
<result column="sax" property="sax" />
<association property="user" javaType="User" >
<id column="user_id" property="userId"/>
<result column="email" property="email" />
<result column="password" property="password" />
<result column="nickname" property="nickname" />
</association>
</resultMap>
mapper中:
@MyBatisRepository
public interface UserMapper {
public List<User> query();
public UserInfo getUserInfo(int userId);
public UserInfo getUserInfo2(int userId);
public User getUser(int id);
}
在mybatis配置文件中增加下面的配置
<settings>
<!-- 数据库的字段为user_id 映射到类中字段为 userId-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开始自动映射,默认是不映射嵌套的map,这样写也会映射嵌套的map-->
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
在mapper.xml中可以这样写
<resultMap type="UserInfo" id="UserInfo2">
<association property="user" javaType="User" />
</resultMap>
简单的映射
<select id="query" resultType="User">
select * from user
</select>
<select id="getUser" parameterType="int" resultType="User">
select * from user where user_id=#{id}
</select>
<select id="getUserInfo" parameterType="int" resultMap="userInfo">
select * from user_info where user_id=#{userId}
</select>
<resultMap type="UserInfo" id="userInfo">
<association property="user" javaType="User" column="user_id" select="getUser" />
</resultMap>
别名的配置,自动加包名
<typeAliases>
<package name="entity"/>
</typeAliases>