用的mysql数据库,原表结构:
id int,
name varchar(10),
age int,
role char(20)
@Test
public void findBySql(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml");
UserDao dao = ac.getBean("userDao", UserDao.class);
String sql="select name,age,role from tbl_user_info";
List<Object[]>list=dao.findAllBySql(sql);
System.out.println("*role1*:"+list.get(0)[2]);
System.out.println("*role2*:"+list.get(1)[2]);
System.out.println("*role3*:"+list.get(2)[2]);
}
当role字段设置为定长字符char(20)型测试时,控制台输出只有字段首字母:
*role1*:p
*role2*:d
*role3*:d
当把role字段改为可变字符varchar(20)型测试时,控制台输出了完整字段:
*role1*:professor
*role2*:doctor
*role3*:database
查找资料,原来是因为:char类型在java中只占2个字节即1个字符大小,所以数据库里的char类型字段传过来后保持类型不变,就只能保留住一个字符了。
解决方法:
1、修改SQL为:select name,age,trim(role) from tbl_user_info;
2、或者将数据库里的字段类型修改为varchar型。