zoukankan      html  css  js  c++  java
  • 1.使用接口结合xml文件 2.解决字段与属性不匹配 3.链表查询 4.$和#的区别 5.添加对象时如何把生成的id返回

    2019-8-29 课程大纲

    1.使用接口结合xml文件

    2.解决字段与属性不匹配

    3.链表查询

    4.$#的区别

    5.添加对象时如何把生成的id返回

    1.使用接口结合xml文件

    1)创建dao包,在包下创建接口,该接口要和映射文件匹配

    2)映射文件中的namespace一定要和接口所在的包以及接口的名字一样

    3)标签中的id一定和接口中方法的名字对照

    4)测试时:

    //得到接口的实现类

    UserDao userDao=session.getMapper(UserDao.class);

    //调用方法

    User user=userDao.getUser(1);

    2.解决字段与属性不匹配

    way1sql语句中为字段起别名,别名与实体类名一致

    way2:使用resultMap标签来定义实体类与字段之间的对应关系

    <select id="getOrder" resultMap="myMap">

          select * from Orders where Order_id=#{id}   

    </select>

    <resultMap type="com.zhiyou.clg.bean.Order" id="myMap">

    <id column="order_id" property="id"/>

    <result column="order_no" property="no"/>

    <result column="order_price" property="price"/>

    </resultMap>

    3.链表查询

    3.1比如根据班级id查询班级信息(带老师的信息)一对一或多对一

    way1SQL语句中使用联表查询

    *实体类中需要添加属性(数据类型为实体类名)

    *映射文件中使用association标签(属性:property(实体类中添加的属性名)、javaType(添加的属性所在的实体类的全类名)

    <select id="getClazz" resultMap="myMap">

          select * from class c,teacher t where c.teacher_id=t.t_id and c_id=#{cid}   

    </select>

    <resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap">

    <id column="c_id" property="id"/>

    <result column="c_name" property="name"/>

    <result column="teacher_id" property="teacherid"/>

    <association property="teacher" javaType="com.zhiyou.clg.bean.Teacher">

    <id column="t_id" property="id"/>

    <result column="t_name" property="name"/>

    </association>

    </resultMap>

    way2:嵌套查询(查询的结果为其他查询的条件)

    *实体类中需要添加属性(数据类型为实体类名)

    *映射文件中使用association标签(属性:propertyjavaType(查询到的结果,此时作为查询条件)、 columnselect(另一映射的语句的id,即另一select标签的id))

    <select id="getClazz" resultMap="myMap2">

          select * from class where c_id=#{cid}   

    </select>

    <select id="getTeacher" parameterType="int" resultType="com.zhiyou.clg.bean.Teacher">

       select t_id id,t_name name from teacher where t_id=#{tid}   

    </select>

    <resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap2">

    <id column="c_id" property="id"/>

    <result column="c_name" property="name"/>

    <result column="teacher_id" property="teacherid"/>

    <association property="teacher"   javaType="com.zhiyou.clg.bean.Teacher"

    column="teacher_id" select="getTeacher">

    <id column="t_id" property="id"/>

    <result column="t_name" property="name"/>

    </association>

    </resultMap>  

    3.2比如根据班级id查询班级信息(带班里学生的信息)一对多

    way1SQL语句中使用联表查询

    *实体类中添加属性(数据类型为集合,泛型为实体类名)

    *映射文件中使用collection标签(属性:property(添加的属性名)、ofType(集合泛型的实体 类的全类名))

     <select id="getClazz" resultMap="myMap3">

    select *from class c,teacher t,student s where c.c_id=s.class_id and c.teacher_id=t.t_id and c.c_id=#{cid}  

    </select>

    <resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap3">

    <id column="c_id" property="id"/>

    <result column="c_name" property="name"/>

    <result column="teacher_id" property="teacherid"/>

    <association property="teacher" javaType="com.zhiyou.clg.bean.Teacher">

    <id column="t_id" property="id"/>

    <result column="t_name" property="name"/>

    </association>

    <collection property="students" ofType="com.zhiyou.clg.bean.Student">

    <id column="s_id" property="id"/>

    <result column="s_name" property="name"/>

    <result column="class_id" property="cid"/>

    </collection>

    </resultMap> -->

    way2:嵌套查询(查询的结果为其他查询的条件)

    *实体类中添加属性(数据类型为集合,泛型为实体类名)

    *映射文件中使用collection标签(属性:propertyofTypecolumnselect)

    <select id="getClazz" resultMap="myMap4">

          select * from class where c_id=#{cid}   

    </select>

    <select id="getTeacher" parameterType="int" resultType="com.zhiyou.clg.bean.Teacher">

       select t_id id,t_name name from teacher where t_id=#{tid}   

    </select>

    <select id="getStudent" parameterType="int" resultType="com.zhiyou.clg.bean.Student">

    select s_id id,s_name name,class_id cid from student where class_id=#{cid}

    </select>

    <resultMap type="com.zhiyou.clg.bean.Clazz" id="myMap4">

    <id column="c_id" property="id"/>

    <result column="c_name" property="name"/>

    <result column="teacher_id" property="teacherid"/>

    <association property="teacher"   javaType="com.zhiyou.clg.bean.Teacher"

    column="teacher_id" select="getTeacher">

    <id column="t_id" property="id"/>

    <result column="t_name" property="name"/>

    </association>

    <collection property="students" ofType="com.zhiyou.clg.bean.Student"

     column="c_id" select="getStudent">

    <id column="s_id" property="id"/>

    <result column="s_name" property="name"/>

    <result column="class_id" property="cid"/>

    </collection>

    </resultMap>

    4. $#的区别

    $: 解析时不会为内容添加”” 他是sql语句的拼接,存在sql注入的缺陷。

    当传入的为表结构时使用,当传入的为列名或表名时可以使用

    order by  group by 时用$

    $:一般用于传入数据库对象,例如列名或表名

    #: 解析时会为内容添加””,它的sql时采用占位符,防止sql注入。

    能用#不要用$

    #:一般传入的是值

    5. 添加对象时如何把生成的id返回

    <insert  id=”addUser” parameterType=”com.zhiyou.clg.bean.User” useGeneratedKeys="true" keyProperty="id">

    </insert>

    useGeneratedKeys="true"  :表示使用自动生成的id

    keyProperty="id"   :把生成的id赋值给对应的实体类属性

    以上两者联合使用

    一般可用于添加单号(自动生成)后即刻便加入商品

  • 相关阅读:
    bzoj1415 NOI2005聪聪和可可
    Tyvj1952 Easy
    poj2096 Collecting Bugs
    COGS 1489玩纸牌
    COGS1487 麻球繁衍
    cf 261B.Maxim and Restaurant
    cf 223B.Two Strings
    cf 609E.Minimum spanning tree for each edge
    cf 187B.AlgoRace
    cf 760B.Frodo and pillows
  • 原文地址:https://www.cnblogs.com/jingmochen/p/11432814.html
Copyright © 2011-2022 走看看