Java

springboot(二):集成mybatis

2018-09-03  本文已影响0人  修远路

1.pom文件添加 mysql(此处以mysql数据库为例)和mybatis依赖

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

2.数据库连接及映射等配置

mybatis:
  # 数据库mapper.xml映射位置
  mapper-locations: classpath*:/mapper/**Mapper.xml
  # 映射model别名
  type-aliases-package: com.sample.springboot.model

spring:
  datasource:
    # 数据库用户名
    username: root
    # 数据库密码
    password: 1234
    # 数据库url
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
    # 数据库驱动
    driver-class-name: com.mysql.jdbc.Driver

3.业务实现类注解@Serivce
具体牵涉事务在特定方法上添加 @Transactional(rollbackFor = Exception.class)

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void deleteByPrimaryKey(String id) {

    mapper.deleteByPrimaryKey(id);
  }

[附]git源码地址
https://github.com/SaltzmanAlaric/springboot-mybatis

上一篇下一篇

猜你喜欢

热点阅读