1. MySQL数据库默认使用的是美国的时区,而我们连接的时候用的是中国的北京时间,然后比美国晚上8个小时,所以当我们在连接数据库的时候要设置一下时区为东八区

ServerTimezone=UTC

  1. 驱动类
  • MySQL5.x的版本使用的驱动类是com.mysql.jdbc.Driver
  • MySQL8.x的版本使用的驱动类是com.mysql.cj.jdbc.Driver

    要在pom.xml文件中添加依赖。
    数据库驱动依赖

    1. <dependency>
    2. <groupId>mysql</groupId>
    3. <artifactId>mysql-connector-java</artifactId>
    4. </dependency>

    数据库连接池依赖

    1. <dependency>
    2. <groupId>com.alibaba</groupId>
    3. <artifactId>druid-spring-boot-starter</artifactId>
    4. <version>1.1.9</version>
    5. </dependency>

    添加Mybatis的依赖

    1. <dependency>
    2. <groupId>org.mybatis.spring.boot</groupId>
    3. <artifactId>mybatis-spring-boot-starter</artifactId>
    4. <version>2.0.1</version>
    5. </dependency>

    Application启动类添加扫描Dao注解

    1. @MapperScan("com.huojuhao.dao")

    application.properties添加配置

    1. #mapper.xml文件地址
    2. mybatis.mapper-locations=classpath:mapper/*Mapper.xml
    3. #设置别名
    4. mybatis.type-aliases-package=com.huojuhao.pojo

    mybatis XML配置方式

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <!--指定mybatis 使用指定日志-->
    7. <settings>
    8. <setting name="logImpl" value="slf4j"/>
    9. </settings>
    10. <environments default="development">
    11. <environment id="development">
    12. <transactionManager type="JDBC"/>
    13. <dataSource type="POOLED">
    14. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
    15. <property name="url" value="jdbc:mysql://localhost:3306/shiro"/>
    16. <property name="username" value="root"/>
    17. <property name="password" value="root"/>
    18. </dataSource>
    19. </environment>
    20. </environments>
    21. <mappers>
    22. <!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
    23. <mapper class="com.huojuhao.mapper.UsersMapper"/>
    24. </mappers>
    25. </configuration>

    xml Mapper文件方式

    1. @Mapper
    2. public interface StudentDao {
    3. //@Select("select * from Student where stu_no=#{id}")
    4. public Student getStudentById(int id);
    5. }

    ```java <?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  1. 多对一结果映射 <br />嵌套结果
  2. ```xml
  3. <association property="dept">
  4. <result column="deptname" property="name"/>
  5. </association>

支持懒加载的分步
嵌套查询

  1. <association property="dept" column="dept_id" select="com.huojuhao.mapper.DeptMapper.findById"/>

一对多结果映射
使用<collection property=”emps” ofType=”Emp/>标签 ofType=>List的泛型

image.png

image.png
动态sql
标签

  • if判断标签
  • 代替了 1=1 解决了 多个参数动态拼接 “and” 的问题
  • 类似 switch语句 满足哪个 就执行哪个 都不满足则执行标签 条件取其一
  • 循环标签 传入集合或数组 <foreach collection=”集合名” item=”单个元素名” separator=”分隔符 一般用’,’ open=”条件前面拼接” close=”条件后面拼接 “> #{item}
  • 用于update语句 前面自动添加set 后面自动去掉最后一个 ‘,’
  • 在mapper映射文件上下文声明一个变量
  • 定义sql片段 id 唯一标识 配合 refid 需要引用的sql片段id 引用使用 可以抽出公共的mysql语句 重复使用
    • 实现模糊查询 like ‘%XX%’
  • 1.可以使用mysql的字符串拼接 — 空格拼接 — concat函数拼接
  • 2 可以拼接好再传进来
  • 3 使用bind再Mapper映射文件上下文声明一个变量 ```xml select * form 表名 1=1 <if test=”id!=null and id!=’’> and id=#{id} 同

select * form 表名

<if test=”id!=null and id!=’’> and id=#{id} ``` image.png

条件构造器

image.png

模糊查询

image.png

排序查询

image.png
image.png
SpringBoot集成Mybatis - 图8

MyBatis注解
  1. @TableLogic 添加在实体类的字段上 删除时会执行逻辑删除 设置字段值为1 查询时会自动添加筛选条件 删除字段值 = 0