本章博客主要讲ssm框架中常用的SQL映射文件的写法,主要以查询为例,仅供参考,希望能帮助到各位道友
ResultType对应entity中的一个对象
ResultMap也是返回值类型,但是范围要大于ResultType,可以存键值对
/**
* 查询所有用户
*/
List<User> findUsers();
<select id="findUsers" resultType="User">
select * from smbms_user
</select>
有多个参数的时候
可以通过@Param进行参数传递
/**
* 根据用户名称和用户角色进行查询
* 多个参数的时候一定要加上@Param才能实现查询,不然会报错
* @return
*/
List<User> findByUserNameAndUserRole(@Param("userName") String userName,@Param("userRole") Integer userRole);
<select id="findByUserNameAndUserRole" resultType="User">
select * from smbms_user where userName like concat("%",#{userName},"%")
and userRole=#{userRole}
</select>
也可以封装成User对象进行参数传递
/**
* 根据用户名称和用户角色进行查询
* @return
*/
List<User> findByUserNameAndUserRole1(User user);
<select id="findByUserNameAndUserRole1" resultType="User" parameterType="User">
select * from smbms_user where userName like concat("%",#{username},"%")
and userRole=#{userrole}
</select>
甚至可以封装成Map进行参数传递
/**
* 根据用户名称和用户角色进行查询
* @return
*/
List<User> findByUserNameAndUserRole2(Map<String,Object> map);
<select id="findByUserNameAndUserRole2" resultType="User">
select * from smbms_user where userName like concat("%",#{username},"%")
and userRole=#{userrole}
</select>
/**
* 根据用户表和角色表进行联表查询
* @return
*/
List<User> findByUserAndRole();
<select id="findByUserAndRole" resultType="User">
select user.*,role.roleName as userRoleName from smbms_user user inner join smbms_role role on user.userRole = role.id
</select>
通过自定义ResultMap绑定数据
/**
* 根据用户表和角色表进行联表查询
* @return
*/
List<User> findByUserAndRole2();
<select id="findByUserAndRole2" resultMap="userMap">
select user.*,role.roleName from smbms_user user inner join smbms_role role
on user.userRole = role.id
</select>
<resultMap id="userMap" type="User">
<id column="id" property="id">id>
<result column="roleName" property="userRoleName">result>
resultMap>
/**
* 对用户和角色进行联表查询
* @return
*/
List<User> findAllUserAndRole();
<resultMap id="UserAndRole" type="User">
<id property="id" column="id">id>
<result property="username" column="userName">result>
<association property="role" javaType="Role">
<id property="id" column="roleId">id>
<result property="roleName" column="roleName">result>
association>
resultMap>
<select id="findAllUserAndRole" resultMap="UserAndRole">
select user.*,role.id as roleId,role.roleName from smbms_user user inner join smbms_role role on user.userRole = role.id
select>
/**
* 根据用户ID查询所有的地址信息
* @return
*/
List<User> findAddressById(Long id);
<resultMap id="UserAndAddressList" type="User">
<id property="id" column="id">id>
<result property="username" column="userName">result>
<collection property="addressList" ofType="com.changan.entity.Address">
<id property="id" column="addressId">id>
<result property="addressDesc" column="addressDesc">result>
collection>
resultMap>
<select id="findAddressById" resultMap="UserAndAddressList">
select user.*,address.id as addressId,address.addressDesc from smbms_user user inner join smbms_address address on
user.id = address.userId where user.id = #{id}
select>
欢迎各位大佬补充和指点