一、设置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. [前端随笔][Vue] 多级菜单实现思路——组件嵌套

    说在前面 本篇记录学习了vue-element-admin中的多级菜单的实现 [传送门] @vue/cli 4.2.2:vuex:scss:组件嵌套 正文 创建项目 npm create 项目名 // ...

  2. CAP 5.2 版本发布通告

    前言 今天,我们很高兴宣布 CAP 发布 5.2 版本正式版,在这个版本中,我们主要致力于更好的优化使用体验以及支持新的 Transport,同时在该版本也进行了一些 bug 修复的工作. 自从 5. ...

  3. [spojSUBST1]New Distinct Substrings

    求出后缀数组和height数组,然后因为子串即后缀的前缀,考虑不断新增后缀然后计算贡献,如果以sa的顺序新增那么第i个就会产生n-sa[k]+1-h[k](n-sa[k]+1为总方案,h为不合法的方案 ...

  4. Stream流的使用

    创建流 创建流的方式很多,从jdk8起,很多类中添加了一些方法来创建相应的流,比如:BufferedReader类的lines()方法:Pattern类的splitAsStream方法.但是开发中使用 ...

  5. spring security 认证源码跟踪

    spring security 认证源码跟踪 ​ 在跟踪认证源码之前,我们先根据官网说明一下security的内部原理,主要是依据一系列的filter来实现,大家可以根据https://docs.sp ...

  6. Codeforces 506E - Mr. Kitayuta's Gift(神仙矩阵乘法)

    Codeforces 题目传送门 & 洛谷题目传送门 神仙题 %%%%%%%%%%%%% u1s1 感觉这道题风格很省选( 下记 \(m=|s|\),首先探讨 \(n+m\) 为偶数的情形. ...

  7. 洛谷 P4437 [HNOI/AHOI2018]排列(贪心+堆,思维题)

    题面传送门 开始 WA ycx 的遗产(bushi 首先可以将题目转化为图论模型:\(\forall i\) 连边 \(a_i\to i\),然后求图的一个拓扑序 \(b_1,b_2,\dots b_ ...

  8. 洛谷 P4663 - [BalticOI 2008]魔法石(dp)

    题面传送门 A:我该是有多无聊来写这种题的题解啊 B:大概是因为这题题解区里没有题解所以我来写一篇了,说明我有高尚的济世情怀(大雾 跑题了跑题了 首先看到字典序第 \(i\) 小小可以自然地想到按位决 ...

  9. Linux-普通用户和root用户任意切换

    普通用户切换为root: 1.[xnlay@bogon ~]$含义:xnlay代表当前用户,bogon指的是主机名,~表示当前用户,$表示普通用户:[root@bogon ~]#root代表是超级用户 ...

  10. makefile高级用法

    在某些文件里面找.o find ./ -name "*.o" -e ls- R 看文件目录下面的文件 嵌套 和 VPATH [1]VPATH的用法 (1)VPATH:虚路径 1) ...