MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一、设置paramterType
1.类型为基本类型
a.代码示例
映射文件:
<select id="findShopCartInfoById" parameterType="int"
resultType="ShopCart">
select*
from shopcart
where shopcartid = #{id}
</select>
Mapper接口:
ShopCart findShopCartInfoById(int id);
测试代码:
ShopCartMapper scm = sqlSession.getMapper(ShopCartMapper.class);
ShopCart shopCart = scm.findShopCartInfoById(14);
2.类型为对象类
映射文件:
<insert id="addShopCartInfo" parameterType="ShopCart">
insert into
shopcart(buyerid,bookid,amount,price,status)
values(#{buyerId},#{bookId},#{amount},#{price},#{status})
</insert>
Mapper接口:
int addShopCartInfo(ShopCart shopCart);
ShopCart.java
package com.yh.entity; public class ShopCart {
private int shopCartId;
private int buyerId;
private int bookId;
private short amount;
private float price;
private short status;
private Book book; public ShopCart() {
} public ShopCart(int buyerId, int bookId, int amount, int price, int status) {
this.buyerId = buyerId;
this.bookId = bookId;
this.amount = (short) amount;
this.price = price;
this.status = (short) status;
} public int getBuyerId() {
return buyerId;
} public void setBuyerId(int buyerId) {
this.buyerId = buyerId;
} public int getBookId() {
return bookId;
} public void setBookId(int bookId) {
this.bookId = bookId;
} public short getAmount() {
return amount;
} public void setAmount(short amount) {
this.amount = amount;
} public float getPrice() {
return price;
} public void setPrice(float price) {
this.price = price;
} public short getStatus() {
return status;
} public void setStatus(short status) {
this.status = status;
} public Book getBook() {
return book;
} public void setBook(Book book) {
this.book = book;
} public int getShopCartId() {
return shopCartId;
} public void setShopCartId(int shopCartId) {
this.shopCartId = shopCartId;
} }
测试代码:
@ResponseBody
@RequestMapping(value = "/addInfo", produces = "application/json; charset=utf-8", method = RequestMethod.GET)
public String addInfo(HttpSession session, ShopCart shopCart) {
this.init();
shopCart.setBuyerId((int) session.getAttribute("userId"));
ShopCartMapper scm = sqlSession.getMapper(ShopCartMapper.class);int result = scm.addShopCartInfo(shopCart);
this.destroy();
return result != 0 ? "添加成功" : "添加失败";
}
二、注解Mapper接口中方法参数
设置Mapper接口中@Param注释的值和映射文件中sql语句中的“参数名”相同。
映射文件:
<update id="changeAmount">
update shopcart set
amount=#{amount} where shopcartid
= #{shopCartId}
</update>
Mapper接口:
int changeAmount(@Param("amount") int amount, @Param("shopCartId") int shopCartId);
测试代码:
@ResponseBody
@RequestMapping(value = "/changeAmount", produces = "application/json;charset=utf-8")
public String changeAmount(@RequestParam(value = "amount") String amount,
@RequestParam(value = "shopCartId") String shopCartId) {
this.init();
ShopCartMapper scm = sqlSession.getMapper(ShopCartMapper.class);
int result = scm.changeAmount(Integer.valueOf(amount), Integer.valueOf(shopCartId));
this.destroy();
return result != 0 ? "修改成功" : "修改失败";
}
MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数的更多相关文章
- Mybatis映射文件sql语句注意事项
1.插入
- Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】
配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...
- mybatis mapper文件sql语句传入hashmap参数
1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...
- java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件
前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...
- MyBatis3-topic-01 -安装/下载/官方文档 -执行输入一条已经映射的sql语句
mybatis XML 映射配置文件 (官方文档) -对象工厂(objectFactory) -配置环境(environments) -映射器(mappers) 本地IDEA搭建/测试步骤 创建数据库 ...
- C#中SQL语句参数写法
OracleConnection oc=new OracleConnection("data source=osserver;User Id=****;password=**"); ...
- Mybatis中的Mapper.xml映射文件sql查询接收多个参数
我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收 ...
- MyBatis第一个案例的优化,通过映射文件与接口进行绑定
1.创建表emp CREATE DATABASE mybatis; USE mybatis; CREATE TABLE emp( id INT(11) PRIMARY KEY AUTO_INCREME ...
- mybatis里的mapper,@Mapper参数,Mapper.xml文件 sql语句Select+where语句
提示:有不清楚的可以试着看一下我最后的连接,是跟这些内容相关的 Mapper文件,特殊符号: 转义符号 原符号 中文意思 &It; < 小于号 > > 大于号 & & ...
随机推荐
- Part 39 AngularJS route change events
In this video we will discuss1. Different events that are triggered when a route change occurs in an ...
- CAP 5.2 版本发布通告
前言 今天,我们很高兴宣布 CAP 发布 5.2 版本正式版,在这个版本中,我们主要致力于更好的优化使用体验以及支持新的 Transport,同时在该版本也进行了一些 bug 修复的工作. 自从 5. ...
- 性能优化 | Go Ballast 让内存控制更加丝滑
关于 Go GC 优化的手段你知道的有哪些?比较常见的是通过调整 GC 的步调,以调整 GC 的触发频率. 设置 GOGC 设置 debug.SetGCPercent() 这两种方式的原理和效果都是一 ...
- C++和Java中的i+++i++
1 public class Cd { 2 public static void main(String[]args){ 3 int i = 50 ; 4 System.out.println(i++ ...
- Spring MVC应用
Spring MVC简介 1.1 经典三层结构 在JavaEE开发中,几乎全部都是基于B/S架构的开发.那么在B/S架构中,系统标准的三层架构包括:表现层.业务层.持久层.三层架构在我们的实际开发中使 ...
- ICCV2021 | 渐进采样式Vision Transformer
前言 ViT通过简单地将图像分割成固定长度的tokens,并使用transformer来学习这些tokens之间的关系.tokens化可能会破坏对象结构,将网格分配给背景等不感兴趣的区域,并引 ...
- Qtree V
lmn u 表示 u 所在splay子树最上方点距离最近的白点 rmn u 表示 u 所在splay子树最下方点距离最近的白点 开一个set维护所有虚儿子能走到的最近的白点的距离 考虑pushup, ...
- 一类巧妙利用利用失配树的序列DP
I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...
- 第42篇-JNI引用的管理(1)
在本地函数中会使用Java服务,这些服务都可以通过调用JNIEnv中封装的函数获取.我们在本地函数中可以访问所传入的引用类型参数,也可以通过JNI函数创建新的 Java 对象.这些 Java 对象显然 ...
- 【基因组组装】HiC挂载软件以及如何用Juice_box手工纠错?
目录 1.常用HiC挂载软件 2. Juice_box手工纠错 1.常用HiC挂载软件 ALLHiC 张兴坦老师专为多倍体和高杂合度物种基因组挂载开发.如果是复杂基因组,肯定是首选.对于简单基因组,我 ...