Map getUserToMap(@Param("id") int id);
select * from t_user where id = #{id}
List> getAllUserToMap();
select * from t_user
@MapKey("id")
Map getAllUserToMap();
select * from t_user
{ 1={password=123456, sex=男, id=1, age=23, username=admin}, 2={password=123456, sex=男, id=2, age=23, username=张三}, 3={password=123456, sex=男, id=3, age=23, username=张三} }
List testMohu(@Param("mohu") String mohu);
select * from t_user where username like "%"#{mohu}"%"
mapper.deleteMore("1, 2, 3");
int deleteMore(@Param("ids") String ids);
delete from t_user where id in (${ids})
List getAllUser(@Param("tableName") String tableName);
select * from ${tableName}
//useGeneratedKeys:设置使用自增的主键,keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参 数user对象的某个属性中
int insertUser(User user);
insert into t_user values(null,#{username},#{password},#{age},#{sex})
List getAllEmp();
select id, emp_name empName, age from t_emp
# mybatis配置文件中配置如下
# mapper
List getAllEmp();
# xml映射
select id, emp_name, age from t_emp
# mapper
List getAllEmp();
# xml映射
select id, emp_name, age from t_emp
# 部门表
public class Dept {
private Integer did;
private String deptName;
//private List emps;
@Override
public String toString() {
return "Dept{" +
"did=" + did +
", deptName='" + deptName + '\'' +
//", emps=" + emps +
'}';
}
//public List getEmps() {
//return emps;
//}
//public void setEmps(List emps) {
//this.emps = emps;
//}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Dept() {
}
public Dept(Integer did, String deptName) {
this.did = did;
this.deptName = deptName;
}
}
# 员工表,添加部门对象作为属性,编写getter方法和setter方法,toString方法
public class Emp implements Serializable {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
private Dept dept;
@Override
public String toString() {
return "Emp{" +
"eid=" + eid +
", empName='" + empName + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
", dept=" + dept +
'}';
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Emp() {
}
public Emp(Integer eid, String empName, Integer age, String sex, String email) {
this.eid = eid;
this.empName = empName;
this.age = age;
this.sex = sex;
this.email = email;
}
}
Emp getEmpAndDept(@Param("eid") Integer eid);
select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
Emp getEmpAndDept(@Param("eid") Integer eid);
select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
# 部门mapper
Dept getEmpDeptByStep(@Param("did") int did);
# 部门映射文件,根据部门id查询部门信息
select * from t_dept where did = #{did}
# 员工mapper
Emp getEmpByStep(@Param("eid") int eid);
# 员工映射文件
select * from t_emp where eid = #{eid}
# lazyLoadingEnabled设置为true,aggressiveLazyLoding设置为false,mybatis版本>=3.4.1时默认为true
# 部门表,添加员工对象,并添加getter和setter方法,toString方法
public class Dept {
private Integer did;
private String deptName;
private List emps;
@Override
public String toString() {
return "Dept{" +
"did=" + did +
", deptName='" + deptName + '\'' +
", emps=" + emps +
'}';
}
public List getEmps() {
return emps;
}
public void setEmps(List emps) {
this.emps = emps;
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Dept() {
}
public Dept(Integer did, String deptName) {
this.did = did;
this.deptName = deptName;
}
}
# mapper
Dept getDeptEmpByDid(@Param("did") int did);
# xml,配置返回结果集,根据部门id查询数据
select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did = emp.did where dept.did = #{did}
# 部门mapper
Dept getDeptByStep(@Param("did") int did);
# 部门xml,根据部门id查询数据
select * from t_dept where did = #{did}
# 员工mapper
List getEmpListByDid(@Param("did") int did);
# 员工xml,根据员工id查询数据
select * from t_emp where did = #{did}