Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Use Spring Boot+MyBatis framework to do query operation

Shulou Source: shulou.com Published: 2022-06-03 07:48:04 10月03日 Update

one。 Create Module under the project you set up and select Spring initializr to create.

two。 Select: Maven Project (the project's build tool) at Type

three。 Check web,mybatis,mysql when creating dependencies (this depends on your personal needs, you can choose)

The established project structure is as follows:

Note: application.properties and application.yml are the same thing and are the core configuration files of the project.

The contents are as follows:

# connecting to the database

Spring.datasource.url=jdbc:mysql://localhost:3306/smbms

Spring.datasource.username=root

Spring.datasource.password=1234

Spring.datasource.driverClassName=com.mysql.jdbc.Driver

# introducing the configuration file of mybatis

Mybatis:

Mybatis.mapper-locations=classpath:mapper/*.xml

Mybatis.type-aliases-package=com.example.sprboot.pojo

Corresponding pom.xml file

4.0.0

Com.examplespringboot0.0.1-SNAPSHOTjarspringbootDemo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis -spring-boot-starter 1.3.0 mysql mysql-connector-java com.alibaba fastjson 1.2.49 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-maven-plugin

The corresponding interface UserMapper is as follows:

@ Repository

Public interface UserMapper {

List getList ()

}

Service is as follows:

Public interface UserService {

List getList ()

}

Impl is as follows:

@ Service

Public class UserServiceImpl implements UserService {@ Resourcebr/ > @ Resourcebr/ > @ Override

Return userMapper.getList ()

}

}

Create a folder mapper in resources and put the mapper.xml file in it. The code is as follows:

Work number

User name

Name

Gender

Birthday

Telephone

Address

Creation time

There is a th tag here, and you need to introduce a

And introduce the corresponding jar package in pom.xml (jstl expression cannot be used in html)

You can expand the knowledge of thymeleaf.

The controller code is as follows:

@ Controller

Public class UserController {@ Resourcebr/ > @ Resourcebr/ > @ RequestMapping ("/")

List list=userService.getList ()

Model.addAttribute ("users", list)

System.out.println (list)

Return "/ index.html"

}

}

Note: many mistakes may be reported during the transfer, which are basically due to errors in the use of annotations. I hope you can be more careful.

Tags: File project UTF-8 selection code configuration careful thing personal content address name tool job number project gender interface controller data database Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Apple Linux macOS