码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis 输入映射和输出映射

时间:2015-08-04 19:27:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:mybatis

开发步骤:

1)映射文件UserMapper,xml中进行配置

2)接口UserMapper中增加方法

3)测试

输入映射:

需求一.:输入包装类型 UserQueryVo中定义Customer类 Customer继承User类

UserQueryVo

<span style="font-family:Courier New;font-size:14px;">public class UserQueryVo {
	private Customer customer;
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Customer getCustomer() {
		return customer;
	}
}
</span>

Customer类继承User类

<span style="font-family:Courier New;font-size:14px;">public class Customer extends User{

}</span>

UserMapper.xml中的配置

<span style="font-family:Courier New;font-size:14px;"><!-- 包装类型的 -->
	<select id="findAllUsersList" parameterType="cn.itcast.domain.UserQueryVo" resultType="cn.itcast.domain.Customer">
		select * from user  where user.sex=#{customer.sex} and user.username like "%${customer.username}%"
		
		
	</select></span>
这里的输入类型是包装类型 输出的是Customer类型

UserMapper接口中定义方法

<span style="font-family:Courier New;font-size:14px;">public List<Customer> findAllUsersList(UserQueryVo userQueryVo);</span>
输出映射:

需求: 查出数据库user表中的总记录数

UserMapper.xml中的配置

<span style="font-family:Courier New;font-size:14px;"><select id="findUserCount" resultType="int">
		select count(*)  from user
	</select></span>
输入类型没有 输出类型是int

UserMapper接口中定义方法

<span style="font-family:Courier New;font-size:14px;">public int findUserCount();</span>
需求:如果查询语句中使用别名!

<!-- 输出类型 -->
	<resultMap type="user" id="userId">
		<id column="id_" property="id"/>
		<result column="username_" property="username"/>
	</resultMap>
	<select id="findUserResultType" parameterType="int" resultMap="userId">
		select id id_,username username_ from user where id =#{id}
	</select>

输入类型是int 输出类型是引用的别名类型这个时候使用resultMap

UserMapper接口中定义方法

public  User findUserResultType(int id);








版权声明:本文为博主原创文章,未经博主允许不得转载。

mybatis 输入映射和输出映射

标签:mybatis

原文地址:http://blog.csdn.net/u014010769/article/details/47279193

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!