Docsify/springboot/chapter5-整合MyBatis.md
2023-06-13 16:37:18 +08:00

1.6 KiB
Raw Blame History

整合MyBatis

  • 首先在pom.xml中配置mybatis的依赖,MySql连接库
<!--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>
  • 然后在application.properties中添加数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot2Test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
spring.datasource.password=root
spring.datasource.username=xiaoyan159
# 注意注意
mybatis.mapper-locations=classpath:com/xiaoxiao/springboot2/dao/*.xml
#mybatis.mapper-locations=classpath:mapper/*.xml        #这种方式需要自己在resources目录下创建mapper目录然后存放xml
mybatis.type-aliases-package=com.xiaoxiao.springboot2.pojo #这里配置mybatis中别名自动生成的包名
# 驼峰命名规范 如:数据库字段是  order_id 那么 实体字段就要写成 orderId
mybatis.configuration.map-underscore-to-camel-case=true

注意! 如果配置的mybatis.mapper-locations同dao包一致在打包时默认可能不会将xml文件打包进jar中需要在pom.xml文件中特殊声明

<resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>