个人笔记,如有错误,恳请批评指正。
MyBatis 世界上流行最广泛的基于SQL语句的ORM框架,由Clinton Begin 在2002 年创建,其后,捐献给了Apache基金会,成立了iBatis 项目。MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
a. 学习成本:MyBatis简单易学(特别是有SQL语法基础的人),较接近JDBC b. 程序灵活性:MyBatis直接使用SQL,灵活性高 c. 程序执行效律:MyBatis效律高 d. 可移植性:hibernate较好(与数据库关联在配置中完成,HQL语句与数据库无关)
mybatis提供一种“半自动化”的ORM实现。 这里的“半自动化”,是相对Hibernate等提供了全面的数据库封装机制的“全自动化”ORM实现而言,“全自动”ORM实现了POJO和数据库表之间的映射,以及SQL的自动生成和执行。而mybatis的着力点,则在于POJO与SQL之间的映射关系。
MyBatis是一个灵活的DAO层解决方案,满足较高的性能要求,可以在很多场合使用,但一般以下场合不建议使用: a. 需要支持多种数据库或数据库有移植要求 b. 完全动态sql,例如:字段要动态生成 c. 使用的不是关系数据库
sql映射文件建立了POJO与SQL之间的依赖关系。 此时需要更新myBatis-config.xml中对sql映射配置的应用。 - 修改myBatis-config.xml,加入映射文件信息
<?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> <environments default="development"> ………… </environments> <mappers> <mapper resource="cn/itcast/entity/DeptMapper.xml" /> </mappers></configuration>编写数据库操作 包括操作接口及实现,接口略,实现类为:DeptDaoImpl.javapublic class DeptDaoImpl { private Sqlsession session = null; /* *1.读取配置文件信息 *2.构建session工厂 *3.创建session *4.开启事务 *5.处理数据 *6.提交/回滚数据 *7.关闭session */ @Deprecated public int save(Dept dept){ int i = 0; String path = "mybatis-config.xml"; SqlSession session = null; Reader reader = null; try { reader = Resources.getResourceAsReader(path); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); session = sessionFactory.openSession(); //参数1:定义的sql 参数2:sql的值// SQL映射文件定义的命名空间+SQL语句的ID定位SQL语句 i = session.insert("cn.ustb.entity.DeptMapper.insertDept", dept); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(session != null){ session.close(); } } return i; }编写测试类 需要导入junit包public class DeptTest { private static DeptDaoImpl deptDaoImpl; @BeforeClass public static void setUpBeforeClass() throws Exception { deptDaoImpl=new DeptDaoImpl(); } @AfterClass public static void tearDownAfterClass() throws Exception { deptDaoImpl=null; } @Test public void testInsert() { Dept dept=new Dept(); dept.setDeptName("市场部"); dept.setDeptAddress("深圳"); int i=deptDaoImpl.insert(dept); System.out.println("受影响行数:"+i); }}在myBatis的主配置文件给cn.itcast.entity.Dept类创建别名Dept,后继的DeptMapper.xml配置文件中可以使用别名
<!-- 通过别名简化对类的使用 --><typeAliases> <typeAlias type="cn.ustb.entity.Dept" alias="Dept" /></typeAliases>封装了获取及关闭session的操作,并保证在线程池中始终只有一份session,节省了资源。
public class MybatisSessionFactory { private static final ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); private static SqlSessionFactory sqlSessionFactory = null; private static String CONFIG_FILE_LOCATION = "myBatis-config.xml"; private static String configFile = CONFIG_FILE_LOCATION; static{ buildSessionFactory(); } public static SqlSession getSession() throws Exception{ SqlSession session = threadLocal.get(); if(session == null){ if(sqlSessionFactory == null){ buildSessionFactory(); } session = (sqlSessionFactory!=null)? sqlSessionFactory.openSession():null; threadLocal.set(session); } return session; } public static void closeSession(){ SqlSession session = threadLocal.get(); threadLocal.set(null); if(session != null ){ session.close(); System.out.println("***Success Session Closing***"); } } public static void buildSessionFactory(){ Reader reader = null; try { reader = Resources.getResourceAsReader(CONFIG_FILE_LOCATION); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); System.out.println("%%%%Create sessionFactory error%%%%"); }finally{ if(reader!=null){ try { reader.close(); System.out.println("***Success reader Closing***"); } catch (Exception e) { System.out.println("%%%% Closing sessionFactory error %%%%"); e.printStackTrace(); } } } }}修改DeptMapper.xml配置insert语句(使用之前配置好的别名)
<!--parameterType="Dept"不写时,也能自动根据代码传递的参数Dept自动匹配 内容--><insert id="insert" parameterType="Dept"> insert into dept(dept_name) values(#{deptName});</insert>修改DeptDaoImpl.java新增方法(使用MyBatisUtil.java工具类):
public int save2(Dept dept){ int i = 0; SqlSession session = null; try { session = MybatisSessionFactory.getSession(); //参数1:定义的sql 参数2:sql的值 i = session.insert("cn.ustb.entity.DeptMapper.insertDept", dept); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } finally{ MybatisSessionFactory.closeSession(); } return i; }修改配置文件deptMapper.xml,添加
<delete id="delete" parameterType="Dept"> delete from dept where dept_id=#{deptId}</delete>修改DeptDaoImpl.java,添加delete方法:
public boolean delete(Integer id){ boolean state = false; try { session = MybatisSessionFactory.getSession(); int i = session.delete("cn.ustb.entity.DeptMapper.deleteDept",id); session.commit(); if(i!=0)state = true; } catch (Exception e) { e.printStackTrace(); session.rollback(); state = false; }finally{ MybatisSessionFactory.closeSession(); } return state;}修改配置文件deptMapper.xml,添加update语句。传入的值用spel表达式#{}获取
<update id="update" parameterType="Dept"> update dept set dept_name=#{deptName} ,dept_address=#{deptAddress} where dept_id=#{deptId} </update>修改DeptDaoImpl.java,添加update方法:
public int update (Dept dept){ int i = 0; try { session = MybatisSessionFactory.getSession(); i = session.update("cn.ustb.entity.DeptMapper.updateDept", dept); session.commit();//necessary } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally{ MybatisSessionFactory.closeSession(); } return i;}修改DeptDaoImpl.java,添加selectOne方法:
public Dept selectOne(Integer id){ Dept dept = null; try { session = MybatisSessionFactory.getSession(); dept = session.selectOne("cn.ustb.entity.DeptMapper.selectDept",id); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally{ MybatisSessionFactory.closeSession(); } return dept;}查询操作(返回多条记录) 修改配置文件deptMapper.xml,添加<!-- 返回多条记录,返回结果配置的不是集合类型,而是集合元素的类型;参数也可以通过Map等方式封装 --> <!-- 多个查询 --><!-- 如果返回的是list,resultMap指定的值是list集合里面的类型 --><select id="selectMultiDept" parameterType="String" resultMap="deptResultMap"> select * from dept where dept_address = #{deptAddress}</select><!-- 参数类型用map的多个查询 --><select id="selectMultiDeptUseMapParamter" parameterType="Map" resultMap="deptResultMap"> select * from dept where dept_address like #{deptAddress}</select>修改DeptDaoImpl.java,添加selectList方法:
public List<Dept> selectMulti(String deptAddress){ List<Dept> list = null; try { session = MybatisSessionFactory.getSession(); list = session.selectList("cn.ustb.entity.DeptMapper.selectMultiDept", deptAddress); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } return list; } /* * 模糊查询在sql中添加like,在传入条件中添加% */ public List<Dept> selectMultiUserMapParameter(Map deptAddresses){ List<Dept> list = null; try { session = MybatisSessionFactory.getSession(); list = session.selectList("cn.ustb.entity.DeptMapper.selectMultiDeptUseMapParamter", deptAddresses); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); } return list; }测试类代码:
@Test public void testSelectList() { Map map=new HashMap(); map.put("deptName", "%研%"); List<Dept> depts=deptDaoImpl.selectList(map); for(Dept dept:depts){ System.out.println("dept:"+dept); }}新闻热点
疑难解答