xml方式在编写复杂SQL时,更适合
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.1.10version>
- dependency>
-
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>1.3.2version>
- dependency>
- //编写实体类
- @Data
- public class User implements Serializable {
- private Integer id;
- private String name;
- private Integer age;
- private String gender;
- }
1.接口
2. 在启动类中添加直接,扫描Mapper接口所在的包
3. 准备映射文件
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.qianfeng.day1124.dao.UserDao">
-
-
- <select id="getAll" resultType="User">
- select * from user
- select>
-
- mapper>
4. yml文件
- server:
- port: 8080
-
-
-
- mybatis:
- mapper-locations: classpath:mappers/*.xml
- # 配置别名扫描的包
- type-aliases-package: com.qianfeng.day1124.entity
-
-
-
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql:///crm?serverTimezone=Asia/Shanghai
- username: root
- password: 123456
测试启动类测试
注解方式在编写配置简单,简单SQL推荐使用
添加Mybatis注解
针对增删改查:@Insert,@Delete,@Update,@Select
还是需要在启动类中添加@MapperScan注解
添加配置
- // yml文件
- logging:
- level:
- com.qf.firstspringboot.mapper: DEBUG
application.yml
- server:
- port: 8080
-
-
-
- mybatis:
- mapper-locations: classpath:mappers/*.xml
- type-aliases-package: com.qianfeng.day1124.entity
-
-
-
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql:///crm?serverTimezone=Asia/Shanghai
- username: root
- password: 123456
- type: com.alibaba.druid.pool.DruidDataSource
- thymeleaf:
- mode: HTML #thymeleaf 的模板模型
- cache: false #不适用缓存
- encoding: UTF-8 #编码
- prefix: classpath:/templates/ #前缀
- suffix: .html #后面
-
-
-
- logging:
- level:
- com.qianfeng.day1124.dao: debug
-