一、建立表
1.1、建立表,并插入数据
/*SQLyog EnterPRise v12.09 (64 Bit)MySQL - 5.6.27- log: Datenbank – mybatis
********************************************* **************************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/ ;/*!40014 SET @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`;/*Tabellenstruktur für Tabelle `author` */DROP TABLE IF EXISTS `author`;CREATE TABLE `author` (
`author_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '作者ID主键' ,
`author_username` varchar(30) NOT NULL COMMENT '作者用户名',
`author_passWord` varchar(32) NOT NULL COMMENT '作者密码',
`author_email` varchar(50) NOT NULL COMMENT '作者邮箱',
`author_bio` varchar(1000) DEFAULT '这家伙很赖,什么也没留下' COMMENT '作者简介',
`register_time` datetime DEFAULT CURRENT_TIMESTAMP MENT '注册时间', PRIMARY KEY (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;/*Daten für die Tabelle `author` */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_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
二、创建项目
2.1、创建项目
2.2、创建POJO类
package com.pb.mybatis.po;import java.util.Date;/**
*
* @Title: Author.java
* @Package com.pb.mybatis.po
* @ClassName Author
* @Beschreibung: TODO (Blog-Autorenklasse)
* @Autor Liu Nan
* @Datum 29.10.2015 9:27:53 Uhr
* @Version V1 .0*/public class Author { //作者ID
private int authorId;
//作者用户名
private String authorUserName;
//作者密码
private String authorPassword;
//作者邮箱
private String authorEmail;
//作者介绍
private int authorBio;
//注册时间
private Date registerTime; /**
* @return die Autoren-ID */
public int getAuthorId() { return authorId;
} /**
* @param authorId die festzulegende Autoren-ID */
public void setAuthorId(int authorId) { this.authorId = authorId; } /**
* @return the authorUserName */
public String getAuthorEmail() { return authorEmail;
} /**
* @param AuthorUserName der festzulegende AuthorUserName */
public void setAuthorEmail(String authorEmail ) { this.authorEmail = authorEmail;
} /**
* @return theauthorPassword */
public int getAuthorBio() { return authorBio;
} /**
* @param AuthorPassword das festzulegende AuthorPassword */
public void io (int authorBio) { this.authorBio = authorBio;
} /**
* @return the authorEmail */
public Date getRegisterTime() { return registerTime;
} /**
* @param authorE-Mail an den festzulegenden AutorE-Mail */
public void setRegisterTime(Date registerTime) { this.registerTime = registerTime;
} /**
* @return the authorBio */
@Override public String toString() { return "Author [authorId=" + authorId + ", authorUserName = "
+ Authorusername +", AuthorPassword = " + AuthorPassword +", Authoremail = " + Authoremail +", AuthorBio = " + AuthorBio +", RegisterTime = " + RegisterTime +";
}
}
2.3、创建Konfiguration
Konfiguration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
2.3、创建mapper接口
public interface AuthorMapper {
/**
*
* @Title: findById
* @Description: TODO (einen Benutzer basierend auf finden)
* @param id
* @return Author*/
public Author findAuthorById(int authorId);
}
2.4、创建mapper.xml
br/> PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
三、传入多个ID,进行查找使用List
3.1、更改Mapper接口
/**
*
* @Title: findAuthors
* @Description: TODO (Suche basierend auf mehreren IDs)
* @param idLists
* @return List
3.2、更改Mapper.xml
3.3、测试
@Test public void testFindAuthors() { //Sitzung abrufen
Sqlsession sqlSession=sqlSessionFactory.openSession(); //Mapper-Schnittstelle
AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);
List< ;Integer> list=new ArrayList
.add (6); Schließe die Sitzung sqlSession();
}
4. Map als Parameter verwenden
*
* @Title: findAuthorsByMap* @Description: TODO (Map als Parameter verwenden)
* @param map
* @return List
public List
4.2. Mapper.xml ändern
Test
@Test public void testFindAuthorsByMap() { //Get session
SqlSession sqlSession=sqlSessionFactory.openSession (); Map
*
* @Title: findAuthorsByParams
* @Description: TODO(unter Verwendung mehrerer Parameter
* @param id
* @param Benutzername
* @ return Liste
öffentliche Liste
5.2, Mapper.xml
5.3, Test
@Test public void testFindAuthorsByParams() { //Sitzung abrufen
SqlSession sqlSession=sqlSessionFactory.openSession(); //Mapper-Schnittstelle
AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class); passieren Autor a:authors){
System.out.println(a.toString());
}
}
🎜 >6. Direkte Verwendung mehrerer Parameteranmerkungen
6.1, Mapper-Schnittstelle
public List
6.2, Mapper.xml
< !--Verwenden Sie mehrere Parameter mithilfe von Anmerkungen --> ;
WHERE author_id=#{id}or author_username LIKE "%"#{username}"%"
Das Obige ist die Einführung in MyBatis (3) – der Inhalt mehrerer Parameter. Bitte achten Sie auf die chinesische PHP-Website (www.php.cn)!