springboot整合mybatis关键部分

依赖的jar包

1,检查parent


<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

2,mysql数据库的驱动包

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>

如果没有指定版本,如5.1.4等默认,载入最新的包,目前是8.0.19,对应的mysql的driver类是 com.mysql.cj.jdbc.Driver。比较早的5.x版的jar包对应的driver类是com.mysql.jdbc.Driver。

3,mybatis的jar包

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

<!-- pagehelper -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.5</version>
</dependency>


yml 配置文件

spring:
  datasource:
    username: root
    password: root123
    url: jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver  # 6.0 版本以上

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml 
  type-aliases-package: com.example.demo.entity

# pagehelper   
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
  • mapper-locations : 用来指定*mapper.xml文件的位置
  • type-aliases-package : 用来指定实体类所在的包
  • driver-class-name :6.0 以上版本用 com.mysql.cj.jdbc.Driver ,5.x 版本用 com.mysql.jdbc.Driver。

主启动类注解

@MapperScan(basePackages = {"com.example.demo.mapper"})
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

mybatis 还有一个@Mapper注解,但是这里使用了@MapperScan,所以@Mapper这个注解就可以不用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值