packageloaderman;importjava.io.IOException;importjava.io.Reader;importjava.sql.Connection;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;/*** 工具类*/
public classMybatisUtil {private static ThreadLocal threadLocal = new ThreadLocal();private staticSqlSessionFactory sqlSessionFactory;/*** 加载位于src/mybatis.xml配置文件*/
static{try{
Reader reader= Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory= newSqlSessionFactoryBuilder().build(reader);
}catch(IOException e) {
e.printStackTrace();throw newRuntimeException(e);
}
}/*** 禁止外界通过new方法创建*/
privateMybatisUtil(){}/*** 获取SqlSession*/
public staticSqlSession getSqlSession(){//从当前线程中获取SqlSession对象
SqlSession sqlSession =threadLocal.get();//如果SqlSession对象为空
if(sqlSession == null){//在SqlSessionFactory非空的情况下,获取SqlSession对象
sqlSession =sqlSessionFactory.openSession();//将SqlSession对象与当前线程绑定在一起
threadLocal.set(sqlSession);
}//返回SqlSession对象
returnsqlSession;
}/*** 关闭SqlSession与当前线程分开*/
public static voidcloseSqlSession(){//从当前线程中获取SqlSession对象
SqlSession sqlSession =threadLocal.get();//如果SqlSession对象非空
if(sqlSession != null){//关闭SqlSession对象
sqlSession.close();//分开当前线程与SqlSession对象的关系,目的是让GC尽早回收
threadLocal.remove();
}
}/*** 测试*/
public static voidmain(String[] args) {
Connection conn=MybatisUtil.getSqlSession().getConnection();
System.out.println(conn!=null?"连接成功":"连接失败");
}
}
packageloaderman;/*** 学生*/
public classStudent {private Integer id;//编号
private String name;//姓名
private Double sal;//薪水
publicStudent(){}publicStudent(Integer id, String name, Double sal) {this.id =id;this.name =name;this.sal =sal;
}publicInteger getId() {
System.out.println("getId()");returnid;
}public voidsetId(Integer id) {this.id =id;
}publicString getName() {
System.out.println("getName()");returnname;
}public voidsetName(String name) {this.name =name;
}publicDouble getSal() {
System.out.println("getSal()");returnsal;
}public voidsetSal(Double sal) {this.sal =sal;
}
}
packageloaderman;importjava.util.List;importorg.apache.ibatis.session.SqlSession;/*** 持久层
*@authorAdminTC*/
public classStudentDao1 {/*** 增加学生*/
public void add(Student student) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
sqlSession.insert("loaderman.Student.add",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}/*** 根据ID查询学生*/
public Student findById(int id) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
Student student= sqlSession.selectOne("loaderman.Student.findById",id);
sqlSession.commit();returnstudent;
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}/*** 查询所有学生*/
public List findAll() throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();return sqlSession.selectList("loaderman.Student.findAll");
}catch(Exception e){
e.printStackTrace();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}/*** 更新学生*/
public void update(Student student) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
sqlSession.update("loaderman.Student.update",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}/*** 删除学生*/
public void delete(Student student) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
sqlSession.delete(Student.class.getName()+".delete",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}public static void main(String[] args) throwsException{
StudentDao1 dao= newStudentDao1();//dao.add(new Student(1,"哈哈",7000D));//dao.add(new Student(2,"呵呵",8000D));//dao.add(new Student(3,"班长",9000D));//dao.add(new Student(4,"键状高",10000D));//Student student = dao.findById(4);
List studentList =dao.findAll();for(Student student : studentList){
System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());
System.out.println();
}//Student student = dao.findById(3);//student.setName("靓班长");//dao.update(student);//Student student = dao.findById(3);//System.out.print(student.getId()+":"+student.getName()+":"+student.getSal());//dao.delete(student);
}
}
packageloaderman;importorg.apache.ibatis.session.SqlSession;/*** 持久层*/
public classStudentDao2 {/*** 增加学生*/
public void add(Student student) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
sqlSession.insert("loaderman.Student.add",student);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}/*** 根据ID查询学生*/
public Student findById(int id) throwsException{
SqlSession sqlSession= null;try{
sqlSession=MybatisUtil.getSqlSession();
Student student= sqlSession.selectOne("loaderman.Student.findById",id);
sqlSession.commit();returnstudent;
}catch(Exception e){
e.printStackTrace();
sqlSession.rollback();throwe;
}finally{
MybatisUtil.closeSqlSession();
}
}public static void main(String[] args) throwsException{
StudentDao2 dao= newStudentDao2();//dao.add(new Student(1,"班长",7000D));
Student student = dao.findById(1);if(student == null){
System.out.println("YES");
}
System.out.println(student.getId()+":"+student.getName()+":"+student.getSal());
}
}
/p>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into students(id,name,sal) values(#{id},#{name},#{sal})
select id,name,sal from students where id=#{id}
select id,name,sal from students
update students set name=#{name},sal=#{sal} where id=#{id}
delete from students where id=#{id}
/p>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into students(students_id,students_name,students_sal)
values(#{id},#{name},#{sal})
select students_id,students_name,students_sal
from students
where students_id=#{xxxxxxxxxxxxxxxxxx}
db.properties
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://127.0.0.1:3306/loaderman
mysql.username=root
mysql.password=root
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
oracle.username=scott
oracle.password=tiger
/p>
"http://mybatis.org/dtd/mybatis-3-config.dtd">