一、建立表

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,'张三','','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,'张三丰','','zhangsf@qq.com','这家伙很赖,什么也没留下','2015-10-29 11:48:00'),(7,'金庸','qwertyuiop','wuji@163.com','这家伙很赖,什么也没留下','2015-10-29 11:48:24'),(8,'知道了','','456789@qq.com','哈哈哈哈哈雅虎','2015-10-29 14:03:27'),(9,'不知道','','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 * @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) * <p>Title: toString</p> * <p>Description:重写toString方法 </p> * @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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<properties resource="db.properties" />
<typeAliases>
<!--使用默认别名 -->
<package name="com.pb.mybatis.po"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射 --> <package name="com.pb.mybatis.mapper"/>
</mappers>
</configuration>

2.3、创建mapper接口

public interface AuthorMapper {

    /**
*
* @Title: findById * @Description: TODO(根据查找一个用户) * @param id
* @return Author
*/
public Author findAuthorById(int authorId); }

2.4、创建mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pb.mybatis.mapper.AuthorMapper">
<!--使用resultMap映射 type使用别名,-->
<resultMap type="Author" id="authorResultMap">
<!--主键 -->
<id property="authorId" column="author_id"/>
<!--普通属性与表中的字段对应 -->
<result property="authorUserName" column="author_username"/>
<result property="authorPassword" column="author_password"/>
<result property="authorEmail" column="author_email"/>
<result property="authorBio" column="author_bio"/>
<result property="registerTime" column="register_time"/>
</resultMap> <!--根据查找一个用户 -->
<select id="findAuthorById" parameterType="int" resultMap="authorResultMap">
SELECT * FROM author
WHERE author_id=#{authorId}
</select>
</mapper>

三、传入多个ID,进行查找使用List

3.1、更改Mapper接口

/**
*
* @Title: findAuthors * @Description: TODO(根据多个ID进行查找) * @param idLists
* @return List<Author>
*/
public List<Author> findAuthors(List<Integer> idLists);

3.2、更改Mapper.xml

<!--根据多个ID查找  -->
<select id="findAuthors" resultMap="authorResultMap">
SELECT * FROM author
WHERE author_id in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach> <!-- collection:传入参数的名称 index:索引: item:collection的别名 -->
</select>

3.3、测试

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

四、使用Map做为参数

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

/**
*
* @Title: findAuthorsByMap * @Description: TODO(使用Map做为参数) * @param map
* @return List<Author>
*/
public List<Author> findAuthorsByMap(Map<String, Object> map);

4.2、更改Mapper.xml

<!--使用Map查找  -->
<select id="findAuthorsByMap" resultMap="authorResultMap">
SELECT * FROM author
<!-- 参数使用Map的Key-->
WHERE author_username LIKE "%"#{username}"%"
or author_bio like"%"#{bio}"%"
</select>

4.3、测试

