mybatis的sql映射文件—增删改查
前提:需要的包log4j.jar,mybatis-3.4.1.jar,mysql-connector-java-5.1.37-bin.jar
1.基本类
员工类
public class Employee {
private Integer eId;
private String eName;
private Integer gender;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer eId,String eName, Integer gender, String email) {
super();
this.eId=eId;
this.eName = eName;
this.gender = gender;
this.email = email;
}
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public String getEName() {
return eName;
}
public void setEname(String ename) {
this.eName = ename;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
}
}
部门类
package com.hand.mybatis.bean;
import java.util.List;
public class Department {
private Integer id;
private String departName;
private List<Employee> empList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department [id=" + id + ", departName=" + departName + "]";
}
}
EmployeeMapper.java 接口
package com.hand.mybatis.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import com.hand.mybatis.bean.Employee;
public interface EmployeeMapper {
Employee selectEmployee(Integer eid);
int addEmp(Employee employee);
Boolean updateEmp(Employee employee);
int deleteEmpById(Integer id);
//传入多个参数的情况
//Employee getEmp(@Param("id") Integer id,@Param("ename") String ename);
//传入多个参数的情况(map)
Employee getEmp(Map<String, Object> map);
//集合List
List<Employee> getEmpByeNameLike(String ename);
//返回一条记录的map;key就是列名,值是对应的值
Map<String, Object> getEmpByIdReturnMap(Integer id);
//多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
//告诉mybatis封装这个map的时候使用哪个属性作为map的key
/*@MapKey("eId")
Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String ename);*/
@MapKey("eName")
Map<String, Employee> getEmpByLastNameLikeReturnMap(String ename);
}
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.hand.mybatis.dao.EmployeeMapper">
<!-- mapper接口 public interface EmployeeMapper {} -->
<!-- 查找
Employee selectEmployee(Integer eid); -->
<select id="selectEmployee" resultType="com.hand.mybatis.bean.Employee">
SELECT * FROM emp WHERE eid=#{eid}
</select>
<!--添加
int addEmp(Employee employee);-->
<insert id="addEmp" useGeneratedKeys="true" keyProperty="eId"> //使用 useGeneratedKeys="true" keyProperty="eId" 调用接口时,可以获得主键eid
insert into emp(ename,gender,email)
VALUES(#{eName},#{gender},#{email})
</insert>
<!-- 更新
Boolean updateEmp(Employee employee); --> //返回值类型Boolen int void long 都行
<update id="updateEmp">
UPDATE emp SET ename=#{eName},gender=#{gender},email=#{email}
WHERE eid=#{eId}
</update>
<!--删除
int deleteEmpById(Integer id); -->
<delete id="deleteEmpById">
DELETE FROM emp WHERE eid=#{eid}
</delete>
<!--
//传入多个参数的情况
//Employee getEmp(@Param("id") Integer id,@Param("ename") String ename); //使用@Param()注解
//传入多个参数的情况(map)
Employee getEmp(Map<String, Object> map); --> //调用接口时,需要给map赋值
<select id="getEmp" resultType="com.hand.mybatis.bean.Employee">
SELECT * FROM emp WHERE eid=#{id} AND ename=#{ename}
</select>
map.put("ename", "张三");
Employee employee=employeeMapper.getEmp(map);
System.out.println(employee);
<!-- //集合List
List<Employee> getEmpByeNameLike(String ename); -->
<select id="getEmpByeNameLike" resultType="com.hand.mybatis.bean.Employee">
SELECT * FROM emp WHERE ename like #{ename}
</select>
<!-- //返回一条记录的map;key就是列名,值是对应的值
Map<String, Object> getEmpByIdReturnMap(Integer id); -->
<select id="getEmpByIdReturnMap" resultType="map">
SELECT * FROM emp WHERE eid=#{id}
</select>
调用 Map<String, Object> getEmpByIdReturnMap(Integer id)接口:
Map<String, Object> map= employeeMapper.getEmpByIdReturnMap(101);
System.out.println(map);
结果:
<!-- //多条记录封装一个map:Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的javaBean
//告诉mybatis封装这个map的时候使用哪个属性作为map的key
/*@MapKey("eId")
Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String ename);*/
@MapKey("eName")
Map<String, Employee> getEmpByLastNameLikeReturnMap(String ename); -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.hand.mybatis.bean.Employee">
SELECT * FROM emp WHERE ename like #{ename}
</select>
调用:
Map<Integer, Employee> map=employeeMapper.getEmpByLastNameLikeReturnMap("%张%");
System.out.println(map);
结果 {101=Employee [eId=101, ename=张三, gender=0, email=2714763867@qq.com], 102=Employee [eId=102, ename=张丽, gender=0, email=163826386@qq.com], 103=Employee [eId=103, ename=杨张, gender=1, email=21901826380863@163.com]}
</mapper>
代码:https://github.com/shuaishuaihand/mybatis.git