mybaits传参的几种方法

合集下载

mybatis参数传递原理

mybatis参数传递原理

mybatis参数传递原理MyBatis是一个开源的持久层框架,提供了优雅的方式将Java对象与SQL语句进行映射。

在MyBatis中,参数传递是非常重要的,它决定了如何在SQL语句中使用参数。

MyBatis的参数传递原理涉及到两方面的内容:参数绑定和动态SQL。

首先,来了解一下参数绑定。

MyBatis支持多种类型的参数绑定方式,包括简单类型、JavaBean对象、Map对象等。

在MyBatis中,参数绑定是通过占位符的方式实现的,比如${name}或#{name}。

其中,${name}使用的是直接替换的方式,而#{name}会将参数转换为预编译语句的参数占位符,从而提高SQL语句的安全性和效率。

通过参数绑定,MyBatis可以将参数传递给SQL语句,并将结果映射到Java对象中。

在映射的过程中,MyBatis会根据参数的类型和名称,自动匹配SQL语句中的占位符,并将参数绑定到对应的位置上。

简单类型的参数绑定非常简单,只需要在SQL语句中使用占位符,然后在参数传递中指定参数的值即可。

对于JavaBean对象或Map对象,MyBatis会自动将对象中的属性或Map中的键值对与占位符进行匹配。

除了静态参数绑定外,MyBatis还支持动态SQL,它可以根据不同的条件动态生成SQL语句。

动态SQL主要包括条件判断、循环和选择。

通过动态SQL,可以根据不同的条件来动态生成SQL语句,避免了过多的SQL语句重复。

在动态SQL中,参数传递的原理与静态参数绑定类似,只是在条件判断、循环和选择时,才会动态地生成SQL语句。

通过动态SQL,可以实现灵活的参数传递,根据不同的条件生成不同的SQL语句,从而提高SQL语句的灵活性和复用性。

总结起来,MyBatis的参数传递原理主要包括静态参数绑定和动态SQL。

通过静态参数绑定,可以将参数传递给SQL语句,并将结果映射到Java对象中。

通过动态SQL,可以根据不同的条件动态生成SQL语句,实现灵活的参数传递。

06-Mybatis中的传参、属性和动态SQL

06-Mybatis中的传参、属性和动态SQL

06-Mybatis中的传参、属性和动态SQL 1、mybatis中参数传递1.1 MyBatis中对sql语句参数的传递这⾥要明⽩⼀点,由于mybatis是在dao层起作⽤,dao层的接⼝中定义了和⼀个sql映射⽂件中各条sql语句的⽅法,就是说接⼝中的⽅法中需要传递的参数都是与sql语句中需要的参数对应的,那么映射⽂件中每个sql语句中添加parameterType属性以指定传递参数是什么类型就变得可有可⽆了。

1、--⼀个参数的传递这类情况就是mapper⽂件中的sql语句只需要⼀个参数,如按表中id查询数据。

传递⼀个参数⼀般都是传递简单数据类型,dao接⼝中的⽅法⼀般如下:public User selectUserById(Integer id);映射⽂件中的sql语句的占位符中变量名可以随便写:select * from t_user where id=#{随便写}当我们⽤接⼝实现类对象调⽤selectUserById时,动态代理⽣成的实现类中调⽤SqlSession对象的selectOne⽅法,将整型参数传进去,因为sql语句中只需要⼀个参数,这个值怎么传都会传给sql中的参数位置,所以在占位符中特意指定接收属性的参数值。

2、--多参数传递这类情况就是mapper⽂件中的sql语句需要多个参数,如按表中id和(或)name查询数据,或者是insert操作,亦或是update操作等等。

多参传递有多种⽅式:--1)注解@Param命名参数传参--(掌握)@Param("参数名") 数据类型变量名dao接⼝⽅法中参数定义如下:public List<User> selectByNameOrId(@Param("loginName") String name,@Param("id") Integer id);当调⽤该⽅法时内部是将我们输⼊的name(id)参数的值以注解中定义的参数名为key存储到⼀个集合中(这只是猜测),所以sql语句中的占位符中填写正确的对应参数名#{}--当然sql中的条件⼀般都是表中的字段,所以参数名都是按表中对应字段命名。

