前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis框架:第七章:注解使用方式和参数传递及#{}和${}

MyBatis框架:第七章:注解使用方式和参数传递及#{}和${}

作者头像
马克社区
发布2022-10-08 15:53:52
2110
发布2022-10-08 15:53:52
举报
文章被收录于专栏:高端IT高端IT

myBatis的注解使用方式(了解。主要使用xml) 注解的使用示例:

public interface UserMapperAnnotation {

@Select(“select id,last_name userName ,sex from t_user where id = #{id}”) public User selectUser(int id);

@Select(“select * from t_user”) public List selectUserList();

@Update(“update t_user set last_name = #{lastName}, sex = #{sex} where id = #{id}”) public int updateUser(User user);

@Delete(“delete from t_user where id = #{id}”) public int deleteUserById(int id);

@Insert(“insert into t_user(last_name,sex) values(#{lastName},#{sex})”) @SelectKey(before = false, keyProperty = “id”, resultType = Integer.class, statement = { “select last_insert_id()” }) public int insertUser(User user);

}

mybatis-config.xml配置文件中导入

代码语言:javascript
复制
<mappers>
	<mapper class="com.dao.UserMapperAnnotation"/>
</mappers>
123

mybatis的参数传递 1.一个普通数据类型 当一个方法中只有一个普通数据类型。在mapper配置文件中可以使用#{}占位符来进行占位输出。 #{} 占位符中,可以写参数的 #{变量名}。 也可以写 #{value}。

方法: public int deleteUserById(int id);

#{变量名}

代码语言:javascript
复制
<delete id="deleteUserById" parameterType="int">
	delete from t_user where id = #{id}
</delete>

#{value}

<delete id="deleteUserById" parameterType="int">
	delete from t_user where id = #{value}
</delete>
123456789

2.多个普通数据类型 多个普通的参数。当我们需要使用 #{} 占位输出的时候,可以使用 param1,param2 …… paramN 也就是 #{param1} …… #{paramN}

或者使用@Param命名参数

使用param1、param2 …… paramN 占位输出参数 方法:

public List findUserByNameAndSex(String username, int sex);

使用param1、param2 …… paramN 的方式 占位输出参数

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120472401

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档