网站后台会员管理,南宫做网站,企业做网站电话约见客户的对话,深圳市建设局工程交易中心网站目录
一、参数两种类型#xff1a;
二、传参的几种方法#xff1a;
三、提交事务
四、sql多表关联查询(两种方法) 一、参数两种类型#xff1a;
1.#{参数}#xff1a;预编译方式#xff0c;更安全#xff0c;只用于向sql中传值#xff1b;
select * from admin w…目录
一、参数两种类型
二、传参的几种方法
三、提交事务
四、sql多表关联查询(两种方法) 一、参数两种类型
1.#{参数}预编译方式更安全只用于向sql中传值
select * from admin where account#{account} and password#{password}
2.${参数}将参数直接拼接到sql中 主要用来动态的向sql中传列名。
select * from admin order by ${colum}
二、传参的几种方法
1.基本类型不需要做任何处理
2.多个参数使用Param(引用名)绑定 3.传多个参数使用类传参使用属性名 4.非基本类型需要Param(colum) 绑定。
三、提交事务
由于mybatis默认是关闭事务自动提交的 1.手动提交
如果进行了数据库的insert、update、delete操作修改了数据库必须必须调用sqlSession.commit()方法手动提交。
2.自动提交
将openSession方法中参数值改为true——sessionFactory.openSession(true);
四、sql多表关联查询(两种方法)
有student学生信息表、dorm宿舍信息表、admin管理员信息表现已查询每个宿舍信息以及每个宿舍所住的学生为例
相关类 表信息
admin表 dorm表 student表 1.sql查询语句已关联表 将需要的数据统一用一条sql语句关联表查询再通过反射存储到相应的类中由于有一对多的查询一个宿舍有多个学生结果需要使用collection标签。 resultMap iddormMap typeDormid columnid propertyid/idresult columnnum propertydormNum/resultassociation propertyadmin javaTypeAdminresult columnaccount propertyaccount/result/associationcollection propertystudents javaTypelist ofTypeStudentresult columnsnum propertynum/resultresult columnname propertyname/result/collection/resultMapselect idfindDormById resultTypecom.ffyc.mybatispro.model.Dorm resultMapdormMapSELECTd.id,d.num,s.num snum,s.name,a.accountFROMdorm dLEFT JOIN admin aON d.adminid a.idLEFT JOIN student sON s.dormid d.idWHERE d.id #{id}/selectselect idfindDorms resultMapdormMapSELECTd.id,d.num,s.num snum,s.name,a.accountFROMdorm dLEFT JOIN admin aON d.adminid a.idLEFT JOIN student sON s.dormid d.id/select
2.分步查询
多表关联查询可以分解为多个sql语句逐次查询出需要数据每次查出的结果可以作为下次查询的条件。 resultMap iddormMap1 typeDormid columnid propertyid/idresult columnnum propertydormNum/resultassociation propertyadmin javaTypeAdmin columnadminid selectfindAdminByid1/associationcollection propertystudents javaTypelist ofTypestudent columnid selectfindStudentByDormid/collection/resultMapselect idfindDorms1 resultMapdormMap1select id,num,adminid from dorm/selectselect idfindAdminByid1 resultTypeAdminselect account from admin where id #{adminid}/selectselect idfindStudentByDormid resultTypeStudentselect name,num from student where dormid#{id}/select