spring-boot 整合 mybatis

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44018093/article/details/88798576

在 pom.xml 中导入坐标

<!--mybatis-->
	<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.46</version>
		</dependency>
<!--连接池-->
       <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>


  
  

第一种: application.properties 配置

# mybatis 配置
#使用mybatis配置文件,需要指定该文件的文件路径
#指定mapper.xml文件的路径,如果mapper.xml与接口在一起则不需要该配置
#扫描pojo包中的实体类并起别名
#日志级别改为debug可以显示sql语句,logging.level后为存放mapper接口的包
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.mr.pojo
logging.level.com.mr.mapper=debug
# 开启驼峰命名法 mybatis.configuration.map-underscore-to-camel-case: true
mybatis.configuration.map-underscore-to-camel-case=true

#配置数据源
#德鲁伊 连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=ws
spring.datasource.password=1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

必须加?号后内容 北京时间东八区

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/four?serverTimezone=GMT%2B8

第二种 application.yml 配置方法(选其一即可)

# mybatis 配置
#使用mybatis配置文件,需要指定该文件的文件路径
#指定mapper.xml文件的路径,如果mapper.xml与接口在一起则不需要该配置
#扫描pojo包中的实体类并起别名
#日志级别改为debug可以显示sql语句,logging.level后为存放mapper接口的包
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.mr.domain
  configuration:
    map-underscore-to-camel-case: true
logging:
  level:
    com:
      mr:
        mapper: debug
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    username: ws
    password: 1
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/four?serverTimezone=GMT%2B8

  
  

mapper接口应要添加注解@Mapper,不然会找不到你的mapper,或者在启动类上添加@MapperScan(“com.mr.mapper”)

需要删除修改新增的话需要在service层上方添加相应的事务注解@Transaction

猜你喜欢

转载自blog.csdn.net/weixin_44018093/article/details/88798576