@Test
public void testFindAuthorsByMap() {
//获取会话
SqlSession sqlSession=sqlSessionFactory.openSession();
//Mapper接口
AuthorMapper authorMapper=sqlSession.getMapper(AuthorMapper.class);
Map<String, Object> map=new HashMap<String, Object>();
map.put("username", "张");
map.put("bio", "哈"); //调用方法
List<Author> 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<Author>
*/
public List<Author> findAuthorsByParams(int authorId,String authorUserName);

5.2、Mapper.xml

<!--直接使用多个参数  -->
<select id="findAuthorsByParams" resultMap="authorResultMap">
SELECT * FROM author
WHERE author_id=#{0}
OR author_username LIKE "%"#{1}"%"
<!-- 其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。 -->
</select>

5.3、测试

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

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

6.1、Mapper接口

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

6.2、Mapper.xml

<!--使用注解的方式使用多个参数  -->
<select id="findAuthorsByParams" resultMap="authorResultMap">
SELECT * FROM author
WHERE author_id=#{id}
or author_username LIKE "%"#{username}"%"
<!-- 使用注解的方式。,直接使用Param中的参数即可 -->
</select>

 

MyBatis入门(三)---多个参数的更多相关文章

  1. mybatis入门(三):mybatis的基础特性

    mybatis的知识点: 1.mybatis和hibernate本质区别和应用场景 hibernate:是一个标准的ORM框架(Ojbect relation mapper对象关系映射).入门门槛较高 ...

  2. <MyBatis>入门四 传入的参数处理

    1.单个参数 传入单个参数时,mapper文件中 #{}里可以写任意值 /** * 传入单个参数 */ Employee getEmpById(Integer id); <!--单个参数 #{} ...

  3. <MyBatis>入门三 sqlMapper文件

    增加 1.增删改在接口中的返回值 Integer.Long.Boolean.void 返回影响多少行 或 true | false 2.mapper 中 增删改没有返回值 (resultType或re ...

  4. Mybatis入门三

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...

  5. mybatis入门基础(三)----SqlMapConfig.xml全局配置文件解析

    一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) settings(全局配置参数) typeAiases(类型别名) typeHandlers(类型处理器 ...

  6. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  7. mybatis入门系列二之输入与输出参数

    mybatis入门系列二之详解输入与输出参数   基础知识   mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...

  8. MyBatis 入门到精通(三) 高级结果映射

    MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...

  9. MyBatis入门基础(一)

    一:对原生态JDBC问题的总结 新项目要使用mybatis作为持久层框架,由于本人之前一直使用的Hibernate,对mybatis的用法实在欠缺,最近几天计划把mybatis学习一哈,特将学习笔记记 ...

  10. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. mysql中连接失败2003错误解决办法

    在使用mysql数据库,新建连接时,会报2003-Can't connect to server on 'localhost'(10038)错误,原因主要是MYSQL服务没有启动起来,但是进入:计算机 ...

  2. HMM 自学教程(八)总结

    本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在HMM 学习最佳范例,这是针对国外网站上一个 HMM 教程的翻译,作者功底很深,翻译得很精彩,且在 ...

  3. Tools - Windows

    1)文本操作 Ctrl + C / Ctrl + V / Ctrl + X / Ctrl + Z / Ctrl + A:复制/粘贴/剪贴/撤销/全选. 2)窗口左右分屏 Win + 方向键:上(最大化 ...

  4. JS Date.Format

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  5. 开发(ASP.NET程序)把写代码写至最有面向对象味道

    前几天,搬房子时又拿起<重构----改善既有代码的设计>这本书来随便翻来看下,重构Refactoring在开发时,是时常也经常会使用得到. 她确实教我们怎样把写程序写简洁,清楚 好明白,好 ...

  6. 几种web字体格式

    目前,文字信息仍是网站最主要的内容,随着CSS3技术的不断成熟,Web字体逐渐成为话题,这项让未来Web更加丰富多彩的技术拥有多种实现方案,其中之一是通过@font-face属性在网页中嵌入自定义字体 ...

  7. SQL 日期转换(阳历转阴历)

    --步骤:创建日期表,放初始放初始化资料 --因为农历的日,是由天文学家推算出来,到现在只有到年,以后的有了还可以加入! if object_id('SolarData') is not nulldr ...

  8. 批量插入数据 C# SqlBulkCopy使用

    转自:http://blog.csdn.net/wangzh300/article/details/7382506 private static void DataTableToSQLServer( ...

  9. Windows下 C++ 实现匿名管道的读写操作

    由于刚弄C++没多久,部分还不熟练,最近又由于开发需求要求实现与其他程序进行通信,瞬间就感觉想到了匿名通信.于是自己查阅了一下资料,实现了一个可读可写的匿名管道: 源代码大部分都有注释: Pipe.h ...

  10. 泛函编程(17)-泛函状态-State In Action

    对OOP编程人员来说,泛函状态State是一种全新的数据类型.我们在上节做了些介绍,在这节我们讨论一下State类型的应用:用一个具体的例子来示范如何使用State类型.以下是这个例子的具体描述: 模 ...