ResultMap結果集對映
阿新 • • 發佈:2021-11-23
資料庫中的欄位
介面
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();
}
測試出現問題
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>