SpringBoot queries database information and displays it on the front desk

1. The basic environment of SpringBoot.

Development tools: IntelliJ IDEA 2020.3.3 x64

JDK version: jdk1.8.0_51

JRE version: jdk1.8.0_51

2. Start installing Spring Boot.

1) Right-click the project>New>Module

2) Click Spring Initializr, Module SDK can use the default, service URL select the default official website address, and then Next

3) Group name, Artifact project name, Java version 8 I chose, Package name, others are default, and then Next

4) Click on Web, select Spring Web, select the Spring Boot version according to your own needs, do not choose others, and then Next

 5) Finish the installation

6) After Finsh, you need to pull the relevant jar package from the official website and just wait (the network may not be able to go down if the network is not good)

 This is the project structure after installation

2. Write a simple page to visit

1) First create the package structure, create controller, mapper, model, service under com.zzz in java, and create a mapping directory in resources.

 com.zzz.down

resources

 Package structure after creation

2) Configure the tomcat port number in application.properties for a simple access test.

Note: There are two configuration methods for application, namely .yml and .properties. Select the file Shift+f6 to modify the name. (Here I use the .yml method first)

3) Create HelloController in the controller layer

code content

@RestController is a combination of @Controller and @RequestMapping, we will use it separately if we jump the page.

4) Run the DemoApplication directly through the DemoApplication, and then open the browser for testing.

5) tomcat is running normally.

Open the browser to visit http://localhost:8080/  , which is normal

Then visit the hello Url path again, and the visit is over.

 6) Change application.yml to application.properties to visit, and re-run DemoApplication after repairing the application

Visit after refreshing, or hello proves that there is no problem.

3. The next step is the visit of Mybatis+springBoot.

1) Create package structure (2.1)

2) Create tables and test data

Directly produce mapper, xml, and model layer configuration files through the generator (the generator has been uploaded to the Baidu network disk, and you can extract what you need)

Link: https://pan.baidu.com/s/14V0RSxpLYHAZiLhFUc61wQ?pwd=narc 
Extraction code: narc

 After saving, enter cmd to open the command line and execute the production statement

success 

View generated files

3) Put the relevant files under the corresponding layer

4) Create ShopController

Create ShopService

Select the interface interface

Put it under the service layer

5) Create an impl package (Page) under the service layer, and then create ShopServiceImpl

6) Write the query code in ShopController (17 lines of red code will not be affected), and import related packages to generate related methods (put it on the red code)

Generate methods in ShopServiceImpl and create queries

 7) Copy the queryShopList method to the xml file under mapping, and write the query sql 

 8) Modify the application.properties configuration.

server.port=8080
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=com.zzz.model
#Load image configuration
spring.web.resources.static-locations=file:${web.web-root},file:${web.upload-path},classpath:/resources/,classpath:/static/,classpath:/templates/
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.min-idle=5
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.validation-query=SELECT 1
spring.datasource.druid.validation-query-timeout=2000
spring.datasource.password = database password
spring.datasource.url=jdbc:mysql://localhost:3306/数据库?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = database user
spring.thymeleaf.cache=false
Error handling set by #pagehelper
spring.main.allow-circular-references=true
#Absolute path to file upload
web.upload-path=D:/upload
web.web-root=D:/upload
#Pagination plugin
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true

9) Repair the pom.xml file

Fix this content:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzz</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mybatis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.sonatype.aether</groupId>
            <artifactId>aether-spi</artifactId>
            <version>1.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.2</version>
            </plugin>
        </plugins>
    </build>

</project>

Click Maven on the right to refresh and pull the jar package.

4. Create a front-end query page.

1) Import related dependencies

Common link: https://pan.baidu.com/s/1AuvSTxZ2OLbIUb3c2Y_-pg?pwd=narc 
Extraction code: narc

Common is copied to resources.

js link: https://pan.baidu.com/s/10vAuNb8TLX-rAfuc8A1YPg?pwd=narc 
Extraction code: narc

js is placed in static.

2) Create a shop folder in templates.

Create the showShop.html page.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table border="1">
    <tr>
        <td>Id</td>
        <td>name</td>
        <td>Is it on the shelves</td>
        <td>Type</td>
        <td>Suitable crowd</td>
        <td>date</td>
        <td>Price</td>
        <td>Image</td>
        <td>Introduction</td>
        <td>Operation</td>
    </tr>
    <tr th:each="s:${list}">
        <td th:text="${s.shopid}"></td>
        <td th:text="${s.shopname}"></td>
        <td th:if="${s.shoponsale==1}">是</td> <td th:if="${s.shoponsale==2}">否</td>
        <td th:if="${s.shoptype==1}">Snacks</td><td th:if="${s.shoptype==2}">Drinks</td><td th: if="${s.shoptype==3}">daily necessities</td>
        <td>
            <span th:if="${s.shopsuitable.contains('1')}">儿童</span>
            <span th:if="${s.shopsuitable.contains('1')}">青年</span>
            <span th:if="${s.shopsuitable.contains('1')}">壮年</span>
            <span th:if="${s.shopsuitable.contains('1')}">老年</span>
        </td>
        <td th:text="${s.shopdate}"></td>
        <td th:text="${s.shopprice}"></td>
        <td>
            <img th:src="${s.shopimg}" width="50px" height="50px">
        </td>
        <td th:text="${s.shopshow}"></td>
        <td>Operation</td>
    </tr>
</table>
</body>
</html>

 5. Add @MapperScan("com.zzz.mapper") to DemoApplication

run tomcat

Access the browser (the query is completed, only one query is written this time)

6. Note: If all the above configurations are installed, the following two errors may be reported

1) java: package org.springframework.beans.factory.annotation does not exist.

2)Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shopsController': Unsatisfied dependency expressed through field 'shopService'; 找不到依赖

Solution to the first error:

Select the Settings in the file

  1. Find Maven, check always update snapshots, then Apply>Ok

Finally, cut all the dependencies of the pom.xml file, copy them in again, and refresh the import. (Or restart Idea, I just have no problem after restarting)

The solution to the second problem:

1. First check whether the annotation on the Service layer class has been added or is wrong. It should be @Service, whether the annotation refers to the Spring class, do not import it into another package, and see the path of the package.

2. Also, if the Service layer is divided into interface and implementation class, check if the implementation class is annotated (@Service), and see if there is any implementation class.

I forgot to add the @Service annotation in the ShopServiceImpl implementation class, and it was solved by adding it. 

Guess you like

Origin blog.csdn.net/kking_Ran/article/details/127352709