zoukankan      html  css  js  c++  java
  • 4.接口与映射文件结合使用

    1.接口与映射文件

    1.接口中的方法名必须要和映射文件中的Id名字相同比如下面接口中方法名为“getUser”

     1 package com.zhiyou100.wc.dao;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.ibatis.annotations.Param;
     6 
     7 import com.zhiyou100.wc.bean.Users;
     8 
     9 public interface UsersDao {
    10     /**
    11      * 根据id查询
    12      * @param id
    13      * @return
    14      */
    15     public Users getUser(int id);
    16     /**
    17      * 添加
    18      * @param user
    19      */
    20     public void addUser(Users user );
    21     /**
    22      * 删除用户
    23      * @param id
    24      */
    25     public void deleteUser(int id);
    26     /**
    27      * 修改user
    28      * @param id
    29      */
    30     public void updateUser(Users user);
    31     /**
    32      * 查询年龄在10~30之间的用户
    33      * @param min
    34      * @param max
    35      * @return
    36      * @Param:表示告诉mybatis把该方法的参数封装成map时,键名叫什么
    37      */
    38     public List<Users> selectByAge(@Param("min") int min,@Param("max") int max);
    39 }

    2.映射文件UsersMapper.xml中的必须为: Id=“getUser”

    1 <!-- 根据id查询用户。id:标识该标签。
    2              parameterType:参数类型。可以写 也可以省略
    3              resultType:返回结果的类型。
    4             #{id}:类似于EL表达式。 解析id的值
    5             id:一定要和接口中的名字对照
    6      -->
    7     <select id="getUser" parameterType="int" resultType="com.zhiyou100.wc.bean.Users">
    8           select * from users where id=#{id}   
    9     </select>

    3.映射文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     4 <!-- namespace:表示名称空间。这里的namespace一定要和接口所在的包以及接口的名字一样. -->
     5 <mapper namespace="com.zhiyou100.wc.dao.UsersDao">
     6     <!-- 根据id查询用户。id:标识该标签。
     7              parameterType:参数类型。可以写 也可以省略
     8              resultType:返回结果的类型。
     9             #{id}:类似于EL表达式。 解析id的值
    10             id:一定要和接口中的名字对照
    11      -->
    12     <select id="getUser" parameterType="int" resultType="com.zhiyou100.wc.bean.Users">
    13           select * from users where id=#{id}   
    14     </select>
    15     
    16     <insert id="addUser" parameterType="com.zhiyou100.wc.bean.Users" >
    17         insert into users(name,age) values(#{name},#{age})
    18     </insert>
    19     
    20     <delete id="deleteUser" parameterType="int" >
    21         delete from users where id=#{id}
    22     </delete>
    23     <update id="updateUser" parameterType="com.zhiyou100.wc.bean.Users">
    24         update users set name=#{name},age=#{age} where id=#{id}
    25     </update>
    26     <!-- 查询年龄在10~30之间的用户。
    27         1.查询条件不在实体类中,参数类型封装到map中。#{参数}===map的键
    28         2.封装一个实体类。 min  max.
    29     如果在xml文件中出现了特殊字符?使用转译字符。
    30     <小于号     >大于号
    31     <![CDATA[文本内容]]>在标记CDATA下,所有的标记、实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待
    32      -->
    33     <select id="selectByAge" resultType="com.zhiyou100.wc.bean.Users" >
    34          <![CDATA[select * from users where age >=#{min} and age<=#{max}]]> 
    35         <!-- select * from users where age between #{min} and #{max} -->
    36         <!-- select * from users where age >=#{min} and age<=#{max} -->
    37     </select>
    38 
    39     <select id="selectAll" resultType="com.zhiyou100.wc.bean.Users" >
    40         select * from users
    41     </select>
    42 
    43 </mapper>
  • 相关阅读:
    egret 里面设置MovieClip的scale缩放值时,没有效果的情况
    游戏中的胜场数,净胜场数的计算
    使用Laya引擎和AS3(非原生AS)开发手游相关 总结常见bug
    javascript 一些注意事项
    JavaScript面向对象学习小结
    编写协议时注意事项
    Jenkins 在windows系统上的安装与使用
    LayaAir2.0 自定义Mesh-画圆环
    LayaAir2.0 自定义Mesh-画扇形
    Cocos Creator 使用 protobuf
  • 原文地址:https://www.cnblogs.com/banzhuanlaowang/p/11455262.html
Copyright © 2011-2022 走看看