下载jar:
mybatis-3.4.6.jar
mysql-connector-java-5.1.34-bin.jar
1) 创建一个Dynamic Web Project,名称为:xm-mybatis-one,项目结构图如下:
2)创建员工实体文件EmployeeEntity.java
package com.xm.mybatis.entity;
/**
* 员工表 entity
* @author ouyangjun
*/
public class EmployeeEntity {
private Integer empId;
private String empName;
private String empNO;
private Long createDate;
public Integer getEmpId() {return empId;}
public void setEmpId(Integer empId) {this.empId = empId;}
public String getEmpName() {return empName;}
public void setEmpName(String empName) {this.empName = empName;}
public String getEmpNO() {return empNO;}
public void setEmpNO(String empNO) {this.empNO = empNO;}
public Long getCreateDate() {return createDate;}
public void setCreateDate(Long createDate) {this.createDate = createDate;}
}
3)创建员工实体映射文件EmployeeEntity.xml
insert into employee(emp_id,emp_name,emp_no,create_date)
values(#{empId}, #{empName}, #{empNO}, #{createDate})
delete from employee where 1=1
and emp_Id in
#{item}
4)创建员工接口文件EmployeeDAO.java
package com.xm.mybatis.dao;
import com.xm.mybatis.entity.EmployeeEntity;
public interface EmployeeDAO {
/**
* 根据empId获取员工信息
*/
EmployeeEntity getEmployeeByEmpId(String empId);
/**
* 添加员工
*/
int addEmployee(EmployeeEntity entity);
/**
* 根据empId修改用户信息
*/
int updateEmployee(EmployeeEntity entity);
/**
* 根据empId删除用户
*/
int deleteEmployeeEntityByEmpId(String[] empIds);
}
5)创建mybatis核心配置文件mybatis.cfg.xml。注:该文件名可随意,在读取配置时指定。
6)创建获取SqlSessionFactory的工具类SessionUtils.java。通过单列模式实现。
package com.xm.mybatis.utils;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SessionUtils {
/**
* 内部类单列模式实现读取mybatis配置文件
* @author ouyangjun
*/
private static class LasyHolder {
private static Reader READER = getReader();
public static Reader getReader() {
try {
System.out.println("==>读取mybatis.cfg.xml文件中的配置!");
// 读取mybatis配置文件
return Resources.getResourceAsReader("mybatis.cfg.xml");
} catch (IOException e) {
System.out.println("==>读取mybatis.cfg.xml文件中的配置错误 error!");
e.printStackTrace();
}
return null;
}
}
/**
* 获取配置文件对象
*/
public static Reader getReader() {
return LasyHolder.READER;
}
/**
* 线程本地配置声明
*/
public static ThreadLocal threadLocal = new ThreadLocal();
/**
* ThreadLocal获取SqlSessionFactory
*/
public static SqlSessionFactory getSqlSessionFactory() {
// 获取sqlSessionFactory
SqlSessionFactory sqlSessionFactory = threadLocal.get();
System.out.println("sqlSessionFactory: " + sqlSessionFactory);
if (sqlSessionFactory == null) {
System.out.println("sqlSessionFactory is null 重新获取SqlSessionFactory!");
// 读取mybatis配置文件
Reader reader = SessionUtils.getReader();
// 重新创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 把sqlSessionFactory添加到ThreadLocal中
threadLocal.set(sqlSessionFactory);
}
// 返回
return sqlSessionFactory;
}
}
7)添加员工新增测试类AddEmployeeTest.java,执行该方法之后可自行到mysql数据库查看。
package com.xm.mybatis.test;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import com.xm.mybatis.dao.EmployeeDAO;
import com.xm.mybatis.entity.EmployeeEntity;
import com.xm.mybatis.utils.SessionUtils;
public class AddEmployeeTest {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSqlSessionFactory().openSession();
// 初始化接口
EmployeeDAO employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
EmployeeEntity emp = new EmployeeEntity();
emp.setEmpId(100000);
emp.setEmpName("ouyangjun");
emp.setEmpNO("333333");
emp.setCreateDate(new Date().getTime());
// 持久化
employeeDAO.addEmployee(emp);
emp = new EmployeeEntity();
emp.setEmpId(200000);
emp.setEmpName("xm");
emp.setEmpNO("888888");
emp.setCreateDate(new Date().getTime());
// 持久化
employeeDAO.addEmployee(emp);
// 提交事务
sqlSession.commit();
System.out.println("==>员工数据持久化成功!");
} catch (Exception e){
e.printStackTrace();
sqlSession.rollback();
System.out.println("==>员工数据持久化失败!");
} finally {
sqlSession.close();
System.out.println("==>事务关闭!");
}
}
}
8)添加员工查询测试类GetEmployeeTest.java
package com.xm.mybatis.test;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import com.xm.mybatis.dao.EmployeeDAO;
import com.xm.mybatis.entity.EmployeeEntity;
import com.xm.mybatis.utils.SessionUtils;
public class GetEmployeeTest {
public static void main(String[] args) throws IOException {
// 获取SqlSession
SqlSession sqlSession = SessionUtils.getSqlSessionFactory().openSession();
// 初始化接口
EmployeeDAO employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
EmployeeEntity entity = employeeDAO.getEmployeeByEmpId("100000");
if(entity!=null){
System.out.println(entity.getEmpId()+" "+entity.getEmpName()+" "+entity.getEmpNO());
} else {
System.out.println("根据empId未查询到数据!");
}
System.out.println("----------测试SqlSessionFactory单列----------");
sqlSession = SessionUtils.getSqlSessionFactory().openSession();
// 初始化接口
employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
entity = employeeDAO.getEmployeeByEmpId("100000");
if(entity!=null){
System.out.println(entity.getEmpId()+" "+entity.getEmpName()+" "+entity.getEmpNO());
} else {
System.out.println("根据empId未查询到数据!");
}
sqlSession.close();
}
}
查询效果图如下:
9)添加员工删除测试类DeleteEmployeeTest.java
package com.xm.mybatis.test;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import com.xm.mybatis.dao.EmployeeDAO;
import com.xm.mybatis.utils.SessionUtils;
public class DeleteEmployeeTest {
public static void main(String[] args) throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSqlSessionFactory().openSession();
// 初始化接口
EmployeeDAO employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
String[] empIds = {"100000","200000"};
employeeDAO.deleteEmployeeEntityByEmpId(empIds);
sqlSession.commit();
System.out.println("==>员工数据删除成功!");
} catch (Exception e){
e.printStackTrace();
sqlSession.rollback();
System.out.println("==>员工数据删除失败!");
} finally {
sqlSession.close();
System.out.println("==>事务关闭!");
}
}
}
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!