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; < 小于号 > > 大于号 & & ...
随机推荐
- C#与dotNET项目想要另存为一个新项目sln文件丢了怎么办
如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到. 很简单的一个思路是反正sln是多余的 ...
- idea创建maven项目时一直在 Process Running
解决方案: 1.设置maven的配置 File--->Settings(Ctrl+Alt+S)--->Build--->Build Tools--->Maven--->R ...
- go微服务框架Kratos笔记(六)链路追踪实战
什么是链路追踪 借用阿里云链路追踪文档来解释 分布式链路追踪(Distributed Tracing),也叫 分布式链路跟踪,分布式跟踪,分布式追踪 等等,它为分布式应用的开发者提供了完整的调用链路还 ...
- 菜鸡的Java笔记 第二十七 - java 链表基本概念
链表基本概念 1.链表的基本形式 2.单向链表的完整实现 认识链表 链表= 可变长的对象数组,属于动态对象数组的范畴 链表 ...
- X-MagicBox-820的luatOS之路连载系列2
这块MagicBox小巧但外设丰富,盖板上的小液晶屏竟有240*240的分辨率.点亮后若是用最小字体,真有看瞎老王的不瞎之眼之势. 这种屏在某宝也是比较多的,大概就是长这样子: 我们这个820的盖板上 ...
- java获取CPU核心数
package ThreadTest; public class ThreadTest05 { public static void main(String[] args) { //获取CPU核心 S ...
- Python学习手册——第二部分 类型和运算(1)之字符串
Python全景 1.程序由模块构成. 2.模块包含语句. 3.语句包含表达式. 4.表达式建立并处理对象. 在python中数据是以对象的形式出现的!!! 为什么使用内置类型 内置对象使程序更容易编 ...
- restTemplate的问题-feign的项目
restTemplate的问题 1.场景描述 在使用feign的项目中,偶然的使用到了restTemplate 在普通方法调用是可以访问的,一旦使用了restTemplate,出现报错 比如: 百度 ...
- 基于Docker搭建Maven私服Nexus,Nexus详解
备注:首先在linux环境安装Java环境和Docker,私服需要的服务器性能和硬盘存储要高一点,内存不足可能到时启动失败,这里以4核8GLinux服务器做演示 一:基于Docker安装nexus3 ...
- Linux查看进程
1.ps 有很多参数,不需要全部记住,只需要记住最有用的那些参数. unix风格的参数 比如:ps -ef查看系统上运行的所有进程 ps -l 会产生一个长格式的输出 >UID:启动这些进程 ...