springboot + mybatis 开启驼峰命名转换失效

Ethan9987 2019-05-31 12:23:10
网上查到的三种 配置文件、配置类都试过没有效果。。
数据字段名是 stu_id stu_name 这种,
实体类:

@Data
public class Student {
private Integer stuId;
private String stuName;
private String stuPassword;
private String stuPhone;
private String stuEmail;
private Date stuBirthday;
private String stuInfo;
}

映射文件:

@Mapper
@Repository("studentMapper")
public interface StudentMapper {

@Select("select * from tb_stu where stuId = #{stuId}")
Student getStudentById(Integer stuId);
。。。
}


application.yml:

mybatis:
configuration:
map-underscore-to-camel-case: true

配置类方式配置:

@MapperScan(basePackages = "com.cangwu.wecoding.mapper")
@org.springframework.context.annotation.Configuration
public class MybatisConfig {

/**
* 配置驼峰命名法
* @return
*/
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return (Configuration configuration) -> configuration.setMapUnderscoreToCamelCase(true);
}

@Bean(name="sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("druidDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setVfs(SpringBootVFS.class);
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}
}


@Test
public void getOne() {
Student one = studentService.getOne(1);
Assert.assertEquals("dalao",one.getStuName());
}


都试过。。但是映射文件里填 stu_id 就能测试成功 填stuId报错:
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column 'stuId' in 'where clause'
### The error may exist in com/cangwu/wecoding/mapper/StudentMapper.java (best guess)
### The error may involve com.cangwu.wecoding.mapper.StudentMapper.getStudentById-Inline
### The error occurred while setting parameters
### SQL: select * from tb_stu where stuId = ?
### Cause: java.sql.SQLSyntaxErrorException: Unknown column 'stuId' in 'where clause'
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'stuId' in 'where clause'

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'stuId' in 'where clause'

...全文
970 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ethan9987 2019-06-01
  • 打赏
  • 举报
回复
引用 1 楼 getdate 的回复:
理论上映射文件上就是写的表中定义的列,也就是和数据库定义是一致的,所以,你把列stu_id,写成 stuId,应该是错误的.
我弄混淆了 我还以为映射文件这里也可以映射成驼峰命名法呢
Ethan9987 2019-06-01
  • 打赏
  • 举报
回复
引用 2 楼 chirenshuomeng1 的回复:
是“where stuId = #{stuId}”有错啊 改成
where stu_Id = #{stuId}
就行了吧,SQL语句中还是要用数据库中定义的名字,使用java变量时就用实体定义的名字
不好意思 我明白了 我把这个弄混淆了
Ethan9987 2019-06-01
  • 打赏
  • 举报
回复
引用 2 楼 chirenshuomeng1 的回复:
是“where stuId = #{stuId}”有错啊 改成
where stu_Id = #{stuId}
就行了吧,SQL语句中还是要用数据库中定义的名字,使用java变量时就用实体定义的名字
stu_id 直接用数据库中的名字是可以用啊,但是开启驼峰命名不就是为了让stuId生效嘛,难道是我的用法不对?
颓宝 2019-05-31
  • 打赏
  • 举报
回复
是“where stuId = #{stuId}”有错啊 改成
where stu_Id = #{stuId}
就行了吧,SQL语句中还是要用数据库中定义的名字,使用java变量时就用实体定义的名字
getdate 2019-05-31
  • 打赏
  • 举报
回复
理论上映射文件上就是写的表中定义的列,也就是和数据库定义是一致的,所以,你把列stu_id,写成 stuId,应该是错误的.

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