首页 Java java教程 MyBatis入门(三)---多个参数

MyBatis入门(三)---多个参数

Dec 21, 2016 pm 02:27 PM

一、建立表

1.1、建立表,并插入数据

 

/*SQLyog EnterPRise v12.09 (64 bit)MySQL - 5.6.27-log : Database - 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`;/*Table structure for table `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 COMMENT '注册时间',  PRIMARY KEY (`author_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;/*Data for the table `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、创建项目

41.png

2.2、创建POJO类

 

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

* @Title: Author.java

* @Package com.pb.mybatis.po

* @ClassName Author

* @Description: TODO(Blog作者类)

* @author 刘楠

* @date 2015-10-29 上午9:27:53

* @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 the authorId     */
   public int getAuthorId() {        return authorId;
   }    /**
    * @param authorId the authorId to set     */
   public void setAuthorId(int authorId) {        this.authorId = authorId;
   }    /**
    * @return the authorUserName     */
   public String getAuthorUserName() {        return authorUserName;
   }    /**
    * @param authorUserName the authorUserName to set     */
   public void setAuthorUserName(String authorUserName) {        this.authorUserName = authorUserName;
   }    /**
    * @return the authorPassword     */
   public String getAuthorPassword() {        return authorPassword;
   }    /**
    * @param authorPassword the authorPassword to set     */
   public void setAuthorPassword(String authorPassword) {        this.authorPassword = authorPassword;
   }    /**
    * @return the authorEmail     */
   public String getAuthorEmail() {        return authorEmail;
   }    /**
    * @param authorEmail the authorEmail to set     */
   public void setAuthorEmail(String authorEmail) {        this.authorEmail = authorEmail;
   }    /**
    * @return the authorBio     */
   public int getAuthorBio() {        return authorBio;
   }    /**
    * @param authorBio the authorBio to set     */
   public void setAuthorBio(int authorBio) {        this.authorBio = authorBio;
   }    /**
    * @return the registerTime     */
   public Date getRegisterTime() {        return registerTime;
   }    /**
    * @param registerTime the registerTime to set     */
   public void setRegisterTime(Date registerTime) {        this.registerTime = registerTime;
   }    /** (non Javadoc)
   
    *

Title: toString


   
    *

Description:重写toString方法


   
    * @return
   
    * @see java.lang.Object#toString()     */
   @Override    public String toString() {        return "Author [authorId=" + authorId + ", authorUserName="
               + authorUserName + ", authorPassword=" + authorPassword                + ", authorEmail=" + authorEmail + ", authorBio=" + authorBio                + ", registerTime=" + registerTime + "]";
   }

   
   
   
}

 

 

 

2.3、创建configruation

 


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(根据查找一个用户)
   
    * @param id
    * @return Author     */
   public Author findAuthorById(int authorId);

}

 

 

 

2.4、创建mapper.xml

 

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(根据多个ID进行查找)
   
    * @param idLists
    * @return List     */
   public List findAuthors(List idLists);

 

3.2、更改Mapper.xml

 


 

3.3、测试

 

@Test    public void testFindAuthors() {        //获取会话
       Sqlsession sqlSession=sqlSessionFactory.openSession();        //Mapper接口
       AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);
       List list=new ArrayList();
       
       list.add(1);
       list.add(3);
       list.add(4);
       list.add(6);
       list.add(7);        //调用方法
       List authors=authorMapper.findAuthors(list);
       System.out.println(authors);        //关闭会话        sqlSession.close();
   }

 

 

四、使用Map做为参数

4.1、在Mapper接口中增加相应方法

 

/**
    *
    * @Title: findAuthorsByMap
   
    * @Description: TODO(使用Map做为参数)
   
    * @param map
    * @return List     */
   public List findAuthorsByMap(Map map);

 

4.2、更改Mapper.xml

 


 

4.3、测试

 

@Test    public void testFindAuthorsByMap() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);
               Map map=new HashMap();
               map.put("username", "张");
               map.put("bio", "哈");                
               //调用方法
               List authors=authorMapper.findAuthorsByMap(map);
               System.out.println(authors);                //关闭会话                sqlSession.close();                for(Author a:authors){
                   System.out.println(a.toString());
               }
   }

 

 

 

五、直接使用多个参数

5.1、Mapper接口

 

/**
    *
    * @Title: findAuthorsByParams
   
    * @Description: TODO(使用多个参数
   
    * @param id
    * @param username
    * @return List     */
   public List findAuthorsByParams(int authorId,String authorUserName);

 

5.2、Mapper.xml

 


 

5.3、测试

 

@Test    public void testFindAuthorsByParams() {        //获取会话
               SqlSession sqlSession=sqlSessionFactory.openSession();                //Mapper接口
               AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);                
               
               //调用方法
               List authors=authorMapper.findAuthorsByParams(6,"张");
               System.out.println(authors);                //关闭会话                sqlSession.close();                for(Author a:authors){
                   System.out.println(a.toString());
               }
   }

 

 

