<configuration>
<mappers>
<mapper class="com.shan.hello.mapper.UserMapper"/>
mappers>
configuration>
public interface UserMapper {
@Insert("insert into t_user (name, salary) values (#{name}, #{salary});")
@Options(useGeneratedKeys = true,keyProperty = "id")
void save(User user);
@Delete("delete from t_user where id = #{id};")
void delete(Long id);
@Update("update t_user set name = #{name}, salary = #{salary} where id = #{id};")
void update(User user);
// void update(User user, Long id);//错误:myBatis默认只能传递一个参数
@Select("select id u_id, name as u_name, salary u_salary from t_user where id = #{id}")
@Results(id="BaseResultMap", value= {
@Result(column = "u_id",property = "id"),
@Result(column = "u_name",property = "name"),
@Result(column = "u_salary",property = "salary")
})
User get(Long id);
@Select("select id u_id, name as u_name, salary u_salary from t_user")
@ResultMap("BaseResultMap")
List<User> getListAll();
}
/* 测试查询 */
@Test
public void testGet() throws IOException {
SqlSession session = MyBatisUtil.getSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
User user = userMapper.get(2L);
System.out.println(user);
//5、关闭资源
session.close();
}
if
choose (when, otherwise)
trim (where, set)
foreach
其他(bind,sql,include)
<select id="select" resultType="Employee">
select * from employee
<if test="minSalary != null">
where salary >= #{minSalary}
if>
select>
细节:在xml中 小于符合不能直接输入 < , 会被当做标签的开始标志,需要使用转义符号 <;
防止第一个查询条件是null,加上 where 1=1,然后其他查询条件接着and 写。
<select id="select" resultType="Employee">
select * from employee where 1=1
<if test="minSalary != null">
and salary >= #{minSalary}
if>
<choose>
<when test="deptId > 0">and deptId = #{deptId}when>
<otherwise>and deptId is not nullotherwise>
choose>
select>
解决sql拼接查询条件时第一个条件为null,而加上 where 1=1,导致不能进行索引查询,影响性能。
where 元素:判断查询条件是否有where关键字,若没有,则第一个查询条件之前要插入 where
若发现查询条件是以and/or开头,则会把第一个查询条件前的and/or 替换成 where
<select id="select" resultType="Employee">
select * from employee
<where>
<if test="minSalary != null">
and salary >= #{minSalary}
if>
<if test="maxSalary != null">
and salary <= #{maxSalary}
if>
<choose>
<when test="deptId > 0">and deptId = #{deptId}when>
<otherwise>and deptId is not nullotherwise>
choose>
where>
select>
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},if>
<if test="password != null">password=#{password},if>
<if test="email != null">email=#{email},if>
<if test="bio != null">bio=#{bio}if>
set>
where id=#{id}
update>
<trim prefix="" prefixOverrides="" suffix="" suffixOverrides="">
trim>
□ prefix – 在这个字符串之前插入 prefix 属性值。
□ prefixOverrides – 并且字符串的内容以 prefixOverrides 中的内容开头(可以包含管道符号),那么使用 prefix 属性值替换内容的开头。
□ suffix – 在这个字符串之后插入 suffix 属性值。
□ suffixOverrides –并且字符串的内容以 suffixOverrides 中的内容结尾(可以包含管道符号),那么使用 suffix 属性值替换内容的结尾。
使用 where 等价于: 注意:此时 AND 后面有一个空格。
使用 set 等价于:
/* Mapper接口 */
void batchDelete(@Param("ids")List<Long> ids);
<delete id="batchDelete">
delete from employee where id in
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
foreach>
delete>
/* Mapper接口 */
void batchSave(@Param("emps")List<Employee>emps);
<insert id="batchSave">
insert into employee (name, sn, salary) values
<foreach collection="emps" separator="," item="e">
(#{e.name}, #{e.sn}, #{e.salary})
foreach>
insert>
<mapper namespace="com.shan.hello.mapper.EmployeeMapper">
<sql id="base_where">
<where>
<if test="keyword != null">
and (name like concat('%', #{keyword}, '%') or sn like concat('%', #{keyword}, '%'))
if>
<if test="minSalary != null">
and salary >= #{minSalary}
if>
<if test="maxSalary != null">
and salary <= #{maxSalary}
if>
<if test="deptId > 0">
and deptId = #{deptId}
if>
where>
sql>
<select id="queryForList" resultType="Employee">
select id, name, sn, salary from employee
<include refid="base_where"/>
select>
<select id="queryForCount" resultType="int">
select count(id) from employee
<include refid="base_where"/>
select>
mapper>
<if test="keyword != null">
<bind name="keywordLike" value="'%' + keyword + '%'"/>
and (name like #{keywordLike} or sn like #{keywordLike})
if>