【Java】数据库mapper与实体字段对应,SpringBoot开启驼峰映射

SpringBoot开启驼峰映射

主要的属性设置

 mybatis.configuration.mapUnderscoreToCamelCase=true或mybatis.configuration.map-underscore-to-camel-case=true

1,引入了mybatis-spring-boot-starter的项目,可以直接去配置参数

官方文档链接:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/


   org.mybatis.spring.boot
   mybatis-spring-boot-starter
   1.1.1

可以在application.yml配置

mybatis: 

    configuration:

        map-underscore-to-camel-case: true

或者在spring boot项目中没有mybatis.xml文件,配置文件application.properties中,加入配置项:

#mybatis配置 #指定mapper.xml的位置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.mapUnderscoreToCamelCase=true
mybatis.configuration.useColumnLabel=true

2,手动通过javaBean的方式配置,配置SqlSessionFactory

@Configuration
@EnableTransactionManagement
// 开启注解事务支持
public class MybatisConfig implements TransactionManagementConfigurer {

   @Autowired
   private DataSource dataSource;

   @Bean(name = "sqlSessionFactory")
   public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(dataSource);
      bean.setTypeAliasesPackage("com.cmb.model");

      org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
      configuration.setUseGeneratedKeys(true);//使用jdbc的getGeneratedKeys获取数据库自增主键值
      configuration.setUseColumnLabel(true);//使用列别名替换列名 select user as User
      configuration.setMapUnderscoreToCamelCase(true);//-自动使用驼峰命名属性映射字段   userId    user_id
      bean.setConfiguration(configuration);
      bean.setFailFast(true);


      Properties properties = new Properties();
      properties.setProperty("helperDialect", "postgresql");
      properties.setProperty("reasonable", "true");
      properties.setProperty("supportMethodsArguments", "true");
      properties.setProperty("params", "count=countSql");

      // 分页插件
      PageInterceptor pageInterceptor = new PageInterceptor();
      pageInterceptor.setProperties(properties);
      // 添加插件
      LogSqlInterceptor logSqlInterceptor = new LogSqlInterceptor();
      bean.setPlugins(new Interceptor[] { pageInterceptor, logSqlInterceptor });
      // 添加XML目录
      ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

      List resources = new ArrayList<>();
      // 添加 多个路径

      try {
         resources.addAll(Arrays.asList(resolver.getResources("classpath:mapper/**/*.xml")));
         bean.setMapperLocations(resources.toArray(new Resource[resources.size()]));

      } catch (Exception e) {
         e.printStackTrace();
         throw new RuntimeException(e);
      }
      return bean.getObject();
   }

   @Bean
   public SqlSessionTemplate sqlSessionTemplate(
         SqlSessionFactory sqlSessionFactory) {
      return new SqlSessionTemplate(sqlSessionFactory);
   }

   // 开启注解事务
   @Bean
   @Override
   public PlatformTransactionManager annotationDrivenTransactionManager() {
      return new DataSourceTransactionManager(dataSource);
   }

}

最后对于mapper.xml的配置可以 如下:1直接对应实体类;2不配置自动驼峰映射就自己写resultMap的映射


     
     

 

你可能感兴趣的:(Java)