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中的變量必須一致!(xml中不知道為什么必須要使用 ${} 方式,使用#{} 的方式查還不出來數(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>
方式三:通過Map鍵值對兒方式
這種方式的好處是變量(就是Map類型中的key)不需要跟字段名一致,而且傳的字段根據(jù)實際需求來定,對于這個例子來說,如果使用 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>
就這么多,以后項目中用到別的方式,在記錄!
到此這篇關于Springboot整合Mybatis傳值的常用方式總結的文章就介紹到這了,更多相關Springboot整合Mybatis傳值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringCloud學習筆記之SpringCloud搭建父工程的過程圖解
SpringCloud是分布式微服務架構的一站式解決方案,十多種微服務架構落地技術的集合體,俗稱微服務全家桶,這篇文章主要介紹了SpringCloud學習筆記(一)搭建父工程,需要的朋友可以參考下2021-10-10
idea pom導入net.sf.json的jar包失敗的解決方案
JSON(JavaScript Object Notation,JS對象簡譜)是一種輕量級的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧2023-11-11

