6.整合mybatis-自动生成

pom中加入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

纯注解方式:

mapper持久层

package com.lcywings.sbt.mapper;

import com.lcywings.sbt.beans.DocEntry;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * @author : Lcywings
 * @date : 2021/7/26 9:11
 * @acl : true
 * @description : 使用注解版实现数据操作
 */
@Mapper
public interface DocEntryMapper {

    /**
     * @author : Lcywings
     * @date : 2021/7/26 9:12
     * @acl : true
     * @description : 根据电子文档编号,查电子文档详情
     */
    @Select("select * from edoc_entry where id = #{id}")
    DocEntry queryDocEntryById(Integer id);
}

yml配置:

# jdbc连接
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/edocdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

# mybatis核心配置
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  # config-location: classpath:mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

xml方式:

mapper持久层

/**
 * @author : Lcywings
 * @date : 2021/7/26 10:07
 * @acl : true
 * @description : 添加电子文档详情
 */
int insertDocEntry(DocEntry docEntry);

mybatis-config.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>

mapper.xml

<?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">

<mapper namespace="com.lcywings.sbt.mapper.DocEntryMapper">
    <!--int insertDocEntry(DocEntry docEntry);-->
    <insert id="insertDocEntry">
        insert into edoc_entry(`cid`, `title`, `summary`, `uploadUser`)
        values (#{cid}, #{title}, #{summary}, #{uploadUser})
    </insert>
</mapper>

插件写法(better-mybatis-generator)

database中+,datasources选mysql,选中表右键genneator

使用EdocEntryExample做查询

/**
 * @author : Lcywings
 * @date : 2021/7/26 11:43
 * @acl : true
 * @description : 查询电子文档列表
 */
@GetMapping("/edocEntrys")
List<EdocEntry> edocEntrys() {
    //创建查询条件对象
    EdocEntryExample edocEntryExample = new EdocEntryExample();

    //不去重
    edocEntryExample.setDistinct(false);

    //排序
    edocEntryExample.setOrderByClause(" id desc ");

    // 摘要中,带有一的文档,添加查询条件对象
    EdocEntryExample.Criteria criteria = edocEntryExample.createCriteria();
    criteria.andSummaryLike("%一%");

    // 执行查询,获取列表
    List<EdocEntry> edocEntries = edocEntryService.queryEdocEntrys(edocEntryExample);

    // 多个连接条件
    return edocEntries;
}

Q.E.D.