欢迎来到天天文库
浏览记录
ID:37914405
大小:134.50 KB
页数:9页
时间:2019-06-02
《ibatis学习笔记》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、ibatis学习笔记1、搭建环境:(1)、创建ibatis_test项目。(2)、添加SQL2000驱动包msbase.jar、mssqlserver.jar、msutil.jar和ibatis-2.3.3.720.jar包。2、配置文件:(1)、JDBC连接属性文件:在src目录下添加SqlMap.properties属性文件,主要包括JDBC连接的主要信息,driver=,url=,username=,password=,也可以见JDBC连接的信息直接写到总配置文件中。(2)、配置每个实体的映射文件(map文件):如Student.xml文件,其主要是包括对实体进行CURD操作的SQL语
2、句的映射。(3)、添加总配置文件:(参见SqlMapConfig.xml文件)其主要功能是导入SqlMap.properties和Student.xml文件,进行统一管理3、创建相应的类:(1)、建立实体类Student.java类。(2)、建立管理类接口StudentDao,和实现类StudentDaoImpl4、测试CRUD操作:(1)、在StudentDaoImpl类中添加读取配置(2)、测试查询所有信息的方法queryAllStudent(),在Student.xml文件中配置SQL语句映射信息。例如:<“--查找所有用户-->3、"resultClass="Student">select*fromt_student;在queryAllStudent()方法中使用sqlMapClient的queryForList()方法进行调用:publicListqueryAllStudent(){ListstudntList=null;try{studntList=sqlMapClient.queryForList("selectAllStudents");}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printSt4、ackTrace();}returnstudntList;}4、ibatis使用HashMap传递SQL多个参数:虽然ibataisqlmap可以配置多个参数,但sqlMap只能传入一个参数,我们有两种方式,一是把我们的参数封装成一个类,通过set/get取值的方式给sqlmap注入参数,二是通过hashMap(可以组合一些不是同一个pojo的参数有优势):范例:<“--使用Map解决多参数问题,多条件的查询-->selec5、tsid,sname,major,birth,scorefromt_studentwheresnamelike'%$sname$%'andscore=#score#注意:Map中的值key和类型要和SQL语句的的参数名称和类型一样。在如:<“[CDATA[ select*fromtestwherenamelike'%$name$%']]> Mapmap=newHashMap(); map6、.put("name","gaoxiang"); key为参数名,value位数据 Listlist=sqlMap.queryForList("getPeopleList",map);5、分页查询,使用page-paglib标签,提供每页分几行pagesize,和排除的记录结束位置pager.offset参数进行查询。建立PagerModel模型类如:<“--分页查询-->selecttop$pagesize$sid,sname,maj7、or,birth,scorefromt_studentwheresidnotin(selecttop$end$sidfromt_studentorderbysid)orderbysid6、存储过程的使用:(1)、在SQLSERVER创建存储过程:createprocedurepro_delete@sidint,--学生编号@msgnvarchar(100)output--返回信息as--定义
3、"resultClass="Student">select*fromt_student;在queryAllStudent()方法中使用sqlMapClient的queryForList()方法进行调用:publicListqueryAllStudent(){ListstudntList=null;try{studntList=sqlMapClient.queryForList("selectAllStudents");}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printSt
4、ackTrace();}returnstudntList;}4、ibatis使用HashMap传递SQL多个参数:虽然ibataisqlmap可以配置多个参数,但sqlMap只能传入一个参数,我们有两种方式,一是把我们的参数封装成一个类,通过set/get取值的方式给sqlmap注入参数,二是通过hashMap(可以组合一些不是同一个pojo的参数有优势):范例:<“--使用Map解决多参数问题,多条件的查询-->selec
5、tsid,sname,major,birth,scorefromt_studentwheresnamelike'%$sname$%'andscore=#score#注意:Map中的值key和类型要和SQL语句的的参数名称和类型一样。在如:<“[CDATA[ select*fromtestwherenamelike'%$name$%']]> Mapmap=newHashMap(); map
6、.put("name","gaoxiang"); key为参数名,value位数据 Listlist=sqlMap.queryForList("getPeopleList",map);5、分页查询,使用page-paglib标签,提供每页分几行pagesize,和排除的记录结束位置pager.offset参数进行查询。建立PagerModel模型类如:<“--分页查询-->selecttop$pagesize$sid,sname,maj
7、or,birth,scorefromt_studentwheresidnotin(selecttop$end$sidfromt_studentorderbysid)orderbysid6、存储过程的使用:(1)、在SQLSERVER创建存储过程:createprocedurepro_delete@sidint,--学生编号@msgnvarchar(100)output--返回信息as--定义
此文档下载收益归作者所有