Springboot-mybatis realizes addition, deletion, modification and query (1)

1. springboot integrates mybatis environment

  1. Create a springboot project.
    initial directory structure
    insert image description here

  2. Introduce dependencies in pom.xml (note refresh after adding)
    first introduce mybatis-springboot integration dependencies, database drivers, and database connection pools.
    Add it within the tag of the pom.xml file <dependencies>.

		<!--mybatis和springboot整合的依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        
        <!--连接mysql,数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <!--数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>

In addition, you can use the lombok plug-in, which can help us simplify the code through simple annotations, and let lombok automatically generate default getter/setter methods. Written in dependencies.

		<!--lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
  1. Build a package (new package)
    to build a dao layer under the src/main/java/personal folder
    : for data access
    pojo: to build an entity class (the entity class in java corresponds to a table in the database)
    controller: the control layer
    service: Call the dao layer to realize the operation on the database

  2. Create an entity class
    Create an entity class (class) User in the pojo layer, which implements a User table

  3. Database connection
    After creating the User class here, you can connect to the database to see what fields there are.
    Method: idea Click Database on the far right sidebar, click the plus sign, enter the user name and password, link to the database, and test the connection. If it is a database linked to a remote server, change the localhost and port number to the server address and port.
    insert image description here
    Then select a data table
    insert image description here
    The mybatis data table selected here is created in advance, and a piece of user information is stored in it, including id, name, and pwd.
    insert image description here

  4. pojo/User.java
    Return to the class in Section 4. The attributes of the entity class in Java are in one-to-one correspondence with the fields of the database.

public class User {
    
    
	private Integer id;
	private String name;
	private String pwd;
	private String perm;
}

Then outside the public

@Data
@AllArgsConstructor
//有参构造
@NoArgsConstructor
//无参构造
public class User {
    
    
	private Integer id;
	private String name;
	private String pwd;
	private String perm;
}

It uses lombok to directly build methods with parameters, without parameters, get, set, tostring, etc.
insert image description here
The entity class has been successfully built here.

  1. The configuration of the configuration file (src/resources/application.yml)
    insert image description here
    looks at the database version here;
    when integrating mybatis, create a new mybatis folder under resources to store all *.xml files, and then configuremapper-locations: classpath:/mybatis/*.xml
#端口
server:
  port: 8083

#数据库的驱动和url.
#url中的serverTimezone是对数据库时区的设置,8.0以上版本需要加入,而且8.0以后的mysql版本需要加入driver-class-name
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
    driver-class-name: com.mysql.cj.jdbc.Driver


#mybatis环境搭建.type-aliases-package是选择包,
mybatis:
  type-aliases-package: lzy.springbootuser.pojo  #别名
  mapper-locations: classpath:/mybatis/*.xml   #xml文件

Here the springboot integration mybatis environment ends.

Guess you like

Origin blog.csdn.net/m0_46538057/article/details/124803393