输入参数可以有三种:简单类型,POJO,包装类

关于前两种:

http://www.cnblogs.com/xuyiqing/p/8600888.html

这里写一下传递包装类参数:

一个POJO:User

package pojo;

import java.io.Serializable;
import java.util.Date; public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String sex;
private Date birthday;
private String address; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
} }

POJO的包装类QueryVo:

package pojo;

import java.io.Serializable;

public class QueryVo implements Serializable {

    private static final long serialVersionUID = 1L;

    private User user;

    public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }

User.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="mapper.UserMapper">
<!-- 根据用户名模糊查询 -->
<select id="findUserByQueryVo" parameterType="pojo.QueryVo"
resultType="pojo.User">
select * from user where username like "%"#{user.username}"%"
</select>
</mapper>

采用Mapper动态代理开发方式测试

UserMapper接口:

package mapper;

import java.util.List;

import pojo.QueryVo;
import pojo.User; public interface UserMapper { public List<User> findUserByQueryVo(QueryVo vo); }

测试类:

package junit;

import java.io.InputStream;
import java.util.List; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import mapper.UserMapper;
import pojo.QueryVo;
import pojo.User; public class MybatisMapperTest { @Test
public void testMapper() throws Exception { String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryVo vo = new QueryVo();
User user = new User();
user.setUsername("王");
vo.setUser(user);
List<User> users = userMapper.findUserByQueryVo(vo);
for (User u : users) {
System.out.println(u);
} } }

核心配置文件:sqlMapConfig.xml:

<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="12345" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>

输出参数:

1.输出简单类型参数:

User.xml:

    <!-- 查询总条数 -->
<select id="countUser" resultType="Integer">
select COUNT(*) from user
</select>

UserMapper:

    public Integer countUser();

测试类:

    @Test
public void testCount() throws Exception { String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Integer i = userMapper.countUser();
System.out.println(i);
}

2.输出POJO对象

3.输出POJO列表

这里都有写过:

http://www.cnblogs.com/xuyiqing/p/8601506.html

另外,这里输出类型都是resultType:这个前提是表的字段名和POJO的属性名一致

如果不一致,其实也有解决方法:需要使用resultMap,不过通常不会用,尽量保持一致即可

Mybatis框架四:输入参数、输出参数的更多相关文章

  1. JavaWeb_(Mybatis框架)输入和输出参数_五

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

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

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

  3. Hibernate调用带有输入参数,输出参数为cursor的存储过程

    一.Oracle创建表及存储过程 1.创建表T_MONITOR_DEVICE 创建后的表结构 2.创建存储过程 create or replace procedure ProcTestNew(v_mo ...

  4. BackgroundWorker 的输入、输出参数、进度条与文字刷新、取消机制、返回事件

    1. 定义全局变量 BackgroundWorker backgroundwoker; 2. 点击开始按钮或其它时机初始化 backgroundwoker = new BackgroundWorker ...

  5. C# - 企业框架下的存储过程输出参数

    output 输出参数 在C# 中的获取方法 新建存储过程 create proc Test @ID int, @maxnum int output as begin declare @num int ...

  6. matlab Tricks(二十七)—— 可变输入参数输出参数的适配

    matlab 内置的对 varargin/varargout(nargin/nargout)的支持,使得 matlab 的输入参数和输出参数,有了更为灵活的传递和使用: 比如对于 matlab 原生支 ...

  7. MyBatis - 输入和输出参数

    基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回值是一个对象的集合,@resultType中只能写该对 ...

  8. mybatis学习记录四——输入、输出映射

    6      输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 6.1.1     需求 完成用户信息的综合查询,需要传入查询条件很 ...

  9. MyBatis框架使用 —— 传递多个参数的方式

    引言 目前,MyBatis的使用越来越普遍,也有一些公司使用Hibernate.使用MyBatis需要我们自己书写SQL语句,面对各种复杂的场景,SQL传递多参是很普遍的.如何传递多参应对不同的场景也 ...

随机推荐

  1. matplotlib 初次编译无法运行

    终端 解决方案:vim ~/.matplotlib/matplotlibrc 输入backend: TkAgg 保存

  2. 腾讯开源项目phxpaxos的编译步骤

    #paxos的一般编译流程在项目文档<中文详细编译手册>里面已经有介绍,这里重点介绍一下编译samples目录下的代码: #我的环境是ubuntu; #设置paxos根目录 phx_dir ...

  3. English Conversations You Can Download for Free (Spoken English MP3/Audio Files)

    If you want to download free English conversations, you’ve come to the right place. This page introd ...

  4. idea在debugger模式下无法启动,但是在run模式下可以启动的问题

    debugger模式下,启动idea,总是报内存溢出异常, Error creating bean with name 'sysRoleUserMapper' defined in URL [jar: ...

  5. django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: ...

    出现此问题的解决方法: 在mysql中创建一个和settings.py里设置的mysql  'name'名字一样的数据库就可以了.

  6. pytest框架之fixture详细使用

    本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从p ...

  7. 关于python-flask中规范创建项目的几个关键py项目文件

    1.config.py——配置文件 DEBUG = True DIALECT = 'mysql' DRIVER = 'mysqldb' USERNAME = 'root' PASSWORD = '' ...

  8. 适用于Mac 的自动补丁管理软件

    适用于Mac 的自动补丁管理软件 ManageEngine Desktop Central 的功能越来越神奇.系统管理员现在可以使用 Desktop Central 管理异构网络.即使是最复杂的任务, ...

  9. vue 图片下载到本地,图片保存到本地

    必须同源(访问的网站域名与服务器域名一致)才能下载 downs() { var alink = document.createElement("a"); alink.href = ...

  10. vue中引入vux

    1.安装相关依赖 cnpm install vux --save cnpm install vux-loader --save-dev cnpm install less less-loader -- ...