方式一:直接传
接口
?
1
2
3
|
public interface UserMapper {
public List<User> getUserById( int id);
}
|
xml
?
1
2
3
4
5
6
7
8
9
|
<?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
这种方式,在模糊查询的时候会用到,注解的参数和xml中的变量必须一致!(xml中不知道为什么必须要使用 ${} 方式,使用#{} 的方式查还不出来数据!)
接口
?
1
2
3
|
public interface UserMapper {
public List<User> getLikeList( @Param ( "name" )String pname);
}
|
xml
?
1
2
3
4
5
6
7
8
9
10
11
|
<?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类作为参数类型,那么你必须要传递所有的属性才行!
接口
?
1
2
3
4
5
6
7
8
9
10
|
import com.lxc.springboot.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
// 插入数据
public void insertUser(Map<String, Object> user);
}
|
xml
?
1
2
3
4
5
6
7
8
9
10
|
<?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传值内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://blog.csdn.net/qq_42778001/article/details/118189570
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 79
-
IOS计步器功能实现之Healthkit和CMPedometer
2025-05-29 37 -
2025-05-25 50
-
详解Intellij IDEA中.properties文件中文显示乱码问题的解决
2025-05-29 102 -
2025-05-27 92
热门评论