动态SQL
项目结构:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/42422718685e9c411f56fc7e0f74a39f.png#pic_center)
一、核心配置文件 (配置了log4j)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.xxxx.pojo"/>
</typeAliases>
<environments default="ev">
<environment id="ev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.xxxx.mappers"/>
</mappers>
</configuration>
二、工具类 和 pojo
package com.xxxx.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
public class SessionUtils {
private static SqlSessionFactory factory = null;
static{
try {
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
SqlSession session = null;
if(factory!=null){
session = factory.openSession(true);
}
return session;
}
}
package com.xxxx.pojo;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class Emp implements Serializable {
private int empno;
private String ename;
private String job;
private int mgr;
private double sal;
private double comm;
private Date date;
private Integer deptno;
public Emp() {
}
public Emp(int empno, String name, String job, int mgr, double sal, double comm, Date hiredate, Integer deptno) {
this.empno = empno;
this.ename = name;
this.job = job;
this.mgr = mgr;
this.sal = sal;
this.comm = comm;
this.date = hiredate;
this.deptno = deptno;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String name) {
this.ename = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", name='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", sal=" + sal +
", comm=" + comm +
", hiredate=" + date +
", deptno=" + deptno +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Emp emp = (Emp) o;
return empno == emp.empno &&
mgr == emp.mgr &&
Double.compare(emp.sal, sal) == 0 &&
Double.compare(emp.comm, comm) == 0 &&
Objects.equals(ename, emp.ename) &&
Objects.equals(job, emp.job) &&
Objects.equals(date, emp.date) &&
Objects.equals(deptno, emp.deptno);
}
@Override
public int hashCode() {
return Objects.hash(empno, ename, job, mgr, sal, comm, date, deptno);
}
}