一、设置Mybatis中的实体类路径别名,以及通过druid配置数据库连接
注意:在设置驼峰式映射时,创建的Configuration应该选择org.apache的那个包,如下面全名
org.apache.ibatis.session.Configuration
二、设置映射的包路径
public class MybatisConfig {
//设置别名以及DataSource
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setTypeAliasesPackage("com.itheima.pojo");
sessionFactoryBean.setDataSource(dataSource);
//设置驼峰式映射
Configuration configuration = new Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactoryBean.setConfiguration(configuration);
return sessionFactoryBean;
}
//设置映射
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.itheima.dao");
return mapperScannerConfigurer;
}
}
说明:这里的 DataSource dataSource 是得自己配置要连接的数据库
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource druidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
这里通过配置文件引入可以不用修改代码,通过修改配置操作