SpringCloud project construction (four)

Integrated persistence layer framework MyBatis

Why use MyBatis instead of Hibernate as the persistence layer framework? Compared with Hibernate, what are the advantages of MyBatis?

  1. When creating a database, you need to pay attention to setting UTF-8. UTF-8 in a mysql database is three bytes, and the supported characters are limited. The encoding of utf8mb4 was added after 5.5.3, which supports more characters, such as emoticons.Insert picture description here
  2. Create a library in the link. The library I created here is named ** "frist_initializr" ** Since my authority is root, I can see all the libraries. In order to see only the specified library, a new database user can be created.
    @localhost means that it can only be accessed from the local machine
    @% means that it can be accessed from the external network,
    Insert picture description hereInsert picture description here
    and dependencies need to be imported in the code
  <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

At the same time, database configuration and mybatis configuration are required in application.yaml

mybatis:
  type-aliases-package: com.young.system.domain
  mapper-locations: classpath:mapper/*.xml
spring:
  application:
    name: System
  datasource:
    url: jdbc:mysql://localhost:3306/frist_initializr?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT #配置数据库连接路径
    username: frist_initializr
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver #配置数据库驱动

Next, you need to set the path for parsing the mapper at startup. The above yaml also needs to be configured.

@MapperScan("com.young.system.mapper")
public class SystemApplication {
    
    
}

Set the mapper parsing path in yaml to classpath:mapper/*.xml, so create mapper
mapper.xml syntax in the resouces folder

<?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.young.system.mapper.TestMapper">
    <select id="list" resultType="com.young.system.domain.Test">
        select * from test
    </select>
</mapper>

The bean is set in the resultType class, so an object of the test table needs to be created under this path.
At the same time, the class in the path is specified in the namespace. At the same time, you need to create a list method in this class.

package com.young.system.mapper;

import com.young.system.domain.Test;

import java.util.List;

public interface TestMapper {
    
    

   public List<Test> list();
}

Service layer to execute these methods

package com.young.system.service;

import com.young.system.domain.Test;
import com.young.system.mapper.TestMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestService {
    
    

    @Resource
    private TestMapper testMapper;

    public List<Test> list() {
    
    
        return testMapper.list();
    }
}

Controller layer business code

package com.young.system.controller;

import com.young.system.domain.Test;
import com.young.system.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {
    
    

    @Resource
    private TestService testService;

    @GetMapping("/test")
    public List<Test> list(){
    
    
        return testService.list();
    }
}

Error found during configuration

  1. Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
    解决方式:driver-class-name:** com.mysql.cj.jdbc.Driver **
    URL 也需要重新设置
    url: jdbc:mysql://localhost:3306/frist_initializr?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
  2. Cause: org.apache.ibatis.builder.BuilderException: Mapper's namespace cannot be empty means that the mapper cannot be found. So I searched in application.yaml to see if the specified path was not set, and came in and found that an error example has been written
    :
mybatis:
  mapper-locations:  classpath:/mapper/*.xml

Correct example:

mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml

3.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘test’’ at line 1

<?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.young.system.mapper.TestMapper">
    <select id="list" resultType="com.young.system.domain.Test">
        select * from 'test'
    </select>
</mapper>

Current writing

<?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.young.system.mapper.TestMapper">
    <select id="list" resultType="com.young.system.domain.Test">
        select * from test
    </select>
</mapper>

Guess you like

Origin blog.csdn.net/weixin_42789301/article/details/107267248