一、parameterType传递多个参数
默认情况只能有一个parameterType;
parameterType 可以为简单类型(int . String 等),自定义pojo,hashMap...等等
如果要分开传多个参数,可以使用pojo,或者hashMap
也可以通过注解实现传递多个参数
- mapper接口参数列表有多个,使用注解 param(“”), param中value是什么 , xml中就通过什么取
void insertWithManyParam(@Param("name") String name, @Param("birthday") Date date, @Param("age") int age);
- xml中取值
<insert id="insertWithManyParam">
INSERT user1 SET NAME = #{name},age=#{age},birthday=#{birthday}
</insert>
3.单元测试代码
@Test
public void testManyParam() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
mapper.insertWithManyParam("多个参数传递", new Date(), 48);
sqlSession.commit();
sqlSession.close();
}
4.控制台日志
二、resultMap不配置个性映射,默认根据pojo属性名称映射
下面是例子:
1.User类
public class User {
private Integer id;
private String name;
private Integer age;
private Date birthday;
2.xml文件配置
<resultMap id="testResultMap" type="user">
<id property="id" column="_id"/>
<result property="name" column="_name"/>
</resultMap>
``<select id="findUserById" parameterType="Integer" resultMap="testResultMap">
SELECT a.id AS _id,a.name AS _name,a.age,a.* FROM ssm.user1 AS a WHERE id=#{id}
</select>`
User类中还有birthday age属性,resultMap不配置,也可以映射成功;
3.测试代码
@Test
public void testFindById() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper1 mapper = sqlSession.getMapper(UserMapper1.class);
User user = mapper.findUserById(3);
System.out.println(user);
sqlSession.close();
}
“`
4.控制台日志
如果查询到的列存在重复字段,mybatis会自动去重