浅入浅出MyBatis(04):只获取表中的部分字段

January 14th 2015  | Tags: MyBatis, orm

2015-01-14

该文章是《专题:浅入浅出MyBatis》系列文章的一篇。点击进入该专题目录

工具与环境:Intellij IDEA 14,JDK 1.7,MyBatis 3.2,MySQL 5.5。

假设这样一个场景,现在有name和password这两个信息,如何只获得对应的id、name和email?

浅入浅出MyBatis(01):查询id为1的用户的信息的基础上,我们看一下该如何实现。

代码

修改letian.mybatis.bean下的User.java

将内容修改为:

package letian.mybatis.bean;

public class User {

    private int id;
    private String name;
    private String email;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

注意,User类中比以前少了个password字段。

修改letian.mybatis.dao下的UserMapper.java

将其内容修改为:

package letian.mybatis.dao;

import letian.mybatis.bean.User;

import java.util.HashMap;

public interface UserMapper {

    User findById(int id);
    User findByNameAndPassword(HashMap<String,String> userInfo);

}

我们添加一个方法findByNameAndPassword

修改letian.mybatis.mapper下的UserMapper.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="letian.mybatis.dao.UserMapper">

    <select id="findById" parameterType="Integer" resultType="letian.mybatis.bean.User">
        select
        * from blog_db.user where id=#{id}
    </select>

    <select id="findByNameAndPassword" parameterType="HashMap" resultType="letian.mybatis.bean.User">
        select id, name, email
        from blog_db.user where name=#{name} and password=#{password}
    </select>

</mapper>

<select id="findById">parameterType属性指的是对应的findById函数的参数的类型,findById函数的参数是int类型,我们之前设置为HashMap没什么问题,但最好还是设置为Integer更合理一些。

我们添加了id为findByNameAndPassword的select,parameterTypeHashMap,这和对应的函数的参数类型一致。resultTypeletian.mybatis.bean.User,这和对应的函数的返回类型一致。

修改Main.java

将其内容修改为:

import java.io.IOException;
import java.util.HashMap;

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 letian.mybatis.bean.User;
import letian.mybatis.dao.UserMapper;


public class Main {

    public static void main(String[] args) throws IOException {

        SqlSessionFactory sessionFactory;
        sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsReader("mybatis-config.xml"));

        SqlSession sqlSession = sessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.findById(1);
        System.out.println(user);

        HashMap<String,String> userInfo = new HashMap<String,String>();
        userInfo.put("name", "letian");
        userInfo.put("password", "123");
        user = userMapper.findByNameAndPassword(userInfo);
        System.out.println(user);
    }
}

执行结果

执行Main.java,结果如下:

User{id=1, name='letian', email='letian@111.com'}
User{id=1, name='letian', email='letian@111.com'}

(完)