六、直接使用多个参数注解写法

6.1、Mapper接口

 

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

 

 

 

6.2、Mapper.xml


 以上就是MyBatis入门(三)---多个参数的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

iBatis和MyBatis:哪个更适合你? iBatis和MyBatis:哪个更适合你? Feb 19, 2024 pm 04:38 PM

iBatis与MyBatis:你应该选择哪个?简介:随着Java语言的快速发展,许多持久化框架也应运而生。iBatis和MyBatis是两个备受欢迎的持久化框架,它们都提供了一种简单而高效的数据访问解决方案。本文将介绍iBatis和MyBatis的特点和优势,并给出一些具体的代码示例,帮助你选择合适的框架。iBatis简介:iBatis是一个开源的持久化框架

详解MyBatis动态SQL标签中的Set标签功能 详解MyBatis动态SQL标签中的Set标签功能 Feb 26, 2024 pm 07:48 PM

MyBatis动态SQL标签解读:Set标签用法详解MyBatis是一个优秀的持久层框架,它提供了丰富的动态SQL标签,可以灵活地构建数据库操作语句。其中,Set标签是用于生成UPDATE语句中SET子句的标签,在更新操作中非常常用。本文将详细解读MyBatis中Set标签的用法,以及通过具体的代码示例来演示其功能。什么是Set标签Set标签用于MyBati

实现MyBatis中批量删除操作的多种方式 实现MyBatis中批量删除操作的多种方式 Feb 19, 2024 pm 07:31 PM

MyBatis中实现批量删除语句的几种方式,需要具体代码示例近年来,由于数据量的不断增加,批量操作成为了数据库操作的一个重要环节之一。在实际开发中,我们经常需要批量删除数据库中的记录。本文将重点介绍在MyBatis中实现批量删除语句的几种方式,并提供相应的代码示例。使用foreach标签实现批量删除MyBatis提供了foreach标签,可以方便地遍历一个集

对比分析JPA和MyBatis的功能和性能 对比分析JPA和MyBatis的功能和性能 Feb 19, 2024 pm 05:43 PM

JPA和MyBatis:功能与性能对比分析引言:在Java开发中,持久化框架扮演着非常重要的角色。常见的持久化框架包括JPA(JavaPersistenceAPI)和MyBatis。本文将对这两个框架的功能和性能进行对比分析,并提供具体的代码示例。一、功能对比:JPA:JPA是JavaEE的一部分,提供了一种面向对象的数据持久化解决方案。它通过注解或X

MyBatis批量删除语句的使用方法详解 MyBatis批量删除语句的使用方法详解 Feb 20, 2024 am 08:31 AM

MyBatis批量删除语句的使用方法详解,需要具体代码示例引言:MyBatis是一款优秀的持久层框架,提供了丰富的SQL操作功能。在实际项目开发中,经常会遇到需要批量删除数据的情况。本文将详细介绍MyBatis批量删除语句的使用方法,并附上具体的代码示例。使用场景:在数据库中删除大量数据时,逐条执行删除语句效率低下。此时,可以使用MyBatis的批量删除功能

MyBatis 一级缓存详解:如何提升数据访问效率? MyBatis 一级缓存详解:如何提升数据访问效率? Feb 23, 2024 pm 08:13 PM

MyBatis一级缓存详解:如何提升数据访问效率?在开发过程中,高效的数据访问一直是程序员们关注的焦点之一。而对于MyBatis这样的持久层框架而言,缓存是提升数据访问效率的关键方法之一。MyBatis提供了一级缓存和二级缓存两种缓存机制,其中一级缓存是默认开启的。本文将详细介绍MyBatis一级缓存的机制,并提供具体的代码示例,帮助读者更好地理

解析MyBatis的缓存机制:比较一级缓存和二级缓存的特点和用法 解析MyBatis的缓存机制:比较一级缓存和二级缓存的特点和用法 Feb 25, 2024 pm 12:30 PM

MyBatis的缓存机制解析:一级缓存与二级缓存的区别与应用在MyBatis框架中,缓存是一个非常重要的特性,可以有效提升数据库操作的性能。其中,一级缓存和二级缓存是MyBatis中常用的两种缓存机制。本文将详细解析一级缓存与二级缓存的区别与应用,并提供具体的代码示例进行说明。一、一级缓存一级缓存也被称为本地缓存,它默认开启且不可关闭。一级缓存是SqlSes

MyBatis Generator配置参数解读及最佳实践 MyBatis Generator配置参数解读及最佳实践 Feb 23, 2024 am 09:51 AM

MyBatisGenerator是MyBatis官方提供的一个代码生成工具,可以帮助开发人员快速生成符合数据库表结构的JavaBean、Mapper接口以及XML映射文件。在使用MyBatisGenerator进行代码生成的过程中,配置参数的设置是至关重要的。本文将从配置参数的角度出发,深入探讨MyBatisGenerator的

See all articles