MyBatis映射器(一)--多参数传递方式

MyBatis映射器(一)--多参数传递方式

MyBatis映射器(⼀)--多参数传递⽅式在mybatis映射器的接⼝中,⼀般在查询时需要传递⼀些参数作为查询条件,有时候是⼀个,有时候是多个。

当只有⼀个参数时,我们只要在sql中使⽤接⼝中的参数名称即可,但是如果是多个呢,就不能直接⽤参数名称了,mybatis中有以下四种第⼀种:使⽤map传递1 定义接⼝1// 使⽤map传递多个参数进⾏查询2public List<Product> getByMap(Map<String, Object> paramMap);2 sql语句1<!--第⼀种:通过map传递 -->2<select id="getByMap" resultType="product" parameterType="map">3 SELECT * FROM product4 WHERE product_name LIKE concat('%',#{name},'%') AND5 CAST(product_price AS INT) > #{price}6</select>需要注意的有:1、parameterType参数类型为map(此处使⽤别名)2、参数名称是map中的key3 查询1/**2 * 通过map传递多个参数3 *4 * @return5*/6public void getProductsByMap() {7 System.out.println("使⽤map⽅式传递多个参数");8 List<Product> products = new ArrayList<>();9 Map<String, Object> paramMap = new HashMap<>();10 paramMap.put("name", "恤");11 paramMap.put("price", 200);12 sqlSession = MybatisTool.getSqlSession();13 productMapper = sqlSession.getMapper(ProductMapper.class);14 products = productMapper.getByMap(paramMap);15 printResult(products);16 }4 查看结果1使⽤map⽅式传递多个参数2 T恤2的价格是230元3 T恤3的价格是270元4 T恤4的价格是270元这种⽅式的缺点是:1、map是⼀个键值对应的集合,使⽤者只有阅读了它的键才能知道其作⽤;2、使⽤map不能限定其传递的数据类型,可读性差所以⼀般不推荐使⽤这种⽅式。

mybatis多参数传递且其中一个参数传多个值用in

mybatis多参数传递且其中一个参数传多个值用in

1. 当查询的参数只有一个时findByIds(List<Long> ids)1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list<select id="findByIdsMap" resultMap="BaseResultMap">Select<include refid="Base_Column_List" />from jria where ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach></select>findByIds(Long[] ids)1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array<select id="findByIdsMap" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from jria where ID in<foreach item="item" index="index" collection="array"open="(" separator="," close=")">#{item}</foreach></select>2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称下面是一个示例Map<String, Object> params = new HashMap<String, Object>(2);params.put("name", name);params.put("ids", ids);mapper.findByIdsMap(params);<select id="findByIdsMap" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from jria where ID in<foreach item="item" index="index" collection="ids"open="(" separator="," close=")">#{item}</foreach></select>完整的示例如下:例如有一个查询功能,Mapper接口文件定义如下方法:List<Jria> findByIds(Long... ids);使用 in 查询的sql拼装方法如下:<select id="findbyIds" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from jria where ID in<foreach item="item" index="index" collection="array"open="(" separator="," close=")">#{item}</foreach></select>例子:Mapper.xml中<!-- 根据库存状态和设备状态查询设备 --><selectid="selectByStockAndEquipmentStatus" parameterType="TbiEquipment"resultMap="resultMap">SELECT<include refid="Base_Column_List"/>FROM t_bi_equipmentwhere 0=0<if test="equipmentStockStatue != null and equipmentStockStatue !=''">and EQUIPMENT_STOCK_STATUS =#{equipmentStockStatue,jdbcType=INTEGER}</if><if test="equipmentStatue != null and equipmentStatue !=''">and EQUIPMENT_STATUS in<foreach item="item"index="index" collection="equipmentStatue"open="("separator=","close=")">#{item}</foreach></if></select>Mapper中List<TbiEquipment> selectByStockAndEquipmentStatus(Map<String, Object> params);Service中List<TbiEquipment> selectByStockAndEquipmentStatus(int equipmentStockStatue,int []equipmentStatue); 这个中的equipmentStockStatue和equipmentStatue 要和mapper.xml中的参数名保持一致,且controller 中也要保持一致ServiceImpl中@Overridepublic List<TbiEquipment> selectByStockAndEquipmentStatus(int equipmentStockStatue,int[] equipmentStatue) {Map<String, Object> params = new HashMap<String, Object>();params.put("equipmentStockStatue", equipmentStockStatue);params.put("equipmentStatue", equipmentStatue);returntbiEquipmentMapper.selectByStockAndEquipmentSta tus(params);}Controller中tbiEquipmentService.selectByStockAndEquipmentSt atus(equipmentStockStatue,equipmentStatue) equipmentStockStatue的值要定义,equipmentStatue 也要定个集合,里面也得定义值。

