81,095
社区成员
发帖
与我相关
我的任务
分享
@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);
。。。
}
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());
}
where stu_Id = #{stuId}
就行了吧,SQL语句中还是要用数据库中定义的名字,使用java变量时就用实体定义的名字