springboot+mybatis+mysql项目中遇到的问题:mybatis中文件如何正确传递字符串参数的问题。

关于mybatis中如何正确传递字符串参数的问题
问题描述:
在用mybatis时想写一个功能,通过用户名查找到相应的用户,但是失败了很多次,研究了很久,终于解决,这里贴出解决方案。

详细解决步骤:
在学习mybatis的时候,参照网上的例子写了一个小demo,根据id从mysql里面查找用户信息。当时是利用一个与User应的接口IUser描述查询语句的参数类型和返回值,这种类型相比于用User.xml写一个mapper的方式简单一些。

package com.xiao.dao;
 
import com.xiao.bean.User;
import org.apache.ibatis.annotations.Select;
public interface IUser {
 
 @Select("select * from user where id = #{id}")
 public User getUserByID(int id);
}

这里是我的表的结构

在这里插入图片描述

Main方法里的语句

IUser iUser = session.getMapper(IUser.class);
User user = iUser.getUserByID(3);
System.out.println("名字:"+user.getName());
System.out.println("年龄:"+user.getAge());
System.out.println("性别:"+user.getSex());
System.out.println("学校:"+user.getSchool());

可以很容易查出ID为3的用户信息。

在这里插入图片描述

但是当我想将查询的条件换成姓名的时候,于是我顺理成章的在上面的代码加上了

package com.xiao.dao;
 
import com.xiao.bean.User;
import org.apache.ibatis.annotations.Select;
public interface IUser {
 
 @Select("select * from user where id = #{id}")
 public User getUserByID(int id);
 @Select("select * from user where name = #{name}")
 public User getUserByName(String name);
}

结果查询不到用户。

上网百度了很久,加上用log4J打印了日志(关于如何用log4J打印sql执行语句请参考这篇文章http://843977358.iteye.com/blog/2259796 )。

发现执行的sql语句虽然是对的,但是还是查询不到结果。之后我添加了一个叫peter的用户发现能查到该用户的信息。于是知道是编码问题。

需要在mybatis配置文件mybatis-config.xml中加上一句

<property name="url" value="jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8"/>

设置编码格式为“utf8”即可用中文进行查询。

mybatis参数为字符型的查询总结
众所周知,用mybatis查询有两种方式,一种是写xml映射文件,一种是写接口映射文件,这两种方式都可以进行字符串参数的查询。参数的填写以也有两种方式#{name}和${name}。这两种方式是有区别的,具体区别请自行百度。
如果用的是 #{name} 这种方式

<select id="GetUserByName" parameterType="java.lang.String" resultType="User">
         SELECT * FROM user WHERE name = #{name}
    </select>

或者

 @Select("select * from user where name =#{name}")
 public User getUserByName(String name);

参数都可以直接传递该字符串,即

	IUser iUser = session.getMapper(IUser.class);
    User user = iUser.getUserByName("孙尚香");
   System.out.println("名字:"+user.getName());
   System.out.println("年龄:"+user.getAge());
   System.out.println("性别:"+user.getSex());
   System.out.println("学校:"+user.getSchool());

如果用的是 n s m e 这 种 方 式 , 注 意 要 将 {nsme}这种方式,注意要将 nsme{name}写成${_parameter},并且向其中传递字符串的时候要在前后加上单引号

<select id="GetUserByName" resultType="User" parameterType="java.lang.String">
        SELECT * FROM user WHERE name = ${_parameter}
 
    </select>

@Select("select * from user where name =${_parameter}")
 public User getUserByName(String name);

传参时要在前后加上单引号

User user = (User) session.selectOne(
                    "com.xiao.bean.xml.UserMapper.GetUserByName","'孙尚香'");
            if (user != null) {
                String userInfo = "名字:" + user.getName() + ", 年龄:" + user.getAge() + ", 性别" + user.getSex() + ",学校" + user.getSchool();
                System.out.println(userInfo);
            }

然后就可以愉快的查询啦

http://www.tofacebook.com/jinjiezhimei.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值