mybatis string 参数

mybatis string 参数

mybatis string 参数摘要:1.MyBatis 简介2.MyBatis 中的String 参数3.String 参数的传值方式4.String 参数的应用示例正文:【MyBatis 简介】MyBatis 是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。

MyBatis 避免了几乎所有的JDBC 代码和手动设置参数以及获取结果集。

MyBatis 可以使用简单的XML 或注解进行配置和原生映射,将接口和Java 的POJO(Plain Old Java Objects,普通的Java 对象)映射成数据库中的记录。

【MyBatis 中的String 参数】在MyBatis 中,我们可以使用String 类型的参数来传递数据。

String 参数在MyBatis 中是非常常用的,它可以在SQL 语句中直接使用,也可以与其他类型的参数一起组成复杂的查询条件。

【String 参数的传值方式】在MyBatis 中,String 参数的传值方式主要有两种:1.直接赋值:在SQL 语句中直接使用String 参数,例如:`SELECT * FROM user WHERE name = #{name}`,这里的`#{name}`就是String 参数。

2.使用预处理语句:在MyBatis 中,可以使用预处理语句来设置String 参数的值,例如:`SELECT * FROM user WHERE name = #{}`,这里的`#{}`就是通过预处理语句传递的String 参数。

【String 参数的应用示例】假设我们有一个用户表(user),包含以下字段:id、name、age。

现在,我们想要根据用户名查找用户信息,可以使用String 参数来实现。

1.直接赋值:```xml<select id="findUserByName" parameterType="string" resultType="er">SELECT * FROM user WHERE name = #{name}</select>```2.使用预处理语句:```xml<select id="findUserByName" parameterType="string" resultType="er">SELECT * FROM user WHERE name = #{}</select>```然后,在Java 代码中调用这两个查询方法:```java// 直接赋值User user = sqlSession.selectList("findUserByName", "name", "John Doe");// 使用预处理语句User user2 = sqlSession.selectList("findUserByName", "", "John Doe");```通过以上示例,我们可以看到如何在MyBatis 中使用String 参数来传递数据并实现查询功能。

mybatis参数方法

mybatis参数方法

mybatis参数方法MyBatis是一款非常强大的Java持久层框架,它提供了一系列灵活的特性和功能来帮助开发者简化数据库访问的过程。

在MyBatis中,参数方法是一种非常重要的机制,它允许我们在SQL语句中传递参数值并执行相关的数据库操作。

在MyBatis中,参数方法的使用非常灵活,可以根据需要传递不同类型的参数,例如基本类型、复杂对象、集合等。

同时,MyBatis还提供了一些特殊的参数方法,如动态SQL、参数映射等,使得参数的处理更加方便和易用。

一、基本类型参数方法1.单个参数MyBatis中最简单的参数方法就是使用单个基本类型参数。

例如,在SQL语句中我们可以使用#{paramName}的方式来引用参数值,参数名可以是任意合法的Java标识符。

```javaUser getUserById(int id);```2.多个参数```java```二、复杂类型参数方法除了基本类型参数外,我们也可以使用复杂对象作为参数。

在这种情况下,MyBatis会将对象的属性映射到SQL语句中。

例如:```javapublic class Userprivate int id;private String username;private int age;// 省略getter和setter方法```三、集合类型参数方法有时候我们需要传递一个集合作为参数,例如查询用户列表、批量插入数据等操作。

在MyBatis中,我们可以使用List或者数组来传递集合参数。

