Spring框架和Mybatis中@param的不同及其对应Xml

15 篇文章 0 订阅
6 篇文章 0 订阅
1. spring中@param
/**
* 查询指定用户和企业关联有没有配置角色
* @param businessId memberId
* @return
*/
int selectRoleCount(@Param("businessId") Integer businessId,@Param("memberId") Long memberId);
2. Mybatis中@param
/*
* 查询指定用户和企业关联有没有配置角色
* @param businessId memberId
* @return
*/
int selectRoleCount(@Param("businessId") Integer businessId,@Param("memberId") Long memberId);

<select id="selectRoleCount" resultType="java.lang.Integer" >
select count(tbm.id)
from t_business_member_relation tbm
where tbm.business_id = #{0,jdbcType=INTEGER}
and tbm.member_id = #{1,jdbcType=INTEGER}
and tbm.role_business_id isnot null
</select>
如上在xml文件中0(作为一个索引值)表示引用第一个@param注释参数businessId,1表示引用第二个@param注释参数memberId。
若使用Mybatis中@param则将0改为businessId,1改为memberId,即直接使用参数名进行引用。

注:如果Mapper.Java文件中引用的是Spring的

org.springframework.data.repository.query.Param;

但是Mapper.xml中使用的是mybatis 的用法,那么就会如下的错误



org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'businessI'
<resultMap id="BaseResultMap" type="实体路径">
    <id column="id" jdbcType="VARCHAR" property="id"/>//column对应数据库字段,property对应实体属性
    <result column="member_id" jdbcType="VARCHAR" property="memberId"/>//jdbcType表示实体属性类型
</resultMap>
<select id="对应dao层方法名" resultMap="BaseResultMap" parameterType="">
//resultMap查询返回类型 parameterType对应传入参数类型
    SELECT * FROM register WHERE member_id = #{memberId,jdbcType=VARCHAR} 
</select>


 
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面给出一个简单的用户登录案例,使用了Spring Boot框架MyBatis、MySQL数据库。 首先,需要在pom.xml文件添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> </dependencies> ``` 然后,在application.properties文件配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml ``` 接下来,创建一个User实体类: ```java @Data public class User { private Integer id; private String username; private String password; } ``` 然后,在resources目录下创建mapper/UserMapper.xml文件,编写SQL语句: ```xml <?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"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="userMap" type="com.example.demo.entity.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="findByUsernameAndPassword" resultMap="userMap"> select * from user where username=#{username} and password=#{password} </select> </mapper> ``` 然后,在com.example.demo.mapper包下创建UserMapper接口: ```java @Mapper public interface UserMapper { User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password); } ``` 接着,在com.example.demo.service包下创建UserService接口和实现类UserServiceImpl: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findByUsernameAndPassword(String username, String password) { return userMapper.findByUsernameAndPassword(username, password); } } ``` 最后,在com.example.demo.controller包下创建UserController: ```java @Controller public class UserController { @Autowired private UserService userService; @PostMapping("/login") @ResponseBody public String login(String username, String password) { User user = userService.findByUsernameAndPassword(username, password); if (user == null) { return "登录失败"; } else { return "登录成功"; } } } ``` 这样,我们就完成了一个简单的用户登录案例。需要注意的是,这里只是一个简单的示例,实际应用还需要进行参数校验、异常处理等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值