mybatis学习笔记1

\>

第十二章动态SQL操作之更新

1) 更新条件不确定,需要根据情况产生SQL语法,这种情况叫动态SQL 2) 参见<<动态SQL—更新.JPG>>

StudentDao.java /** *持久层 *@authorAdminTC */ publicclass StudentDao { /** *动态SQL--更新 */ publicvoid dynaSQLwithUpdate(Student student) throws Exception{ ; SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ sqlSession.update(\,student) }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e;

} }finally{ sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } publicstaticvoid main(String[] args) throws Exception{ StudentDao dao = new StudentDao(); dao.dynaSQLwithUpdate(new Student(10,null,5000D)); dao.dynaSQLwithUpdate(new Student(10,\哈哈\,null)); dao.dynaSQLwithUpdate(new Student(10,\哈哈\,6000D)); } StudentMapper.xml update students name=#{name}, sal=#{sal}, where id=#{id}

第十三章动态SQL操作之删除

1) 根据ID删除学生

2) 参见<<动态SQL—删除.JPG>>

StudentDao.java /** *持久层

*@authorAdminTC */ publicclass StudentDao { /** } *动态SQL--删除 */ publicvoid dynaSQLwithDelete(int... ids) throws Exception{ SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ sqlSession.delete(\,ids); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{ sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } publicstaticvoid main(String[] args) throws Exception{ StudentDao dao = new StudentDao(); dao.dynaSQLwithDelete(1,3,5,7); } StudentMapper.xml delete from students where id in ${ids}

第十四章动态SQL操作之插入

1) 根据条件,插入一个学生

2)参见<<动态SQL—插入.JPG>> StudentDao.java /** *持久层 *@authorAdminTC */ publicclass StudentDao { /** *动态SQL--插入 */ publicvoid dynaSQLwithInsert(Student student) throws Exception{ SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ sqlSession.insert(\,student); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{ sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } publicstaticvoid main(String[] args) throws Exception{ StudentDao dao = new StudentDao(); } dao.dynaSQLwithInsert(new Student(1,\哈哈\,7000D)); dao.dynaSQLwithInsert(new Student(2,\哈哈\,null)); dao.dynaSQLwithInsert(new Student(3,null,7000D)); } StudentMapper.xml

id, name, sal, #{id}, #{name}, #{sal}, insert into students() values()

第十五章jsp/js/jquery/easyui/json + @springmvc + spring + mybatis + oracle开发

【员工管理系统--增加员工】

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4