SSM 框架学习 (MyBatis篇)
二、MyBatis SQL 映射文件的配置
条件模糊查询表信息
1、单个参数
// UserMapper.java
public interface UserMapper {
public List getUserListByUserName(String userName);
}
// UserMapper.xml
<select id="getUserListByUserName" resultType="User"
parameterType="String">
select * from smbms_user where userName like CONCAT
('%',#{userName},'%')
select>
2、(传入参数:对象)
// UserMapper.java
public interface UserMapper {
public List getUserListByUser(User user);
}
// UserMapper.xml
<select id="getUserListByUser" resultType="User" parameterType="User">
select * from smbms_user where userName like CONCAT
('%',#{userName},'%') and userRole = #{userRole}
select>
3、(传入参数:Map)
// UserMapper.java
public interface UserMapper {
public List getUserListByMap(Map userMap);
}
// UserMapper.xml
<select id="getUserListByMap" resultType="User" parameterType="Map">
select * from smbms_user where userName like CONCAT
('%',#{uName},'%') and userRole = #{uRole}
select>
4、resultMap使用
// UserMapper.xml
<resultMap type="User" id="userList">
<result property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<result property="birthday" column="birthday" />
<result property="gender" column="gender" />
<result property="userRole" column="userRole" />
<result property="userRoleName" column="roleName" />
resultMap>
<select id="getUserListByUser" resultMap="userList" parameterType="User">
select u.*,r.roleName from smbms_user u,smbms_role r
where u.userName like CONCAT ('%',#{userName},'%')
and u.userRole = #{userRole} and u.userRole = r.id
select>
5、向数据库添加信息
// UserMapper.java
public interface UserMapper {
public int addUser(User user);
}
// UserMapper.xml
insert into smbms_user
(userCode,userName,userPassword,gender,birthday,phone,
address,userRole,createdBy,creationDate)
values (#{userCode}, #{userName}, #{userPassword},#{gender},#{birthday},
#{phone},#{address},#{userRole},#{createdBy},#{creationDate})
6、更新数据库信息1
// UserMapper.java
public interface UserMapper {
public int modify(User user);
}
// UserMapper.xml
update smbms_user set
userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},
gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},
userRole=#{userRole},modifyBy=#{modifyBy},modifyDate=#{modifyDate}
where id = #{id}
7、更新数据库信息2
// UserMapper.java
public interface UserMapper {
public int updatePwd(@Param("id")Integer id, @Param("userPassword")String pwd);
}
// UserMapper.xml
"updatePwd">
update smbms_user set userPassword=#{userPassword} where id = #{id}
8、删除数据
// UserMapper.java
public interface UserMapper {
public int deleteUserById(@Param("id")Integer delId);
}
// UserMapper.xml
"deleteUserById">
delete from smbms_user where id = #{id}
9、resultMap > association 使用
// UserMapper.java
public interface UserMapper {
public List getUserListByRoleId(@Param("userRole")Integer roleId);
}
// User.java
public class User {
// association
private Role role; // 用户角色
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
// association
}
// Role.java
public class Role {
private Integer id; // id
private String roleCode; // 角色编码
private String roleName; // 角色名称
...
//get{} set{}
}
// UserMapper.xml
"User" id="userRoleList">
"id" column="id" />
"userCode" column="userCode" />
"userName" column="userName" />
"userRole" column="userRole" />
"role" javaType="Role">
"id" column="r_id" />
"roleCode" column="roleCode" />
"roleName" column="roleName" />
<select id="getUserListByRoleId" parameterType="Integer"
resultMap="userRoleList">
select u.*,r.id as r_id,r.roleCode, r.roleName
from smbms_user u,smbms_role r
where u.userRole=#{userRole}
and u.userRole = r.id
select>
10、resultMap > association使用2 (在第9步的基础上修改)
// UserMapper.java
public interface UserMapper {
public List<User> getUserListByRoleId2(@Param("userRole")Integer roleId);
}
// UserMapper.xml
<resultMap type="User" id="userRoleList2">
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
<association property="role" javaType="Role" resultMap="roleResult" />
resultMap>
<resultMap type="Role" id="roleResult">
<id property="id" column="r_id" />
<result property="roleCode" column="roleCode" />
<result property="roleName" column="roleName" />
resultMap>
<select id="getUserListByRoleId2" parameterType="Integer" resultMap="userRoleList2">
select u.*,r.id as r_id,r.roleCode, r.roleName
from smbms_user u,smbms_role r
where u.userRole=#{userRole}
and u.userRole = r.id
select>
11、resultMap > collection使用1
// UserMapper.java
public interface UserMapper {
public List<User> getAddressListByUserId(@Param("id")Integer userId);
}
// User.java
public class User {
// collection
private List<Address> addressList;
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
// collection
}
// Role.java
public class Address {
...
//get{} set{}
}
// UserMapper.xml
<resultMap type="User" id="userAddressList">
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<collection property="addressList" ofType="Address">
<id property="id" column="a_id" />
<result property="postCode" column="postCode" />
<result property="tel" column="tel" />
<result property="contact" column="contact" />
<result property="addressDesc" column="addressDesc" />
collection>
resultMap>
<select id="getAddressListByUserId" parameterType="Integer" resultMap="userAddressList">
select u.*, a.id as a_id, a.contact, a.addressDesc, a.tel, a.postCode
from smbms_user u, smbms_address a
where u.id=a.userId and u.id=#{id}
select>
12、resultMap > collection使用2 (在第11步的基础上修改)
// UserMapper.java
public interface UserMapper {
public List<User> getAddressListByUserId2(@Param("id")Integer userId);
}
// UserMapper.xml
<resultMap type="User" id="userAddressList2">
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<collection property="addressList" ofType="Address" resultMap="addressResult"/>
resultMap>
<resultMap type="Address" id="addressResult">
<id property="id" column="a_id" />
<result property="postCode" column="postCode" />
<result property="tel" column="tel" />
<result property="contact" column="contact" />
<result property="addressDesc" column="addressDesc" />
resultMap>
<select id="getAddressListByUserId2" parameterType="Integer" resultMap="userAddressList2">
select u.*, a.id as a_id, a.contact, a.addressDesc, a.tel, a.postCode
from smbms_user u, smbms_address a
where u.id=a.userId and u.id=#{id}
select>
13、resultMap自动映射的匹配 级别
// mybatis-config.xml
<settings>
<setting name="logImpl" value="LOG4J"/>
settings>
@Author 瞌睡虫
@mybatis-3.2.2
@Database: mysql 5.7.15
@Tool: MyEclipse