一、设置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. JVM 核心参数

    JVM 内存相关的几个核心参数 参数部分看我笔记   https://note.youdao.com/s/Ch3awnVu JVM模板 1. ParNew + CMS 版 根据服务调整 -Xmx -X ...

  2. 【JAVA】编程(4)---摇色子

    作业要求: 利用" Math.random ( )  "生成随机数的方法来模拟同时摇三个色子获得的点数:点数的多少不同,也会导致不同的输出结果:可适当对程序增添一些更有趣的功能: ...

  3. c++学习笔记4(函数重载)

    一个或多个函数,名字相似,然而参数个数或类型不同,这个叫做函数重载 优点:可以使函数的命名变得简单

  4. JS和JQUERY常见函数封装方式

    JS中常用的封装函数4种方法: 1. 函数封装法: function box(){ } 2. 封装成对象 : let Cookie = { get(){ }, set(){ } } 3. 封装成构造函 ...

  5. 单元测试NUnit,mock组件NSubstitute,信号量SemaphoreSlim,异步lock等例子

    public class LockTest { private IDatabase _database; private readonly Random _random = new Random(); ...

  6. [atARC105F]Lights Out on Connected Graph

    记$G[S]$表示图$G$在点集$S$上的导出子图,即$G[S]=(S,{(x,y)|x,y\in S且(x,y)\in E})$ 定义$g(S)$为所有$E'$(满足$E'\subseteq G[S ...

  7. 洛谷 P6199 - [EER1]河童重工(点分治+虚树)

    洛谷题面传送门 神仙题. 首先看到这样两棵树的题目,我们肯定会往动态树分治的方向考虑.考虑每次找出 \(T_2\) 的重心进行点分治.然后考虑跨过分治中心的点对之间的连边情况.由于连边边权与两棵树都有 ...

  8. 洛谷 P5897 - [IOI2013]wombats(决策单调性优化 dp+线段树分块)

    题面传送门 首先注意到这次行数与列数不同阶,列数只有 \(200\),而行数高达 \(5000\),因此可以考虑以行为下标建线段树,线段树上每个区间 \([l,r]\) 开一个 \(200\times ...

  9. spring-boot spring-MVC自动配置

    Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...

  10. 远程登录Linux系统及上传下载文件

    目录 1. 远程登录Linux系统 1.1 为什么要远程登录 1.2 Xshell6安装 1.3 连接登录 1.3.1 连接前提 1.3.2 Xshell连接配置 2. 远程上传下载文件 2.1 Xf ...