mybatis学习笔记1 下载本文

内容发布更新时间 : 2024/5/18 10:18:15星期一 下面是文章的全部内容请认真阅读。

parameterType=\> insert into students(id,name,sal) values(#{id},#{name},#{sal}) update students set name=#{name},sal=#{sal} where id=#{id} delete from students where id=#{id}

第十章分页查询

1)StudentDao.java /** *持久层 *@authorAdminTC */ publicclass StudentDao { /** *增加学生 */ publicvoid add(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(); } } /** *无条件分页查询学生 */ public List findAllWithFy(int start,int size) throws Exception{ SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ Map map = new LinkedHashMap(); map.put(\,start); map.put(\,size); return sqlSession.selectList(\,map); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{ sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } /** *有条件分页查询学生 */ public List findAllByNameWithFy(String name,int start,int size) throws Exception{ SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ Map map = new LinkedHashMap(); map.put(\,\+name+\); map.put(\,start); map.put(\,size); return sqlSession.selectList(\,map); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback();

throw e; }finally{ sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } publicstaticvoid main(String[] args) throws Exception{ StudentDao dao = new StudentDao(); System.out.println(\); for(Student s:dao.findAllByNameWithFy(\哈\,0,3)){ System.out.println(s.getId()+\+s.getName()+\+s.getSal()); } System.out.println(\); for(Student s:dao.findAllByNameWithFy(\哈\,3,3)){ System.out.println(s.getId()+\+s.getName()+\+s.getSal()); } System.out.println(\); for(Student s:dao.findAllByNameWithFy(\哈\,6,3)){ System.out.println(s.getId()+\+s.getName()+\+s.getSal()); } System.out.println(\); for(Student s:dao.findAllByNameWithFy(\哈\,9,3)){ System.out.println(s.getId()+\+s.getName()+\+s.getSal()); } } } 2)StudentMapper.xml insert into students(id,name,sal) values(#{id},#{name},#{sal})

思考:oracle分页如何做呢?

第十一章动态SQL操作之查询

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

StudentDao.java /** *持久层 *@authorAdminTC */ publicclass StudentDao { /** *动态SQL--查询 */ public List dynaSQLwithSelect(String name,Double sal) throws Exception{ SqlSession sqlSession = MyBatisUtil.getSqlSession(); try{ Map map = new LinkedHashMap(); map.put(\,name); map.put(\,sal); return sqlSession.selectList(\,map); }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{

sqlSession.commit(); MyBatisUtil.closeSqlSession(); } } publicstaticvoid main(String[] args) throws Exception{ StudentDao dao = new StudentDao(); List studentList1 = dao.dynaSQLwithSelect(\哈哈\,null); for(Student student : studentList1){ System.out.println(student.getId()+\+student.getName()+\:\+student.getSal()); } System.out.println(\); List studentList2 = dao.dynaSQLwithSelect(null,7000D); for(Student student : studentList2){ System.out.println(student.getId()+\+student.getName()+\:\+student.getSal()); } System.out.println(\); List studentList3 = dao.dynaSQLwithSelect(\哈哈\,7000D); for(Student student : studentList3){ System.out.println(student.getId()+\+student.getName()+\:\+student.getSal()); } System.out.println(\); List studentList4 = dao.dynaSQLwithSelect(null,null); for(Student student : studentList4){ System.out.println(student.getId()+\+student.getName()+\:\+student.getSal()); } } System.out.println(\); } StudentMapper.xml