ResultMap结果集映射

数据库中的字段

ResultMap结果集映射

接口

package com.my.dao;
import com.my.pojo.User;
import java.util.List;
public interface UserMapper {
    //根据ID查询用户
    User getUserById(int id);
}

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="com.my.dao.UserMapper">
    <select id="getUserById"  parameterType="int" resultType="com.my.pojo.User">
        select * from mybatis.user where id=#{id}
   </select>
</mapper>

实体类

package com.my.pojo;
//框架=设计模式+反射+注解
import org.apache.ibatis.type.Alias;
public class User {
    private int id;
    private String name;
    private String password;
    public User() {
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

utils(数据库)

public class MybatisUtils {
    private static  SqlSessionFactory sqlSessionFactory;
    static{
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  static SqlSession getSqlSession(){
      return sqlSessionFactory.openSession();
    }

}

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3307/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

mybatis-config.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>
    <!--引入外部文件在同一级目录下
    优先读取外部文件并且当前的属性值来覆盖掉
    -->
    <properties resource="db.properties"/>

    <!--别名-->
    <typeAliases>
        <typeAlias type="com.my.pojo.User" alias="user"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <!--连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
   <!-- <mapper resource="com/my/dao/UserMapper.xml"/>-->
   <mapper class="com.my.dao.UserMapper"/>
    </mappers>
</configuration>

测试类

public class UserDaoTest {
    @Test
    public void getUserLike()  {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);
        sqlSession.close();
    }

测试出现问题

ResultMap结果集映射

select * from mybatis.user where id=#{id}等于
select id ,name,pwd from mybatis.user where id=#{id}**

解决方法一

select id ,name,pwd as password from mybatis.user where id=#{id}**
//这是将数据库中的名字作为 字段名 password来使用

解决方法二 (resultMap(结果集映射))

 id   name    pwd//数据库字段名
 id   name     password//实体类
    <resultMap id="UserMap" type="User">
        <!--column数据库中的字段,property 实体类中的属性-->
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result  column="pwd" property="password"/>
    </resultMap>
<select id="getUserById"  parameterType="int" resultMap="UserMap">
       select id ,name,pwd    from mybatis.user where id=#{id}
  </select>
上一篇:从零开始---控制台用c写俄罗斯方块游戏(2)


下一篇:[div+css]网站布局实例二