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>
  • 相关阅读:
    Lucene.Net 2.3.1开发介绍 —— 二、分词(一)
    控制‘控制台应用程序’的关闭操作
    详解for循环(各种用法)
    敏捷软件开发
    Sql Server的一些知识点
    在SharePoint 2010 中配置Remote Blob Storage FILESTREAM Provider
    使用LotusScript操作Lotus Notes RTF域
    JOpt Simple 4.5 发布,命令行解析器
    John the Ripper 1.8.0 发布,密码破解工具
    PacketFence ZEN 4.0.1 发布,网络接入控制
  • 原文地址:https://www.cnblogs.com/banzhuanlaowang/p/11455262.html
Copyright © 2011-2022 走看看