您好,登錄后才能下訂單哦!
這篇文章主要介紹了mybatis使用resultMap獲取不到值怎么辦,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
<resultMap type="com.fc.model.Shop" id="employeeMap"> <id column="shop_id" property="shopId"></id> <result column="name" property="name"></result> </resultMap> <!-- 獲取店員列表 --> <select id="getEmployeeList" parameterType="java.util.Map" resultMap="employeeMap"> select *, ( 6371 * acos ( cos ( radians( #{latitude} ) ) * cos( radians( s.latitude ) ) * cos( radians( s.longitude ) - radians( #{longitude} ) ) + sin ( radians( #{latitude} ) ) * sin( radians( s.latitude ) ) ) ) as distance from <include refid="table_name"></include> as e join <include refid="table_name_shop"></include> as s on e.shop_id = s.shop_id limit #{offset, jdbcType=INTEGER}, #{limit, jdbcType=INTEGER} </select>
前端獲取的接口沒有得到distance字段
在實體中沒有聲明distance字段,在實體中聲明
ResultMap和返回值為空的的問題
代碼塊如下:
接口:
package com.lx.dao; import com.lx.pojo.User; public interface UserMapper { User getUserById(int id); }
穿插:
要想使用@Alias注解的話,必須要在mybatis-config.xml配置typeAlias,例如:
<typeAliases> <package name="com.lx.pojo"/> </typeAliases>
實體類:
package com.lx.pojo; import org.apache.ibatis.type.Alias; @Alias("user") public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } 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 getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
resouce目錄下的數(shù)據(jù)庫配置文件:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8 username=root pwd=123456
<?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> <package name="com.lx.pojo"/> </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="${pwd}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com\lx\dao\UserMapper.xml"/> </mappers> </configuration>
工具類:獲取sqlSession
package com.lx.utils; 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 java.io.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ //使用Mybatis第一步:獲取sqlSessionFactory對象 String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } //有了SqlSessionFactory ,可以從中獲取SqlSession 的實例 //SqlSession 在其中包含了面向數(shù)據(jù)庫執(zhí)行 SQL 命令的所有方法 public static SqlSession getSqlSession(){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
執(zhí)行sql語句,幫定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"> <!--namespace=綁定一個對應的Dao/Mapper接口--> <mapper namespace="com.lx.dao.UserMapper"> <select id="getUserById" parameterType="int" resultType="user"> select * from user where id= #{id} </select> </mapper>
測試類:
import com.lx.dao.UserMapper; import com.lx.pojo.User; import com.lx.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class test3 { @Test public void userbyid(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); sqlSession.close(); } }
測試結果:
pwd的值為null
mybatis會根據(jù)這些查詢的列名(會將列名轉(zhuǎn)化為小寫,數(shù)據(jù)庫不區(qū)分大小寫) , 去對應的實體類中查找相應列名的set方法設值 , 由于找不到setPassword() , 所以pwd返回null ; 【自動映射】
使用結果集映射:ResultMap
column是數(shù)據(jù)庫表的列名 , property是對應實體類的屬性名
<?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"> <!--namespace=綁定一個對應的Dao/Mapper接口--> <mapper namespace="com.lx.dao.UserMapper"> <resultMap id="usermap" type="user"> <!-- id為主鍵 --> <id column="id" property="id"/> <!-- column是數(shù)據(jù)庫表的列名 , property是對應實體類的屬性名 --> <result column="name" property="name"/> <result column="password" property="pwd"/> </resultMap> <select id="getUserById" resultMap="usermap"> select * from user where id=#{id} </select> <!-- <select id="getUserById" parameterType="int" resultType="user">--> <!-- select * from user where id= #{id}--> <!-- </select>--> </mapper>
測試結果:
感謝你能夠認真閱讀完這篇文章,希望小編分享的“mybatis使用resultMap獲取不到值怎么辦”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。