mybatis--parametertype的参数传递
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<!-- 设置MyBatis使用log4j日志支持 -->
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- typeAliases给类型取别名 -->
<typeAliases>
<!-- 给user类取别名 -->
<!-- <typeAlias type="com.bjsxt.pojo.User" alias="u"/> -->
<!-- 别名都是类的名字 -->
<package name="com.bjsxt.pojo"/>
</typeAliases>
<!-- 用于指定使用哪个开发
用于指定使用的环境id
-->
<environments default="dev">
<!-- 用于配置开发环境
id:环境的唯一识别码
-->
<environment id="dev">
<!-- 事务管理器
type:用于设定mybatis采用什么方式管理事务
JDBC表示和JDBC一样事务的管理方式
-->
<transactionManager type="JDBC"/>
<!-- 数据源/连接池
用于配置链接池和数据库链接的参数
type:用于设置mybatis是否采用链接池技术
连接池:用来存数据库链接的,减少数据库的频繁开关
POOLED表示mybatis采用连接池技术
-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 扫描mapper文件 -->
<!-- 文件的全限制路径要用/ -->
<mappers>
<mapper resource="com/bjsxt/mapper/UserMapper.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-|3-mapper.dtd">
<!-- namespa:命名空间,可以随意定义,一般情况下要写全限定路径(包名加类)
MyBatis管理SQL语句是通过namespace+id来定位的
-->
<mapper namespace="com.bjsxt.mapper.UserMapper">
<!-- select标签用于编写查询语句
id:sql语句的唯一的标识,类比为方法名
resultType:用于设定返回结果的类型(全限定路径)
如果返回结果是集合,要写集合泛型的类型
-->
<select id="sellAll" resultType="user">
select * from t_user
</select>
<select id="selOne" resultType="user">
select * from t_user where id=1
</select>
<!-- parameterType,参数类型,用于参数的传递 -->
<select id="selById" resultType="user" parameterType="int">
<!--
#{用于使用参数
index,索引,从0开始
param+数字,param1,param2,parame3}
-->
select * from t_user where id=#{0}
</select>
<select id="sel" resultType="user" parameterType="map">
<!-- 如果参数是对象,可以通过#{属性名}来获取 -->
<!-- 如果参数是map,可以通过#{key}来获取 -->
select * from t_user where username=#{username} and password=#{password}
</select>
</mapper>
package com.bjsxt.test; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import com.bjsxt.pojo.User; public class TestParams {
@Test
public void selById(){
SqlSession session=null;
try {
session=(SqlSession) new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis.xml"))
.openSession(); User user=session.selectOne("com.bjsxt.mapper.UserMapper.selById",2);
System.out.println(user);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.close(); } @Test
public void sel(){
SqlSession session=null;
try {
session=(SqlSession) new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream("mybatis.xml"))
.openSession(); /*User u=new User();
u.setUsername("zhangsan");
u.setPassword("123");*/ Map<String,String> map =new HashMap<>();
map.put("username", "zhangsan");
map.put("password", "123"); User user=session.selectOne("com.bjsxt.mapper.UserMapper.sel",map);
System.out.println(user);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
session.close(); }
}
mybatis--parametertype的参数传递的更多相关文章
- mybatis的mapper参数传递
简单参数传递 简单参数传递是指: 传递单个基本类型参数,数字类型.String 传递多个基本类型参数 parameterType 属性可以省略: 传递单个基本类型参数 SQL语句中参数的引用名称并不 ...
- MyBatis parameterType、resultType的数据类型
parameterType.resultType的数据类型要写全限定类名或者别名. mybatis已经给常用的数据类型起好了别名,参考mybatis.pdf 3.1.1小节: 基本数据类型很少用,一 ...
- mybatis parameterType和resultType的顺序问题
有一次在写java web后端的代码中发现了一个问题,那就是我将parameterType放在resultType之前的时候,发现程序启动的时候突然报错,说找不到某某map,我也不知道,所以感觉非常怪 ...
- mybatis parameterType报错:There is no getter for property named 'xxx' in 'class java.lang.String'
方法1: 当parameterType = "java.lang.String" 的时候,参数读取的时候必须为 _parameter 方法2: 在dao层的时候,设置一下参数,此方 ...
- mybatis的多参数传递,使用
1.mybatis的多参数使用 mapper 使用 通过@param注解,传递到xml中的参数名称指定 public interface RedisInstanceTypesMapper extend ...
- mybatis传入参数类型parameterType和输出结果类型resultType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType和resultType属性,parameterType属性用于对 ...
- mybatis传入参数类型parameterType详解
前言 Mybatis的Mapper文件中的select.insert.update.delete元素中都有一个parameterType属性,用于对应的mapper接口方法接受的参数类型. ( res ...
- java框架---->mybatis的使用(一)
这里我们记录一些mybatis的一些常用知识和项目中遇到的问题总结.快乐人生的三个必要元素是,有要做的事.热爱的事及盼望的事. mybatis的一些知识 一.mybatis插入返回主键值 插入的jav ...
- MyBatis进阶(一)
MyBatis参数传递 1. MyBatis单参数传递 单参数传递不做特殊处理,直接取出参数值赋给xml文件,如#{id} 2. MyBatis多参数传递 多参数传递默认使用{arg1, arg0, ...
- SSM之Mybatis整合及使用
SSM 在ss基础上加进行整合Mybatis(applicationContext.xml中添加配置),并添加分页拦截器(添加mybatis分页拦截器),并用generator动态生成到层. 构建基础 ...
随机推荐
- nowcoder 211E - 位运算?位运算! - [二进制线段树][与或线段树]
题目链接:https://www.nowcoder.com/acm/contest/211/E 题目描述 请实现一个数据结构支持以下操作:区间循环左右移,区间与,区间或,区间求和. 输入描述: 第一行 ...
- TensorRT使用解析器导入模型的过程
- hive中的几个参数:元数据配置、仓库位置、打印表字段相关参数
hive仓库位置由以下参数决定,默认位置/user/hive/warehouse: <property> <name>hive.metastore.wareho ...
- MVC 实用架构设计(三)——EF-Code First(5):二级缓存
一.前言 今天我们来谈谈EF的缓存问题. 缓存对于一个系统来说至关重要,但是是EF到版本6了仍然没有见到有支持查询结果缓存机制的迹象.EF4开始会把查询语句编译成存储过程缓存在Sql Server中, ...
- linux fix the superblock by dumpe2fs fsck
It seems that you have a bad superblock. To fix this: Firstly, boot into a live CD or USB Find out y ...
- ios开发dismiss所有控制器
-(void)dismissToRootViewController { UIViewController *vc = self; while (vc.presentingViewController ...
- BouncyCastle 密钥转换 - Java
转自: https://blog.csdn.net/a351945755/article/details/63707040 1. PKCS#8 转 PKCS#1 You will need Bounc ...
- python摸爬滚打之day010----函数进阶
1.函数动态传参 *args : 将所有的位置参数打包成一个元组的形式. **kwargs : 将所有的关键字参数打包成一个字典的形式. 形参的接收顺序: 位置参数 > *args > ...
- TCP/IP具体解释--TCP首部的TimeStamp时间戳选项
TCP应该是以太网协议族中被应用最为广泛的协议之中的一个,这里就聊一聊TCP协议中的TimeStamp选项.这个选项是由RFC 1323引入的,该C建议提交于1992年.到今天已经足足有20个年头.只 ...
- 为什么要使用 Docker(二)
作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势. 更高效的利用系统资源 由于容器不需要进行硬件虚拟以及运行完整操作系统等额外开销,Docker 对系统资源的利用率更高.无 ...