easy code plugin

Ah, this is really awesome. It automatically generates code. The first time I saw the code generator, I was a little excited to
download it in IDEA-setting-plus and search for easy code. Install and restart, configure the database

This can only be used for mybatis, not for JPA

1. Connect to the database and generate with one click

Insert picture description here
Insert picture description here

  • Right-Easy Code-Genertate Code,

Insert picture description here
Then, the code is automatically generated
Insert picture description here
within the red box.

2. Attention

To run, add @Mapperon the DAO layer or in the main program

@MapperScan("com.qiang.springbootvue.dao"), Which scans the dao layer package

3. Configure the data source

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/jdbc?library=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    initialization-mode: always

mybatis:
  mapper-locations: classpath:/mapper/*Dao.xml
  • The last one needs to be imported, otherwise the mapper layer cannot be read
    Insert picture description here

4. Talk about the specific process

  • It has an entity class entity.
  • A dao layer (interface class), this dao layer is equivalent to the mapper layer, but this interface is all written with method names, because the configuration file version does not write annotations and writes xml. So the followingmapper/xxxDao.xml I wrote the specific SQL statements in the dao layer, and the xml of mybatis is still learning
  • Then there is the service layer. I thought the previous service layer was just a class. But the big guy said that the service layer of a complex project is all of the service interface + ServiceImpl class. The service interface writes the method name, and the ServiceImpl class calls the dao layer method.
  • A controller layer for receiving requests and returning data.
  • mapper/xxxDao.xml, still learning grammar

The following are the specific conditions of this example

1. BookDao


/**
 * (Book)表数据库访问层
 *
 * @author makejava
 * @since 2020-08-16 15:52:32
 */
public interface BookDao {
    
    

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Book queryById(Integer id);
    }
  • This interface is really just a name. .

2、BookDao.xml

Write specific SQL statements

<?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.qiang.springbootvue.dao.BookDao">

<!--    局部变量-->
    <resultMap type="com.qiang.springbootvue.entity.Book" id="BookMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="author" column="author" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
<!--   如果resultMap是基本类型,就不用写resultMap -->
    <select id="queryById" resultMap="BookMap">
        select
          id, name, author
        from library.book
        where id = #{id}
    </select>
</mapper>
  • Line 1.2 is copied
  • The third line says that this xml is the mapper of BookDao
  • The next step is the local variables. The resultMapreturned types are all Book classes. Take the name BookMap so that you don’t need to type a lot of things.
  • propertyIs the attribute of the java object, columnis the attribute jdbcTypeof the table , is the attribute type of the table
  • select id="queryById", This id corresponds to the method name of the BookDao layer, the Dao layer writes the return type as Book, here resultMap writes Book

3、BookService

An interface


/**
 * (Book)表服务接口
 *
 * @author makejava
 * @since 2020-08-16 15:52:34
 */
public interface BookService {
    
    

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Book queryById(Integer id);
}

4、BookServiceImpl

Specific method of writing interface

@Service("bookService")
public class BookServiceImpl implements BookService {
    
    
    @Resource
//   @Resource 注解和 @Autowired 一样,只不过@Autowired是spring的,@Resource是Java自带的
    private BookDao bookDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Book queryById(Integer id) {
    
    
        return this.bookDao.queryById(id);
    }
}
  • @ServiceIndicates that it is the Service layer
  • @Resource annotation is the same as @Autowired, except that @Autowired is spring and @Resource comes with Java
  • Method of calling Dao layer

5、BookController

@RestController
@RequestMapping("book")
public class BookController {
    
    
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Book selectOne(Integer id) {
    
    
        return this.bookService.queryById(id);
    }
}

Traditional arts, regular operations

Guess you like

Origin blog.csdn.net/yi742891270/article/details/108039614