首頁 > Java > java教程 > 主體

MyBatis入門(三)---多個參數

黄舟
發布: 2016-12-21 14:27:10
原創
1127 人瀏覽過

一、建立表

1.1、建立表,並插入資料

 

/*SQLyog EnterPRise v12.09 (64 bit)MySQL - 5.6.27-log : 資料庫- mybatis
** ************************************************** **** ************//*!40101 設定名稱utf8 */;/*!40101 設定SQL_MODE=''*/;/*!40014 設定@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS , UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@ @SQL_NOTES, SQL_NOTES=0 */;CREATE DATABASE /*!32312 IF NOT EXISTS*/`mybatis` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `mybatis``/FAULT CHARACTER SET utf8 */;USE `mybatis`` 的表表格結構*/ DROP TABLE IF EXISTS `author`;CREATE TABLE `author` (
 `author_id` int(11) unsigned NOT NULL AUTO_INCRMENT COMMENT '作者ID主鍵',
 `authorusername`(使用者名稱',
 `author_passWord` varchar(32) NOT NULL COMMENT '作者密碼',
 `author_email` varchar(50) NOT NULL COMMENT '作者郵筒',
 `author_`biovar( ,也沒留下來' COMMENT '作者簡介',
 `register_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '註冊時間',  PRIMARY KEY (`author_id`)
) ENGINE=InnoDB AUTO_INCRMENT 10/U5;的資料` */insert  into `author`(`author_id`,`author_username`,`author_password`,`author_email`,`author_bio`,`register_time`)
values (1,'張三','123456',' 123@ QQ.com','張三是新手,剛開始註冊','2015-10-29 10:23:59'),(2,'李四','123asf','lisi@163.com ', '魂牽夢融合','2015-10-29 10:24:29'),(3,'王五','dfsd342','ww@sina.com','康熙王朝','2015 -10-29 10 :25:23'),(4,'趙六','123098sdfa','zhaoliu@qq.com','花午骨','2015-10-29 10:26:09' ),(5, '錢七','zxasqw','qianqi@qq.com','這傢伙很賴,什麼也沒剩下','2015-10-29 10:27:04'),( 6,'張三豐' ,'123456','zhangsf@qq.com','這傢伙很賴,也沒留下什麼','2015-10-29 11:48:00'),(7,'金庸','qwertyuiop' ,'wuji@163.com','這傢伙很賴,什麼也沒留下','2015-10-29 11:48:24知道'),(8,'了',' 456789','456789 @qq.com','哈哈哈雅虎','2015-10-29 14:03:27'),(9,'不知道','1234567890','123456@qq.com' ,'哈哈哈哈雅虎','2015-10-29 14:01:16');/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHES * UNIQUE_CHECKS=@ OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

 

2.2、創建POJO類

package com.pb.mybatis.po;import java.util.Date;/**
*

* @Title: Author.java

* @Package com.pb.mybatis.po

* @ClassName Author
* 劉 🜎 * @date 2015-10-29 上午9:27:53

* @version V1.0*/public class Author {    //作者ID
   private intauthorId;
   //作者密碼
   private StringauthorPassword;    
   //作者信箱
   private StringauthorEmail;    
.
   private Date registerTime;    /**
    * @返回authorId    */
   public int getAuthorId() {        returnauthorIdId ;
   }    /**
    * @paramauthorId 要設定的authorId    */
   public void setAuthorId(intauthorId) {        this.authorId =authorId 
       this.authorId =authorId**
   }     returnauthorUserName;
   }    /**
    * @return 作者使用者名稱    */
   public void setAuthorUserName(StringauthorUserName) {        this.authorUserName =authorUserName;
   }   
   }    /**
    * @paramauthorUserName 要設定的authorUserName    */
   public void setAuthorPassword(StringauthorPassword) {        this.authorPassword =authorPassword;
   }    /**
    * @return 作者密碼    */
   public String getAuthorEaut(mailAuthor   public void setAuthorEmail(StringauthorEmail) {        this .authorEmail =authorEmail;
   }    /**
    * @paramauthorPassword 要設定的authorPassword    */
   public int getAuthorBio() {        returnauthorBio;
      this.authorBio =authorBio;
   } /**
    * @return 作者信箱    */
   public Date getRegisterTime() {        return registerTime;
   }    /**
    * @paramauthorEmail 要設定的authorEmail    */
register?
   }    /**
    * @return 作者簡介    */
   @Override    public String toString() {        return "作者[authorId=" +authorId + ",authorUserName ="
     + ",authorEmail=" +authorEmail + ",authorBio =" +authorBio                + ", registerTime=" + registerTime + "]";
   }

    

 

2.3、建立設定

 


br/>  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http:// mybatis.org /dtd/mybatis-3-config.dtd">









   
 
       
       
 
       
   

mappers>


lt;/mappers> gt;/configuration> gt; gt; gt; gt;


2.3

, /**

    *

    * @Title: findById

   

    * @Description: TODO(根據尋找一個使用者)

    parame p */

   public Author findAuthorById(intauthorId);

}
4.建立mapper.xml

 

公共“-//mybatis.org//DTD Mapper 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-mapper.dtd ">


三、刪除多個ID,進行查找使用List

3.1、更改Mapper介面

 

/**

    *

    * @Title: findAuthors

   

    * @Description: TODO(依據多個ID */

   public List; findAuthors(List&*/

   public List; findAus(List&*/

   
 

3.2、更改Mapper.xml

 

 

3.3、測試

 

@Test    public void testFindAuthors() {        //取得會話
       Sqlsession sqlSession=sqlSessionFactory.openSession(); authorMapper=sqlSession.getMapper(AuthorMapper.class);
       List list=new ArrayList();
       
       list.add(1);
       list.add(3);
         list.add(3);
          list.add(7);        //呼叫方式
       List authors=authorMapper.findAuthors(list);
       System.out.println(authors);       //關閉會話   

 

 

四、使用Map做為參數

4.1、在Mapper介面中增加對應方法

 

/**

    *

    * @Title: findAuthorsByMap

   

    * @Description: TODO(使用Map. */
   public List findAuthorsByMap(Map map); xml

 




 

4.3、測試

 

@T 4.3、測試

 
SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper介面
         遠           Map map=new HashMap();

               map.put( "username", "張");

               map.put("bio", "哈");                🠎              🠎         List authors=authorMapper.findAuthorsByMap(map);

               System.out.println(authors );                //關閉會話               sqlSession.close();             System.out.println(a.toString());

               }

   }

使用多個參數

5.1、Mapper介面

 

/**
    *
    * @Title: findAuthorsByParams
   
    * @Description: TODO(使用多個參數
   
    * @param id
    * @param username
    * @return List    */

   public List findAuthorsByParams(int authorId,String horUser

 

WHERE author_id=#{0}

OR author_username LIKE "%"{114}" %"

 

5.3、測試

 

@Test    public void testFindAuthorsByParams() {        //取得會話
               //Mapper介面
               AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);                    //呼叫方式
               List authors=authorMapper.findAuthorsByParams(6,"張");
               System.out.println(authors);       ession.close();                for(Author a:authors){
                   System.out.println) .toString());
               }
   }


 

 

. public List findAuthorsByParams(@Param("id") int authorId,@Param("username")String authorUserName);

 

 

 

6.2、Mapper.xml

 

6 findAuthorsByParams" resultMap="authorResultMap">SELECT * FROM author

WHERE author_id=#{id}

or author_username LIKE "%"#{username}"%"

 以上就是MyBatis入門(三)---多個參數的內容,更多相關內容請關注PHP中文網(www.php.cn )!




相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!