mybatis-2-增删改查
CRUD操作
接口
package com.wang.mybatis;
import java.util.List;
public interface EmployeeMapper {
//通过id获取返回职员对像
//这个方法只有输入数据类型和输出数据类型,刚好对应了我们需要在mapper.xml文件中写的输入数据和输出数据
public Employee getEmpById(Integer id);
//获取全部职员
public List<Employee> getEmpList();
//insert一个职员
//insert的方法最好是返回一个int,insert成功会返回1,失败会返回-1,我们可以根据这个来判断是否insert成功
public int addEmployee(Employee e);
//update一个职员
//成功会返回1,失败会返回-1
public int updateEmployee(Employee employee);
//删除员工
public int deleteEmployee(int id);
}
sql映射主配置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">
<!--namespace的值为mapper对应的那个类的包名-->
<mapper namespace="com.wang.mybatis.EmployeeMapper">
<!--resultType的值为对应的Java类-->
<!--id可以理解为唯一标识,之后将通过id调用对应sql-->
<select id="getEmpById" resultType="com.wang.mybatis.Employee">
<!--#{id}类似于jdbc中的占位符?,表示传过来的参数-->
select id,last_name lastName ,email,gender from tbl_employee where id = #{id}
</select>
<!--获取全部员工信息-->
<select id="getEmpList" resultType="com.wang.mybatis.Employee">
select * from tbl_employee
</select>
<!-- insert一个用户-->
<!-- 我们这里虽然传入的是Employee,但是我们在#{}可以直接获取到实例中的属性-->
<insert id="addEmployee" parameterType="com.wang.mybatis.Employee">
<!--注意点,我们的构造方法的顺序也必须和这个歌一致,否则报错-->
insert into tbl_employee (last_name,gender,email) values (#{lastName},#{gender},#{email})
</insert>
<!--传入参数为基本数据类型时不需要添加属性-->
<update id="updateEmployee" >
update tbl_employee
set last_name = #{lastName},gender = #{gender},email = #{email}
where last_name = #{lastName}
</update>
<!--删除员工-->
<delete id="deleteEmployee">
delete from tbl_employee where id=#{id}
</delete>
</mapper>
测试
@org.junit.Test
public void test3(){
//获取到sqlsession对象
InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//利用getmapper方法获取到Employee的实例化结果
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
System.out.println(mapper.getEmpById(2));
System.out.println("__________________");
//获取全部员工信息
List<Employee> empList = mapper.getEmpList();
Iterator<Employee> iterator = empList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//插入新员工信息
Employee employee = new Employee("阿三", "男","333@qq.com");
int i1 = mapper.addEmployee(employee);
if (i1 > 0) {
sqlSession.commit();
System.out.println("数据提交成功");
}else{
System.out.println("数据提交失败");
}
//更新员工信息
Employee re_employee = new Employee("阿三", "男","999@qq.com");
int i2 = mapper.updateEmployee(re_employee);
if (i2 > 0) {
sqlSession.commit();
System.out.println("数据更新成功");
}else{
System.out.println("数据更新失败");
}
//删除员工
int i3 = mapper.deleteEmployee(6);
if (i3 > 0) {
sqlSession.commit();
System.out.println("数据删除成功");
}else{
System.out.println("数据删除失败");
}
}
拓展
使用map插入
我们之前一般时创建一个包含了我们需要传入数据的对象给数据库
但是,当数据量比较大,且复杂时就有点麻烦
这个时候我们一般传入一个包含了数据的map对象给数据库
sql映射接口mapper中
//map属性加入
public int addEmpployee2(Map<String,Object> map);
sql映射xml配置文件中
<!--map测试-->
<insert id="addEmpployee2" parameterType="map">
insert into tbl_employee (id,last_name,gender,email) values (#{id},#{userlastName},#{usergender},#{useremail})
</insert>
测试
@org.junit.Test
public void test2(){
//获取sqlsession
SqlSession sqlSession = getSqlSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
HashMap<String, Object> stringObjectHashMap = new HashMap<String, Object>();
//在map中插入数据
stringObjectHashMap.put("id",30);
stringObjectHashMap.put("userlastName","左昌");
stringObjectHashMap.put("usergender","女");
stringObjectHashMap.put("useremail","111@ll.com");
//传入mao数据
int i = mapper.addEmpployee2(stringObjectHashMap);
if (i>0){
sqlSession.commit();
System.out.println("数据提交成功");
}else{
System.out.println("数据提交失败");
}
}
试试:
可以试一下用map内包含一个id来查询
使用map输出
我们之前使用的时List
- 我们可以这样输出:HashMap<Integer,Employee>来达到目的
- 但我们怎么让id作为主键呢:在接口的方法上加上注解@MapKey("id");
接口中
@MapKey("id")
public HashMap<String,Employee> getEmployeeList(HashMap<String, Object> map);
sql映射文件中
<select id="getEmployeeList" resultType="map">
select * from tbl_employee where last_name like concat('%',#{value},'%');
</select>
实现模糊查询
接口
public List<Employee> getEmpLike(Map<String, Object> map);
mapperxml配置文件,注意使用concat函数拼接字符串
<select id="getEmpLike" parameterType="map" resultType="com.wang.mybatis.Employee">
select *
from tbl_employee
where last_name like concat('%',#{value},'%')
</select>
测试
@org.junit.Test
public void test5(){
SqlSession sqlsession = getSqlSession();
EmployeeMapper mapper = sqlsession.getMapper(EmployeeMapper.class);
HashMap<String, Object> map = new HashMap<String,Object>();
map.put("value","李");
List<Employee> empSlur = mapper.getEmpLike(map);
Iterator<Employee> iterator = empSlur.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}