SpringBoot整合MyBatis(xml文件内书写sql)
一、添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><..
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MyBatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、配置文件application.yml
server:
port: 8080
servlet:
context-path: /mybatis
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootofmybatis?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
username: root
password: xxx@123
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
mybatis:
type-aliases-package: com.example.mybatis.entity
mapper-locations: classpath*:mapper/*.xml
configuration:
cache-enabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
对于配置文件需要说明的有以下几点:
1、指定扫描xml目录使用mybatis.mapper-locations;
2、mybatis.configuration.cache-enabled是是否启动mybatis的二级缓存,mybatis.configuration.log-impl的配置是在控制台打印mybatis具体执行的sql语句详情;
3、要在控制台打印日志,不一定非要使用mybatis.configuration.log-impl还可以在配置文件中使用下面方式,其中com.example.mybatis.mapper指定的是mapper层代码所在的包目录,注意不是xml所在目录,是mapper层接口代码所在目录
logging:
level:
com.example.mybatis.mapper: debug
三、创建项目目录结构和mapper层对应xml文件所在目录。
注意xml所在文件已经在配置文件中指定,要创建在resource下的mapper文件夹下,各级目录如图所示
四、注意
mapper层接口要使用注解@Mapper
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)