Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with SpringBoot MyBatis

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to get started with SpringBoot MyBatis". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to get started with SpringBoot MyBatis" can help you solve the problem.

A brief introduction to MyBatis

MyBatis is an excellent persistence layer framework that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all the JDBC code and the work of setting parameters and getting result sets. MyBatis can configure and map primitive types, interfaces, and Java POJO (Plain Old Java Objects, plain old Java objects) to records in the database through simple XML or annotations.

II. Steps for using MyBatis

1. General catalogue structure of MyBatis project

2. Create a simple SpringBoot project

3. Add MyBatis dependencies

Mysql mysql-connector-java 5.1.32 org.mybatis mybatis 3.4.6

4. Create a USER table in the database

CREATE TABLE `user` (`id` int (11) NOT NULL AUTO_INCREMENT, `username` varchar (20) NOT NULL DEFAULT "" COMMENT "username", `password` varchar (50) NOT NULL DEFAULT "COMMENT" password, PRIMARY KEY (`id`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC

5. Configure database connection information in application.properties

# Database related configuration spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useAffectedRows=truespring.datasource.username=rootspring.datasource.password=QQ796413#mybaits configuration # mapper load path mybatis.mapper-locations= classpath:mapper/*.xml# entity package location mybatis.type-aliases-package= com.example.mybatisdemo.entity#myatbis configuration file mybatis.config-location= classpath:mybatis-config.xml

6. Create the entity class corresponding to the USER table

Package com.example.mybatisdemo.entity;public class User {private int id; private String username; private String password; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username } public String getPassword () {return password;} public void setPassword (String password) {this.password = password } @ Override public String toString () {return "User {" + "id=" + id + ", username="+ username +" + ", password="+ password +" + "}";}

7. Create a UserMapper.java in mapper/UserMapper

Package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.entity.User;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface UserMapper {User findUserById (Integer id);}

8. Create a new UserService.java in service/UserService

Package com.example.mybatisdemo.service;import com.example.mybatisdemo.entity.User;public interface UserService {User findUserById (Integer id);}

9. Create a UserServiceImpl.java in service/impl/UserServiceImpl

Package com.example.mybatisdemo.service.impl;import com.example.mybatisdemo.entity.User;import com.example.mybatisdemo.mapper.UserMapper;import com.example.mybatisdemo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class UserServiceImpl implements UserService {@ Autowired private UserMapper userMapper; @ Override public User findUserById (Integer id) {return userMapper.findUserById (id);}}

10. Create a new mybatis-conf.xml under resources

11. Create a UserMapper.xml under the mapper file under resources

Select id, username, password from user where id= # {id,jdbcType=INTEGER}

12. Create a UserController.java

Package com.example.mybatisdemo.controller;import com.example.mybatisdemo.entity.User;import com.example.mybatisdemo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController {@ Autowired UserService userService; @ GetMapping ("/ findUserById") public User findUserById (@ RequestParam Integer id) {return userService.findUserById (1) }}

13. Testing

This is the end of the introduction to "how to get started with SpringBoot MyBatis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report