关于mybatis标签属性说明

来源:互联网 发布:php开发mac应用 编辑:程序博客网 时间:2024/04/30 14:00



首先看代码  代码如下:

       <resultMap id="studentMap" type="StudentBean"><result property="id" column="id" /><result property="studentName" column="studentName" /><result property="sex" column="sex" /><result property="age" column="age" /></resultMap>


       <select id="findeStudentAndAddressById" <span style="background-color: rgb(255, 255, 51);"><span style="color:#FF0000;">resultType="StudentBean"</span></span>>select * from t_student s ,t_address a where s.addressID=a.id and s.id=#{id}</select>


        <select id="findeStudentAndAddressById"<span style="color:#FF0000;"> <span style="background-color: rgb(255, 255, 0);">resultMap="studentMap"</span></span>>select * from t_student s ,t_address a where s.addressID=a.id and s.id=#{id}</select>

以上三段代码,亲注意红色部分的区别:

       1:首先 resultType 和resultMap 都是表示这个方法的返回值类型  但他们也是有区别的,不能同时存在,也就是方法的返回类型不能既写resultType又写自定义的resultMap

       2:如果该方法的在查询数据库后返回的列名(也就是数据库的字段名)和你的实体bean里面的字段名是对应的(名字一模一样),那么你可以不用写自定义映射(resultMap其实就是自己写的自定义映射) 直接用resultType就可以,如第二段代码

       3:如果该方法在查询数据库后返回的列名和实体bean里面的字段名不对应,那么你就得必须写自定义映射resultMap 如代码段1;property表示实体bean的属性名,column表示查询数据库后的列名

      4:补充一点,在第一段代码中 type表示的是这个自定义映射中的对象全都是studentBean对象

      5:再补充一下:在写resultMap的时候,也可以只写实体bean中和查询出来的列名不同的属性,相同的可以不用写出来,当然,你写出来了也不会死人!!!

<span style="background-color: rgb(255, 255, 102);"><span style="color:#FF0000;">============================华丽的==================分割线=====================以上就是resultType和resultMap的区别==============================</span></span>


另外再总结一下常用的标签或者其属性;

         <insert id="saveUserBean"<span style="background-color: rgb(255, 255, 0);"> parameterType</span>="UserBean" useGeneratedKeys="true" keyProperty="u.id">insert into t_user (user_name,password,sex,salary) values (#{u.userName},#{u.password},#{u.sex},#{u.salary})</insert>
  parameterTpye:表示的是:在这个方法中,你传入的值得数据类型 :比如这个方式是添加一个学生,那么你在调用这个方法的时候,传入的是一个学生对象



另外写一点无聊的东西:不知道是我听错了呢还是产生了幻读:

有人说在写测试方法的时候,如果你不对数据库进行修改或者删除这种操作,那么你可以不用提交事务管理(也就是不用谢session。commit()),但是本人今晚测试了,比如在查询的时候,如果不写提交事务管理,那么你根本查询不到数据,全是null,必须写上,害的我用了一个晚上检查错误 ,我也是醉了


0 0
原创粉丝点击