例如:```java"SELECT * FROM user WHERE id in" +"<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +"#{id}" +"</foreach>" +"</script>")四、动态SQL参数方法```java"SELECT * FROM user" +"<where>" +"<if test='name != null'>AND username = #{name}</if>" +"<if test='age != null'>AND age = #{age}</if>" +"</where>" +"</script>")```在上述例子中,根据name和age参数的值来动态生成WHERE子句,只有在参数不为空时才会生成对应的条件语句。

关于Mybatis的@Param注解及mybatisMapper中各种传递参数的方法

关于Mybatis的@Param注解及mybatisMapper中各种传递参数的⽅法关于Mybatis的@Param注解Mybatis 作为⼀个轻量级的数据持久化框架,⽬前(2018)的应⽤⾮常⼴泛,基本可以取代Hibernate。

关于 @param 这个注解的使⽤,作者这⾥整理了⼀些笔记。

关于Mybatis @Param 注解,官⽅⽂档:其中关于 @param部分的说明是:@Param Parameter N/A 如果你的映射器的⽅法需要多个参数, 这个注解可以被应⽤于映射器的⽅法参数来给每个参数⼀个名字。

否则,多参数将会以它们的顺序位置来被命名 (不包括任何 RowBounds 参数) ⽐如。

#{param1} , #{param2} 等 , 这是默认的。

使⽤ @Param(“person”),参数应该被命名为 #{person}。

也就是说如果有多个参数的时候,可以使⽤@Param 这个注解,但是不是⼀定需要⽤到 @Param 这个注解呢?作者在这⾥列出以下⼏种情景1.传递单个参数,不使⽤ @Param 注解代码如下:DAO 层CommodityDao.javapackage com.ljq.cs.dao;/*** @description: 商品信息 DAO 接⼝* @author: lujunqiang* @email: flying9001@* @date: 2017/12/17*/@Repositorypublic interface CommodityDao {// 查询某⼀件商品Commodity queryOne(Commodity commodity);// 省略其他⽅法}Mapper ⽂件: commoditymapper.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><mapper namespace="modityDao" ><select id="queryOne" resultType="Commodity">select *from t_commodity comwhere id = #{id}</select></mapper>这⾥只有⼀个参数,java 接⼝不使⽤ @Param 注解,同时 mapper ⽂件也不需要使⽤ parameterType 这个参数,Mybatis会根据实体类(entity)的类型⾃动识别并匹配javaBean(这⼀部分在 spring配置⽂件关于数据源那⼀部分)2.传递单个参数,使⽤@Param注解代码如下:DAO 层CommodityDao.javapackage com.ljq.cs.dao;/*** @description: 商品信息 DAO 接⼝* @author: lujunqiang* @email: flying9001@* @date: 2017/12/17*/@Repositorypublic interface CommodityDao {// 查询某⼀件商品Commodity queryOne(@Param("commodity")Commodity commodity);// 省略其他⽅法}Mapper ⽂件: commoditymapper.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><mapper namespace="modityDao" ><select id="queryOne" parameterType="modity" resultType="Commodity">select *from t_commodity comwhere id = #{commodity.id}</select></mapper>当使⽤javaBean作为对象的时候,在写 SQL 语句的时候,必须指定参数类型parameterType="modity",同时在#{ }取值的时候不能直接填⼊javaBean的属性,必须这样使⽤commodity.id ;否则,会抛出参数类型不匹配异常如果不是javaBean,则需要在写 SQL 语句的时候, #{ }中的属性必须与 @Param中定义的⼀致,eg: @Param("username") , #{username} ,这样才可以3.传递多个参数,使⽤ @Param 注解为了精简代码,作者这⾥只写关键部分DAO 层, UserInfoDao.java// ⽤户登录UserInfo signin(@Param("account")String account,@Param("passcode")String passcode);mapper⽂件userInfomapper.xml<!-- ⽤户登录 --><select id="signin" resultType="UserInfo">select *from t_userinfo infowhere account=#{account} and passcode=#{passcode}</select>这⾥ @Param 中定义的变量名必须和 mapper 中保持⼀致才可以4.传递多个参数,不使⽤ @Param 注解其实从第⼀种场景中已经可以实现传递多个参数了,即把多个参数封装到⼀个javaBean中就可以实现了,但是如果是两个或者多个javaBean 的时候,可以通过使⽤@Param注解的⽅式来实现,但是需要把每个javaBean中的属性全部拆分出来,这样就增加了巨⼤的代码量,因此不推荐这么做那么有没有可以不使⽤@Param注解,同样也可以传递多个参数(尤其是多个javaBean)呢?答案是有的,废话不多说,直接上代码同上,这⾥只贴出关键部分DAO 层, UserInfoDao.java// 搜索⽤户,对结果进⾏分页List searchUser(Map<String,Object>);使⽤DAO,UserService.javaUserInfo userInfo = new UserInfo();Pagination page = new Pagination();Map<String,Object> map = new HashMap<>;map.put("userInfo",userInfo);pam.put("page",page);userService.searchUser(map);mapper⽂件userInfomapper.xml<select id="searchUser" parameterType="java.util.Map" resultType="UserInfo">select *from t_userinfo userwhere 1 =1<if test="user.uname != null and ''!= user.uname ">and user.uname like '%${userInfo.uname}$%'</if><if test="page.order != null and page.order == 10" >order by user.id asc</if>limit ${page.pagenum * page.limitnum}, #{page.limitnum}</select>作者通过上边的4种情况,主要是为了说明,Mybatis⽆论是传单个参数,还是传递多个参数,没有必要使⽤@Param注解啊使⽤@param 注解增添了不少代码不说,还容易导致错误,尤其是在 mapper ⽂件中(paraterType属性)以上只是作者的列举的部分代码,源码请看这⾥:。

MyBatis示例-传递多个参数

MyBatis⽰例-传递多个参数映射器的主要元素:本章介绍 select 元素中传递多个参数的处理⽅式。

测试类:com.yjw.demo.MulParametersTest使⽤ Map 传递参数(不建议使⽤)使⽤ MyBatis 提供的 Map 接⼝作为参数来实现。

StudentDao/*** 使⽤ Map 传递参数** @param params* @return*/List<StudentDO> listByMap(Map<String, String> params);StudentMapper.xml<!-- 使⽤ Map 传递参数 --><select id="listByMap" parameterType="map" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from t_student<where><if test="ids != null and ids.size() > 0">AND id IN<foreach collection="ids" item="item" open="(" close=")" separator=",">#{item}</foreach></if><if test="name != null and name != ''">AND name LIKE CONCAT('%', #{name}, '%')</if><if test="sex != null">AND sex = #{sex}</if><if test="selfcardNo != null">AND selfcard_no = #{selfcardNo}</if></where></select>这个⽅法虽然简单,但是有⼀个弊端:这样设置的参数使⽤ Map,⽽ Map 需要键值对应,由于业务关联性不强,造成代码可读性低。

mybatis3中@SelectProvider传递参数方式

mybatis3中@SelectProvider传递参数⽅式mybatis3 @SelectProvider传递参数⼀、通常情况下我喜欢使⽤实体或者vo去传参数这样在Provide的⽅法中可以直接通过#{param}(param为你实体中的字段)来获取你要的参数。

⼆、使⽤map传参数在超过⼀个参数的情况下,@SelectProvide⽅法必须接受Map<String, Object>做为参数,如果参数使⽤了@Param注解,那么参数在Map中以@Param的值为key,如下例中的userId;如果参数没有使⽤@Param注解,那么参数在Map中以参数的顺序为key,如下例中的password:UserMapper.java:@SelectProvider(type = SqlProvider.class, method = "selectUserCheck")@ResultMap("userMap")public User getUserCheck(@Param("userId") long userId, String password);SqlProvider.java:public String selectUserCheck(Map<String, Object> para) {return "select * from user where userId=" + para.get("userId") + " and password=' " + para.get("1") + "'";}@SelectProvider,@Select和xml⽤法的⼀点理解1.@Select同@select功能类似的还有@insert,@delete,@update,对应于数据库语句的CRUD。

Mybatis传递多个参数进行SQL查询的用法

Mybatis传递多个参数进⾏SQL查询的⽤法PS:ibatis3如何传递多个参数有两个⽅法:⼀种是使⽤java.Map,另⼀种是使⽤JavaBean。

当只向xxxMapper.xml⽂件中传递⼀个参数时,可以简单的⽤“_parameter”来接收xxxMapper.java传递进来的参数,并代⼊查询,⽐如说这样:(1)xxxMapper.java⽂件中这样定义:List<String> selectAllAirportCode(Boolean mapping);(2)这时在对应的xxxMapper.xml⽂件中可以使⽤“_parameter”来接收这个参数:<select id="selectAllAirportCode" resultType="ng.String"parameterType="ng.Boolean">select DEPARTURE_AIRPORT from USR_AIR_LINE union selectARRIVAL_AIRPORT from USR_AIR_LINE<if test="_parameter == true">union select REL_DEPARTURE_AIRPORT from USR_AIR_LINE unionselectREL_ARRIVAL_AIRPORT from USR_AIR_LINE</if></select>但是,如果在xxxMapper.java⽂件中传递进来多个参数,就不能使⽤上⾯这种形式来接收参数,这时可以有两种⽅案来解决这个问题:⼀向xml⽂件中传递进去⼀个Map<String, Object>集合,然后xml⽂件中就可以正常使⽤Map集合中的各个参数了。

具体实例如下:(1)xxxMapper.java⽂件中这样定义:List<Airline> findAll(Map<String, Object> parms);(2)在⽤到上⾯定义的具体实现类中给Map传参:public List<Airline> findAll(PageInfo page,Airline airline) {HashMap<String,Object> params = new HashMap<String,Object>();params.put("page", page);params.put("airline", airline);return airlineMapper.findAll(params);}(3)此时对应的xxxMapper.xml⽂件使⽤“java.util.Map”来接收这个Map集合:<sql id="sqlfileders"><bind name="fileders"value="#{'id':'ID','departureAirport':'DEPARTURE_AIRPORT','relDepartureAirport':'REL_DEPARTURE_AIRPORT','arrivalAirport':'ARRIVAL_AIRPORT','relArrivalAirport':'REL_ARRIVAL_AIRPORT','popStatus':'POP_STATUS','status':'STATUS','creator':'CREATOR',' <bind name="javapropertys"value="#{'ID':'id','DEPARTURE_AIRPORT':'departureAirport','REL_DEPARTURE_AIRPORT':'relDepartureAirport','ARRIVAL_AIRPORT':'arrivalAirport','REL_ARRIVAL_AIRPORT':'relArrivalAirport','POP_STATUS':'popStatus','STATUS':'status','CREATOR':'creator',' </sql><select id="findAll" resultMap="BaseResultMap" parameterType="java.util.Map"><![CDATA[select x.* from (select z.*, rownum numbers from (]]>select<include refid="Base_Column_List" />fromUSR_AIR_LINE<where><if test="airline.departureAirport != null">DEPARTURE_AIRPORT = #{airline.departureAirport}</if><if test="airline.arrivalAirport != null">and ARRIVAL_AIRPORT=#{airline.arrivalAirport}</if><if test="airline.relDepartureAirport != null">and REL_DEPARTURE_AIRPORT =#{airline.relDepartureAirport}</if><if test="airline.relArrivalAirport != null">and REL_ARRIVAL_AIRPORT = #{airline.relArrivalAirport}</if><if test="airline.popStatus != null">and POP_STATUS = #{airline.popStatus}</if><if test="airline.status != null">and STATUS = #{airline.status}</if></where><if test="page.sortName != null"><include refid="sqlfileders" /><bind name="orderfield" value="#this.fileders[page.sortName]" />order by ${orderfield} ${page.sortOrder}</if><![CDATA[ ) z where rownum < ]]>#{page.to}<![CDATA[ ) x where x.numbers >= ]]>#{page.from}</select>注:上⾯的实例实现的是分页查询数据。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Mybaits连接数据库的几种方式:
第一种:单个参数传递
Java代码:
public List getAdmRole(String userName)
{
return dataSourceService.getAdmRole(userName);
}

Xml文件:

第二种:单个集合传递

Java代码:
public List getAdmRole(List userRoleList)
{
return dataSourceService.getAdmRole(userRoleList);
}

Xml文件:

第三种:传递的参数中有String,和List
Java代码:
public List getAdmRole(String userName)
{
List userRoleList = new ArrayList();
userRoleList.add("1000");
userRoleList.add("23432");
Map map = new HashMap();
map.put("userName", userName);
map.put("list", userRoleList);
return dataSourceService.getAdmRole(map);
}

Xml文件:

参数对应的是map集合中的key值

相关文档
最新文档