欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Spring Boot + H2 + MyBatis: 数据库事务设置指南

最编程 2024-07-20 20:04:00
...

参考:https://github.com/liuhongdi/h2test

1. 配置

#error
server:
  error:
    include-stacktrace: always
#errorlog
logging:
  level:
    org.springframework.web: trace
#mysql
spring:
  h2:
    console:
      path: /h2-console #进入h2 web操作界面的路径
      enabled: true #开启h2 web界面
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/*.sql
    data: classpath:db/data_h2*.sql
    url: jdbc:h2:mem:test
    username: root
    password: test
#mybatis
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.***.dao, com.***.mapper
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2. 将mysql 语法转化为 H2语法

    public static void mysql转化为H2(String file) throws Exception {
        File resource = ResourceUtils.getFile(file);
        String content = FileUtils.fileRead(resource.getPath());

        content = "SET MODE MYSQL;\n\n" + content;

        content = content.replaceAll("`", "");
        content = content.replaceAll("ENGINE = InnoDB CHARACTER SET.*;", ";");

        content = content.replaceAll("ENGINE = InnoDB", "");
        content = content.replaceAll("CHARACTER SET = utf8", "");
        content = content.replaceAll("CHARACTER SET utf8", "");
        content = content.replaceAll("ROW_FORMAT = COMPACT", "");
        content = content.replaceAll("COLLATE = utf8_general_ci.*", "");
        content = content.replaceAll("COLLATE utf8_general_ci", "");
        content = content.replaceAll(" USING BTREE", "");
        content = content.replaceAll("COLLATE.*(?=D)", "");
        content = content.replaceAll("\\).*ENGINE.*(?=;)", ")");
        content = content.replaceAll("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", " AS CURRENT_TIMESTAMP");
        FileUtils.fileWrite(resourceFile.getPath(), content);
    }

启动spring 的 DemoApplication

@SpringBootApplication(scanBasePackages = {"包路径"})
@MapperScan(basePackages = {"mapper的包路径"})
public class TestApplication {
    public static void main(String[] args) throws Exception {
        // 调用 
        mysql转化为H2();
        SpringApplication.run(DemoApplication.class, args);
    }
}

3. 查看

http://localhost:8080/h2-console
输入密码

4. 事务

配置事务bean

@Configuration
public class MyBean {
    // @Bean
    // public JdbcTemplate createJdbcTemplate() {
    //     JdbcTemplate template = new JdbcTemplate();
    //     template.setDataSource(dataSource());
    //     return template;
    // }


    @Bean(destroyMethod = "shutdown")
    public EmbeddedDatabase dataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:db/*.sql")
            .addScript("classpath:db/data_h2*.sql")
            .build();
    }

    @Bean(name = "transaction")
    public PlatformTransactionManager transaction() {
        return new DataSourceTransactionManager(dataSource());
    }
}

配置了事务bean后,无法再访问管理台,暂时没有定位到原因,可能是jdbc.url 错误。

推荐阅读