您的位置:首页 > 博客中心 > 数据库 >

ibatis学习(5)sqlMap注意事项

时间:2022-03-13 23:37

 

1.sqlMap的整体理解:配置设置之后,就是建立了一个sqlMAP(ID,SQL),可以向其中输入参数,也可与返回数据。  

<!--select标签:设置SQL的ID,并给SQL设置返回值 -->

   <select id="selectAllStudent"resultClass="Student">

       select * from student

   </select>


<!--typeAlias标签:给类名取了一个别名 -->

<typeAlias alias="student"type ="Student" />

2.理解SQL语句中的参数书写方式

   1)理解#id#的意思:

      #id#   就相当与占位符,就像JDBC里面的“?”。

      parameterClass="int"  就相当于设置“?”的类型。 

          对于一般基本类型:

 <select id="selectStudentByID"  parameterClass="int"  resultClass="Student">

       select * from student

       where id=#id#

   </select>

         对于引用类型:

     当读到#id#的时候,就会调用到参数中的get方法,而调用哪个get是看# #里面的内容。

     #Id#   就是调用getID的方法。所以,values中内容顺序是不能换的。

<insert id="insertStudent"   parameterClass="Student">

       insert into Student(id,name,brith)

       values(#id#,#name#,#brith#)

   </insert>

    2)select语句中模糊查询   

          where 从句中 LIKE "%$name$%" $name$ 会取到name的值

 

3.序列生成主键

       <selectKey>  </selectKey>中先查询到ID,在插入。

       resultClass="int"就是指得到的返回值。

       keyPropery="id"   是指Student中的id,而不是数据库中的id,代表赋值给Student id

<insert id="insertStudentBySequence"   parameterClass="Student">

       <selectKey  resultClass="int"keyPropery="id">

       select xuelie.nextVal  from dual

       </selectKey>

       insert into Student(id,name,brith)

       values(#id#,#name#,#brith#)

   </insert><span style="color:black;">  </span>

4.注意点: 

    在写实体类的时候,要知道SQLMAP运行过程中,会动态调用实体类的相关方法和属性,构建时必须提供无参构造函数。否则会出错误。

 

5.优点:把SQL语句和Java代码分离

   缺点:1.SQL语句需要自己写

               2.参数使用不方便,需要把所有的参数都打包成一个对象,才能接收。  

 

  

 

本类排行

今日推荐

热门手游