一、开发流程
1)引jar包
//mybatis_coremybatis3.4core\asm-5.2.jarmybatis3.4core\cglib-3.2.5.jarmybatis3.4core\commons-logging-1.2.jarmybatis3.4core\log4j-1.2.17.jarmybatis3.4core\mybatis-3.4.4.jar//db connectorDB-connector\mysql-connector-java-5.1.40-bin.jar
2)变写实体类Student
package com.huitong.entity;public class Student { private Integer id; private String sname; private double salary; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return sname + ":" + salary; }}
3)写映射文件StudentMapper.xml,配置mybatis.xml
INSERT INTO student(sname, salary) VALUES("allen",34.23); INSERT INTO student(sname, salary) VALUES(#{sname},#{salary}); UPDATE student SET sname=#{sname},salary=#{salary} WHERE id=#{id} DELETE FROM student WHERE id=#{id}
注意:如果查询结果返回的对象和数据表中字段,名称名不一致,需要使用resultMap,否则使用resultType。
配置mybatis.xml
4)写工具类MybatisUtil
package com.huitong.util.mybatis;import java.io.IOException;import java.io.Reader;import java.sql.Connection;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MybatisUtil { private static ThreadLocalthreadLocal = new ThreadLocal (); private static SqlSessionFactory sqlSessionFactorysion; //禁止通过new创建对象 private MybatisUtil(){} /** * 加载mybatis配置文件 */ static{ try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); sqlSessionFactorysion = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * 获取sqlsession * @return */ public static SqlSession getSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession == null){ sqlSession = sqlSessionFactorysion.openSession(); threadLocal.set(sqlSession); } return sqlSession; } /** * 关闭sqlsession */ public static void closeSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession != null){ //关闭sqlsession sqlSession.close(); //分离当前线程与sqlsession关系 threadLocal.remove(); } } public static void main(String[] args) { Connection connection = MybatisUtil.getSqlSession().getConnection(); System.out.println(connection!=null?"连接成功":"没有连接成功"); } }
5)StudentDao数据持久层Dao
package com.huitong.dao;import java.util.List;import org.apache.ibatis.session.SqlSession;import com.huitong.entity.Student;import com.huitong.util.mybatis.MybatisUtil;import com.huitong.util.mybatis.mybatisutil2;public class StudentDao { /** * 增加学生 * @throws Exception */ public void add() throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); int n = sqlSession.insert("com.huitong.entity.StudentMapper.add"); System.out.println(n); sqlSession.commit(); } catch (Exception e){ e.printStackTrace(); sqlSession.rollback(); } finally { MybatisUtil.closeSqlSession(); } } public void add2(Student stu) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); int n = sqlSession.insert("com.huitong.entity.StudentMapper.add2",stu); System.out.println(n); sqlSession.commit(); } catch (Exception e){ e.printStackTrace(); sqlSession.rollback(); } finally { MybatisUtil.closeSqlSession(); } } public Student getStudentById(int id) throws Exception{ SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ Student student = sqlSession.selectOne(Student.class.getName() + ".getStudentById", id); return student; } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } finally { MybatisUtil.closeSqlSession(); } } public ListgetAll() throws Exception{ SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ return sqlSession.selectList(Student.class.getName() + ".getAll"); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } finally { MybatisUtil.closeSqlSession(); } } public void update(Student stu) throws Exception{ SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ int n = sqlSession.update(Student.class.getName() + ".update",stu); System.out.println(n); sqlSession.commit(); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw new RuntimeException(e); }finally{ MybatisUtil.closeSqlSession(); } } public void delete(int id){ SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ int n = sqlSession.delete(Student.class.getName() + ".delete", id); System.out.println(n); sqlSession.commit(); } catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw new RuntimeException(e); } finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) { StudentDao studentDao = new StudentDao();// Student stu = new Student();// stu.setId(2);// stu.setSname("beed");// stu.setSalary(20.12);// try { studentDao.delete(3); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }}