Springboot整合Mybatis傳值的常用方式總結
方式一:直接傳
接口
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>
方式二:通過注解方式 @Param
這種方式,在模糊查詢的時候會用到,注解的參數(shù)和xml中的變量必須一致?。▁ml中不知道為什么必須要使用 ${} 方式,使用#{} 的方式查還不出來數(shù)據!)
接口
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>
方式三:通過Map鍵值對兒方式
這種方式的好處是變量(就是Map類型中的key)不需要跟字段名一致,而且傳的字段根據實際需求來定,對于這個例子來說,如果使用 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ù)據 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>
就這么多,以后項目中用到別的方式,在記錄!
到此這篇關于Springboot整合Mybatis傳值的常用方式總結的文章就介紹到這了,更多相關Springboot整合Mybatis傳值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot 集成Kaptcha實現(xiàn)驗證碼功能實例詳解
在一個web應用中驗證碼是一個常見的元素。今天給大家介紹一下kaptcha的和springboot一起使用的簡單例子。感興趣的朋友參考下吧2017-08-08SpringDataRedis入門和序列化方式解決內存占用問題小結
spring-data-redis是spring-data模塊的一部分,專門用來支持在spring管理項目對redis的操作,這篇文章主要介紹了SpringDataRedis入門和序列化方式解決內存占用問題,需要的朋友可以參考下2022-12-12- 不喜歡羅里吧嗦,講的很精簡易懂。從基礎開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例
這篇文章主要介紹了Java實現(xiàn)冒泡排序算法及對其的簡單優(yōu)化示例,冒泡排序的最差時間復雜度為O(n^2),最優(yōu)時間復雜度為O(n),存在優(yōu)化的余地,需要的朋友可以參考下2016-05-05SpringBoot靜態(tài)資源css,js,img配置方案
這篇文章主要介紹了SpringBoot靜態(tài)資源css,js,img配置方案,下文給大家分享了三種解決方案,需要的朋友可以參考下2017-07-07最新評論