Springboot整合Mybatis傳值的常用方式總結(jié)
方式一:直接傳
接口
public interface UserMapper {
public List<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.lxc.springboot.mapper.UserMapper" >
<select id="getUserById" resultType="com.lxc.springboot.domain.User">
select * from user where id = #{id}
</select>
</mapper>
方式二:通過(guò)注解方式 @Param
這種方式,在模糊查詢的時(shí)候會(huì)用到,注解的參數(shù)和xml中的變量必須一致?。▁ml中不知道為什么必須要使用 ${} 方式,使用#{} 的方式查還不出來(lái)數(shù)據(jù)!)
接口
public interface UserMapper {
public List<User> getLikeList(@Param("name")String pname);
}
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.lxc.springboot.mapper.UserMapper" >
<select id="getLikeList" resultType="com.lxc.springboot.domain.User">
select id, user, name, age, password from user where name like '%${name}%'
</select>
</mapper>
方式三:通過(guò)Map鍵值對(duì)兒方式
這種方式的好處是變量(就是Map類型中的key)不需要跟字段名一致,而且傳的字段根據(jù)實(shí)際需求來(lái)定,對(duì)于這個(gè)例子來(lái)說(shuō),如果使用 User類作為參數(shù)類型,那么你必須要傳遞所有的屬性才行!
接口
import com.lxc.springboot.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
// 插入數(shù)據(jù)
public void insertUser(Map<String, Object> user);
}
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.lxc.springboot.mapper.UserMapper" >
<insert id="insertUser" parameterType="hashmap">
insert into user(user, name, age, password) values (#{userPost}, #{userName}, #{userAge}, #{userPassword})
</insert>
</mapper>
就這么多,以后項(xiàng)目中用到別的方式,在記錄!
到此這篇關(guān)于Springboot整合Mybatis傳值的常用方式總結(jié)的文章就介紹到這了,更多相關(guān)Springboot整合Mybatis傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解
在一個(gè)web應(yīng)用中驗(yàn)證碼是一個(gè)常見(jiàn)的元素。今天給大家介紹一下kaptcha的和springboot一起使用的簡(jiǎn)單例子。感興趣的朋友參考下吧2017-08-08
SpringDataRedis入門和序列化方式解決內(nèi)存占用問(wèn)題小結(jié)
spring-data-redis是spring-data模塊的一部分,專門用來(lái)支持在spring管理項(xiàng)目對(duì)redis的操作,這篇文章主要介紹了SpringDataRedis入門和序列化方式解決內(nèi)存占用問(wèn)題,需要的朋友可以參考下2022-12-12
Java基礎(chǔ)高級(jí)綜合練習(xí)題撲克牌的創(chuàng)建
今天小編就為大家分享一篇關(guān)于Java基礎(chǔ)高級(jí)綜合練習(xí)題撲克牌的創(chuàng)建,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java實(shí)現(xiàn)冒泡排序算法及對(duì)其的簡(jiǎn)單優(yōu)化示例
這篇文章主要介紹了Java實(shí)現(xiàn)冒泡排序算法及對(duì)其的簡(jiǎn)單優(yōu)化示例,冒泡排序的最差時(shí)間復(fù)雜度為O(n^2),最優(yōu)時(shí)間復(fù)雜度為O(n),存在優(yōu)化的余地,需要的朋友可以參考下2016-05-05
SpringBoot靜態(tài)資源css,js,img配置方案
這篇文章主要介紹了SpringBoot靜態(tài)資源css,js,img配置方案,下文給大家分享了三種解決方案,需要的朋友可以參考下2017-07-07

