Spring integrates mybatis to write back-end interface

Spring integrates mybatis to write back-end interface

basic introduction

Blog如有不对,敬请斧正
喜欢Android的可以关注我,日常更新Android干货
看都看到这了,加个关注叭!
建议收藏,防止找不到,哈哈

Spring:

The Spring framework is an open source application framework on the Java platform. Spring boot is designed to simplify the initial setup and development process of new Spring applications.

SpringBoot所具备的特征有

(1) Independent Spring applications can be created, and executable JARs and WARs can be created based on its Maven or Gradle plug-ins;
(2) Servlet containers such as Tomcat or Jetty are embedded;
(3) Automatic configuration "starter" is provided Project Object Model (POMS) to simplify Maven configuration;
(4) Automatically configure the Spring container as much as possible;
(5) Provide prepared features, such as indicators, health checks and external configuration;
(6) Absolutely no code generation, no need XML configuration.

mybatis:

Simply put, it is a database operation tool, and its performance is much better than JDBC.
MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs to records in the database.

Maybe you still don’t understand what it means. Keep reading

The implementation diagram
http://localhost:8080/User/8
finds the account password with id 8 in the returned database with id 8
Insert picture description here

下一篇文章:spring boot提供api接口与Android交互

Create project

1. Select Spring Initializr in the new project

Insert picture description here
2. Change the name to change the package name and other defaults just fine

Insert picture description here
3. Select the tools you need, and then wait for the idea to download. Only two are selected here. If you download it for the first time, it will take a long time to download. After you download it, you will check the pom.xml file. The pom is responsible for adding dependencies and letting the idea download. Yes, there are tools used in this project in it

Insert picture description here
Insert picture description here

Code

After downloading, I started to write the code. I only demonstrate the operation of finding users, because it is basically the same after meeting one, just change the operation statement of the database.

First show the structure of the entire project, the structure is very important!

DemoApplicationIt is the startup class of the project, responsible for scanning and loading the file
Insert picture description here
application.properties under the same package and sub-packages : configuration file
mapping: mapping file, writing database statements,
they are interlocked and need each other, the code is very concise, and the project structure is clear

Insert picture description here1. Create a User entity class

package com.example.demo.entity;

public class User {
    
    
    public int id;
    public String account;
    public String password;

    public int getId() {
    
    
        return id;
    }

    public void setId(int id) {
    
    
        this.id = id;
    }

    public String getAccount() {
    
    
        return account;
    }

    public void setAccount(String account) {
    
    
        this.account = account;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }
}

2. Create UserMapper.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.example.demo.mapper.UserMap">
    <!--数据库查询语句
    id是接口名字 resultType是实体类的路径
    -->
    <select id="Sel" resultType="com.example.demo.entity.User">
        select * from user where id = #{
    
    id}
    </select>

</mapper>

3. Create UserMap

package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMap {
    
    
    User Sel(int id);
}

4. Create UserServlet

package com.example.demo.servlet;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServlet {
    
    
    @Autowired
    UserMap userMapper;
    public User Sel(int id){
    
    
        return userMapper.Sel(id);
    }
}

5. Create UserController

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.servlet.UserServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {
    
    

    @Autowired
    private UserServlet userServlet;

    /**
     * 查找用户
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public User GetUser(@PathVariable int id){
    
    
       return userServlet.Sel(id);
    }
}

6. Configure the database in application.properties. Add the following code and change it to your own. The database should be understood, so I won’t introduce it here.

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/toys?useUnicode=true&characterEncoding=utf-8&userSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=com.example.demo.entity
logging.level.com.example.demo.mapper=debug

Finally, introduce the annotations used inside

annotation detailed
@Repository Used to label data access components, namely DAO components
@Service Generally used to modify the components of the service layer
@RequestMapping The mark can be requested with get or post request
@RestController Return JSON data

Simple test

Start the project
and enter
http://localhost:8080/User/8 in the browser to find the account password with the id of 8 in the returned database with the id of 8.
Insert picture description here
http://localhost:8080/User/1 find the id in the database with the id of 1 to return Account password of 1
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44758662/article/details/109556178