一、设置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语句参数的更多相关文章

  1. Mybatis映射文件sql语句注意事项

    1.插入

  2. Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】

    配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...

  3. mybatis mapper文件sql语句传入hashmap参数

    1.怎样在mybatis mapper文件sql语句传入hashmap参数? 答:直接这样写map就可以 <select id="selectTeacher" paramet ...

  4. java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件

    前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...

  5. MyBatis3-topic-01 -安装/下载/官方文档 -执行输入一条已经映射的sql语句

    mybatis XML 映射配置文件 (官方文档) -对象工厂(objectFactory) -配置环境(environments) -映射器(mappers) 本地IDEA搭建/测试步骤 创建数据库 ...

  6. C#中SQL语句参数写法

    OracleConnection oc=new OracleConnection("data source=osserver;User Id=****;password=**"); ...

  7. Mybatis中的Mapper.xml映射文件sql查询接收多个参数

    ​ 我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收 ...

  8. MyBatis第一个案例的优化,通过映射文件与接口进行绑定

    1.创建表emp CREATE DATABASE mybatis; USE mybatis; CREATE TABLE emp( id INT(11) PRIMARY KEY AUTO_INCREME ...

  9. mybatis里的mapper,@Mapper参数,Mapper.xml文件 sql语句Select+where语句

    提示:有不清楚的可以试着看一下我最后的连接,是跟这些内容相关的 Mapper文件,特殊符号: 转义符号 原符号 中文意思 &It; < 小于号 > > 大于号 & & ...

随机推荐

  1. celery config

    /* Useful celery config. app = Celery('tasks', broker='redis://localhost:6379', backend='redis://loc ...

  2. 常见yaml写法-deployment

    apiVersion: extensions/v1beta1 #接口版本 kind: Deployment #接口类型 metadata: name: cango-demo #Deployment名称 ...

  3. 环境(8)Linux用户组权限

    一:Linux时间日期-时间同步策略 1.日期与时间 ①时间命令 data:查看当前系统时间 cal :查看日历     cal  2020 修改时间:   date -s  11:11:11    ...

  4. pytest框架+conftest.py配置公共数据的准备和清理

    1.pytest介绍:1.自动发现测试模块和测试方法 2.断言使用 assert+表达式即可 3.可以设置会话级.模块级.类级.函数级的fixture 数据准备+清理工作 4.丰富的插件库,==all ...

  5. Netcat基础使用

    netcat命令选项 本文参考文章链接 本文参考文章链接1 本文参考文章链接2 本文参考文章链接3 常用命令选项 ## 网络模式和代理相关 -l 监听,作服务器.不填时作客户端. -u UDP模式.不 ...

  6. 04373 C++程序设计 2019版 第一章习题五、程序设计题

    题目: 1.编写一个程序,将从键盘输入的n个字符串保存在一个一维数组A中.在输入字符串之前,先输入n的值.要求,数组A需要动态申请空间,程序运行结束前再释放掉. #include <iostre ...

  7. [luogu5387]人形演舞

    先对每一个求sg函数,暴力复杂度为$o(m^{2})$ 取$k$满足$2^{k}\le x<2^{k+1}$(即$x$二进制下的最高位),考虑$y$与$2^{k}$的关系 1.若$1\le y& ...

  8. vue实现聊天+图片表情功能

    项目需求是这样的:要求实现类似于微信聊天一样,表情+文字效果 "文字效果" 表情包三种方案 表情包的实现其实可以分为以下三种情况: 表情包:点击表情--直接发送大表情(这种方案其实 ...

  9. Java生产环境JVM设置成固定堆大小深层原理

    可能很多人都知道Java程序上生产后,运维人员都会设定好JVM的堆大小,而且还是把最大最小设置成一样的值.那究竟是为什么呢?一般而言,Java程序如果你不显示设定该值得话,会自动进行初始化设定. -X ...

  10. CF1373G

    考虑中间格子不能有相同的点,其实是没用的. 其唯一用处是用来规定最后的是无法重叠的. 我们可以证明最后位置的无重叠和中间不重叠是充要的. 那显然可以我们对每个点往后连边: 形式的话的说: 对 \((x ...