MyBatis映射文件(集合数据的添加与获取,条件查询,模糊查询)
继续上次的MyBatis的映射文件,
EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cn.mybatis.dao.EmployeeMapper">
<!-- public List<Employee> getEmpsByLastNameLike(String name); -->
<!-- restuletType: 如果返回的是一个集合,要写集合元素的类型 -->
<select id="getEmpsByLastNameLike" resultType="com.cn.zhu.bean.Employee">
select * from tbl_employee
where last_name like #{lastName}
</select>
<!--
namespace: 名称空间 id:唯一标识 resultType: 返回值类型 #{id} 从传递过来的参数中取出id
-->
<select id="getEmpByMap" resultType="com.cn.zhu.bean.Employee">
select * from tbl_employee
where id = #{id} and last_name=#{lastName}
</select>
<select id="getEmpByIdAndLastName" resultType="com.cn.zhu.bean.Employee">
select * from tbl_employee
where id = #{id} and last_name=#{lastName}
</select>
<select id="getEmpById" resultType="com.cn.zhu.bean.Employee">
select * from tbl_employee
where id = #{id}
</select>
<!--
mysql自增主键,主键主键值的获取,mybatis也是利用statement.getGenreatedKeys()
useGeneratedKeys="true" 使用自增主键获取主键值策略
keyProperty: 指定对应的主键属性,也就是mybatis获取到主键值以后,将这个
值封装到javaBean的哪个属性
-->
<insert id="addEmp" parameterType="com.cn.zhu.bean.Employee"
useGeneratedKeys="true" keyProperty="id">
insert into tbl_employee(last_name,gender,email) values(#{lastName},#{gender},#{email})
</insert>
<!--
Orcal不支持自增,Orcal使用序列来模拟自增
每次插入的数据的主键是从序列中拿到的值,如何获取到这个值
-->
<insert id="">
<!-- keyProperty 查出的主键值封装给javaBean哪个属性
order="BEFORE" :当前sql插在sql之前运行
resultType : 查出数据的返回值类型
-->
<selectKey keyProperty="id" order="BEFORE" resultType="integer">
<!-- 编写主键的sql语句 -->
select EMPLOYEES_SEQ.nextval from dual
</selectKey>
<!-- 插入时的主键是从序列中拿到的 -->
insert into employees(EMPLOYEE_ID,LAST_NAME,EMATL)
VALUES (#{ID},#{lastname},#{email})
</insert>
<update id="updateEmp">
update tbl_employee set last_name=#{lastName},gender=#{gender},email=#{email}
where id=#{id}
</update>
<delete id="deleteEmp">
delete from tbl_employee where id=#{id}
</delete>
</mapper>
EmployeeMapper.java
package com.cn.mybatis.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.cn.zhu.bean.Employee;
public interface EmployeeMapper {
public Employee getEmpById(int id);
public long addEmp(Employee employee);
public Boolean updateEmp(Employee employee);
public void deleteEmp(int id);
//根据两个参数进行查询
public Employee getEmpByIdAndLastName(@Param("id")Integer id,@Param("lastName")String lastName);
public Employee getEmpByMap(Map<String, Object> map);
public List<Employee> getEmpsByLastNameLike(String name);
}
测试方法:
@Test
public void test05() throws IOException{
SqlSessionFactory sqlsessionFactory=getSqlSessionFactory();
// 1 获取到的sqlsession不会自动提交数据
SqlSession openSession=sqlsessionFactory.openSession();
try{
EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
List<Employee> like=mapper.getEmpsByLastNameLike("%zh%");
for(Employee employee : like ){
System.out.println(employee);
}
}finally{
openSession.commit();
}
}
模糊查询的测试结果: