MyBatis的输入/输出参数
输入参数
MyBatis中的Dao映射配置中的parameter属性表示输入参数类型,它对应Dao接口方法的参数类型有三种:
- 基本数据类型:如int、char等
<insert id="inserName" parameterType="string">
insert into user(username) values (#{username})
insert>
- 实体类:POJO类(要输入全限定类名,如:com.simon.domain.User)
<insert id="inserUser" parameterType="com.simon.domain.User">
insert into user(username,birthday,sex,address)values (#{username},#{birthday},#{sex},#{address})
insert>
- 实体类包装类
<insert id="inserUserVO" parameterType="com.simon.domain.UserVO">
insert into user(username,birthday,sex)values (#{username},#{birthday},#{sex})
insert>
输出参数
输出参数有两种表现形式,分别为resultType和resultMap
1、resultType
resultType可以指定结果集的类型,它可以支持基本类型和实体类型
- 基本类型
<select id="findTotal" resultType="int">
select count(*) from user;
select>
- 实体类
<select id="findAll" resultType="com.simon.domain.User">
select * from user
select>
2、resultMap
resultMap标签可以建立查询的列名和实体类的属性名称不一致时建立关系,从而实现封装。在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类 型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。
- 定义resultMap
<resultMap id="userMap" type="com.simon.domain.user">
<id property="userId" column="id">id>
<result property="userName" column="username">result>
<result property="userAddress" column="address">result>
<result property="userSex" column="sex">result>
<result property="userBirthday" column="birthday">result>
resultMap>
- 映射的配置
<select id="findAll" resultMap="userMap">
select * from user
select>
原创不易,如果您觉得这篇文章有用,请您安排「在看」、「点赞」、「转发」,这将是对我最大的鼓励!