Mybatis框架四:输入参数、输出参数

输入参数可以有三种:简单类型,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,不过通常不会用,尽量保持一致即可

上一篇:调用android的getColor()方法出现 java.lang.NoSuchMethodError: android.content.res.Resources.getColor


下一篇:第9章 符合Python风格的对象