一、建立表
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">
, /**
** @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
*
* @Title: findAuthors* @Description: TODO(依據多個ID */
public List; findAuthors(List&*/
public List; findAus(List&*/
3.2、更改Mapper.xml
@Test public void testFindAuthors() { //取得會話
Sqlsession sqlSession=sqlSessionFactory.openSession(); authorMapper=sqlSession.getMapper(AuthorMapper.class);
List
list.add(1);
list.add(3);
list.add(3);
list.add(7); //呼叫方式
List
System.out.println(authors); //關閉會話
四、使用Map做為參數
4.1、在Mapper介面中增加對應方法
/**
** @Title: findAuthorsByMap
* @Description: TODO(使用Map. */
public List
4.3、測試
@T 4.3、測試
SqlSession sqlSession=sqlSessionFactory.openSession(); //Mapper介面
遠 Map
map.put("bio", "哈"); List
}
}使用多個參數
5.1、Mapper介面
/**
*
* @Title: findAuthorsByParams
* @Description: TODO(使用多個參數
* @param id
* @param username
* @return List
WHERE author_id=#{0}
OR author_username LIKE "%"{114}" %"
5.3、測試@Test public void testFindAuthorsByParams() { //取得會話
//Mapper介面
AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class); //呼叫方式
List
System.out.println(authors); ession.close(); for(Author a:authors){
System.out.println) .toString());
}
}
. public List
6.2、Mapper.xml
WHERE author_id=#{id}
or author_username LIKE "%"#{username}"%"以上就是MyBatis入門(三)---多個參數的內容,更多相關內容請關注PHP中文網(www.php.cn )!