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

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

时间:2018-08-02 14:54:59      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:hda   技术   new   cti   close   environ   host   birt   this   

输入参数可以有三种:简单类型,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 + "]";
    }

}
View Code

 

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框架四:输入参数、输出参数

标签:hda   技术   new   cti   close   environ   host   birt   this   

原文地址:https://www.cnblogs.com/xuyiqing/p/9406396.html

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