mybatis中#{0}和#{arg0}的区别,xml中如何使用mapper接口的参数?

区别

在mybatis早的版本(3.4.2之前)中,如果Mapper接口中的方法的参数上没有使用注解指定参数名@Param(""),则可以按照参数的顺序,在xml文件中使用#{0}、#{1}、#{2}、#{3}…来获取到参数中的数据。

举例:

Mapper接口:

User getUserBys(int id,String name);

xml文件:

<select id="getUserBys" resultType="com.model.User">
	select * from user where id = #{0} and name=#{1}
</select>

在mybatis后来的版本(3.4.2之后)中,使用arg0来代替0

举例

Mapper接口:

User getUserBys(int id,String name);

xml文件:

<select id="getUserBys" resultType="com.model.User">
	select * from user where id = #{arg0} and name=#{arg1}
</select>
注意:上面两种方法都属于位置传递

建议

为了避免不必要的麻烦,我们可以使用值传递的方式,在Mapper类中使用@Param("")注解,xml中使用的话,直接通过名字找到。
Mapper接口:

User getUserBys(@Param("id") int id,@Param("name") String name);

xml文件:

<select id="getUserBys" resultType="com.model.User">
	select * from user where id = #{id} and name=#{name}
</select>

扩展

对于集合来说,mybatis有默认的参数名字,比如List类型的可以通过list获取、数组类型的可以通过array获取。举个例子,接口的参数名为idList,在xml中要用到这个参数,可以使用list获取。

Mapper接口:

public List<User> findByIdList(List<Integer> idList);

xml:

<select id="findByIdList" resultType="com.example.bean.User">
	SELECT id, name, password, address, enable
	FROM user
	<where>
        <if test="list!= null and list.size() > 0">
            id IN
            <foreach collection="list" item="ietm" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
	</where>
</select>
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 对于你的第一个问题,使用MyBatis创建ResultMap映射和获得Session可以按照以下步骤操作: 1. 创建ResultMap映射 在MyBatis的配置文件使用<resultMap>标签创建ResultMap映射。例如: <resultMap id="userMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="email" property="email"/> </resultMap> 在上面的例子,我们定义了一个名为"userMap"的ResultMap映射,它映射到User类。该映射包括了4个属性:id、username、password和email。 2. 创建SqlSessionFactory 使用MyBatis,我们需要首先创建一个SqlSessionFactory对象。可以使用MyBatis提供的SqlSessionFactoryBuilder类来创建SqlSessionFactory对象。例如: String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 在上面的例子,我们加载了MyBatis的配置文件,并使用SqlSessionFactoryBuilder创建了一个SqlSessionFactory对象。 3. 获得Session 一旦我们有了SqlSessionFactory对象,我们就可以通过它来获得Session对象。例如: SqlSession session = sqlSessionFactory.openSession(); 在上面的例子,我们使用SqlSessionFactory对象的openSession方法创建了一个Session对象。 希望这个回答对你有所帮助。 ### 回答2: 使用mybatis创建resultMap映射可以通过在映射文件(mapper.xml定义resultMap标签来实现。具体步骤如下: 1. 在mapper.xml文件的<select>、<insert>、<update>、<delete>等操作标签内部,使用<resultMap>标签创建resultMap映射。 2. 在<resultMap>标签使用<id>标签定义主键属性的映射,使用<result>标签定义其他属性的映射。 3. 在<id>和<result>标签内部,使用属性column来指定数据库的列名,使用属性property来指定对应的Java对象属性名。 例如,假设有一个User表,有id、name和age这三个列,对应Java对象User的id、name和age属性,可以如下创建resultMap映射: <resultMap id="userMap" type="User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> 创建获得session的方法如下: 在mybatis的配置文件(mybatis-config.xml,可以配置数据源和transactionManager,然后使用SqlSessionFactoryBuilder类的build方法根据配置文件创建SqlSessionFactory对象。 之后,通过openSession方法获取到一个SqlSession对象。代码示例如下: // 加载mybatis的配置文件 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); // 使用SqlSessionFactoryBuilder创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 使用SqlSessionFactory创建一个SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); 注意,在完成使用SqlSession之后,需要关闭SqlSession,避免资源泄露: sqlSession.close(); ### 回答3: 在使用MyBatis时,可以通过resultMap来定义数据库查询结果与Java对象之间的映射关系。下面是使用MyBatis创建resultMap映射的步骤: 1. 在MyBatis的配置文件,定义一个resultMap元素。 <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> </resultMap> 这里的id指定了主键字段,并用property指定了Java对象的属性,column指定了数据库表字段。 2. 在Mapper接口使用@ResultMap注解引用上面定义的resultMap。 @Select("SELECT * FROM user WHERE id = #{id}") @ResultMap("userMap") User getUserById(int id); 这样,在使用getUserById方法进行数据库查询时,MyBatis会根据resultMap的定义自动将查询结果映射为User对象。 使用MyBatis获得session的过程如下: 1. 在MyBatis的配置文件,配置数据源和相关参数。 <dataSource type="com.alibaba.druid.pool.DruidDataSource"> ... </dataSource> 2. 在配置文件配置SqlSessionFactoryBean。 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> 这里通过dataSource属性指定数据源,mapperLocations属性指定Mapper接口XML配置文件所在路径。 3. 在配置文件配置SqlSessionTemplate。 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> <constructor-arg index="1" value="BATCH"/> </bean> 这里通过构造函数参数注入SqlSessionFactory。 4. 在子类注入SqlSessionTemplate。 @Resource private SqlSessionTemplate sqlSessionTemplate; 这样就可以在代码使用sqlSessionTemplate进行数据库操作,例如:User user = sqlSessionTemplate.selectOne("getUserById", 1); 这样可以通过MyBatis的映射配置和session操作来实现数据库查询和持久化等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值