最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis使用resultMap獲取不到值的解決方案

 更新時間:2021年08月25日 12:00:02   作者:木頭沒有瓜  
這篇文章主要介紹了mybatis使用resultMap獲取不到值的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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字段,在實體中聲明

Mybatis 從數(shù)據(jù)庫中獲取值為null ResultMap

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();
    }
}

測試結(jié)果:

pwd的值為null

mybatis會根據(jù)這些查詢的列名(會將列名轉(zhuǎn)化為小寫,數(shù)據(jù)庫不區(qū)分大小寫) , 去對應的實體類中查找相應列名的set方法設(shè)值 , 由于找不到setPassword() , 所以pwd返回null ; 【自動映射】

在這里插入圖片描述

解決方法

使用結(jié)果集映射: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>

測試結(jié)果:

在這里插入圖片描述

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot配置攔截器代碼實例

    spring boot配置攔截器代碼實例

    這篇文章主要介紹了spring boot配置攔截器代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • java的接口解耦方式

    java的接口解耦方式

    這篇文章主要介紹了java的接口解耦方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring入門到精通之Bean標簽詳解

    Spring入門到精通之Bean標簽詳解

    這篇文章主要為大家詳細介紹了Spring中Bean的標簽,文中的示例代碼講解詳細,對我們學習Spring有一定的幫助,快跟隨小編一起學習學習吧
    2022-07-07
  • Java開發(fā)Oracle數(shù)據(jù)庫連接JDBC Thin Driver 的三種方法

    Java開發(fā)Oracle數(shù)據(jù)庫連接JDBC Thin Driver 的三種方法

    這篇文章主要介紹了Java開發(fā)Oracle數(shù)據(jù)庫連接JDBC Thin Driver 的三種方法,需要的朋友可以參考下
    2015-12-12
  • 探索Java分布式限流技術(shù)

    探索Java分布式限流技術(shù)

    探索Java分布式限流技術(shù),讓你的系統(tǒng)遠離流量過載的煩惱,本指南將帶你了解如何使用Java實現(xiàn)高效的限流策略,幫助你輕松應對高并發(fā)場景,讓我們一起開啟這段精彩的技術(shù)之旅,打造更加穩(wěn)定可靠的系統(tǒng),需要的朋友可以參考下
    2024-03-03
  • 2023最新版本idea用maven新建web項目(親測不報錯)

    2023最新版本idea用maven新建web項目(親測不報錯)

    這篇文章主要給大家介紹了關(guān)于2023最新版本idea用maven新建web項目,Maven是當今Java開發(fā)中主流的依賴管理工具,文中介紹的步驟親測不報錯,需要的朋友可以參考下
    2023-07-07
  • SpringBoot整合SpringSession實現(xiàn)分布式登錄詳情

    SpringBoot整合SpringSession實現(xiàn)分布式登錄詳情

    這篇文章主要介紹了SpringBoot整合SpringSession實現(xiàn)分布式登錄詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • Mybatis Integer類型參數(shù)值為0時得到為空的解決方法

    Mybatis Integer類型參數(shù)值為0時得到為空的解決方法

    這篇文章主要介紹了Mybatis Integer類型參數(shù)值為0時得到為空的解決方法,有需要的朋友們可以學習下。
    2019-08-08
  • springboot整合logback打印日志,分文件

    springboot整合logback打印日志,分文件

    本文主要介紹了springboot整合logback打印日志,分文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-11-11
  • Mybatisplus詳解如何注入自定義的SQL

    Mybatisplus詳解如何注入自定義的SQL

    mybatis-plus 提供了許多默認單表 CRUD 語句,對于其他 SQL 情況愛莫能助。如果有一個刪庫跑路,并且需要多次調(diào)用,來清空多張表數(shù)據(jù)得需求,那么如何把他封裝在 mybatis-plus 中調(diào)用呢,下面我們一起來看一下
    2022-06-06

最新評論

玉屏| 邵武市| 盘山县| 惠来县| 岳阳市| 桦川县| 光山县| 安庆市| 金乡县| 友谊县| 黄平县| 营山县| 阿拉善右旗| 汝阳县| 班玛县| 砀山县| 灵川县| 罗甸县| 琼结县| 滕州市| 四平市| 公安县| 轮台县| 定安县| 霍城县| 紫金县| 贺兰县| 溧阳市| 乐山市| 六盘水市| 涡阳县| 安吉县| 湘西| 方正县| 黑河市| 齐河县| 东山县| 阳曲县| 克什克腾旗| 色达县| 长顺县|