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

What is the method of integrating Mybatis with springboot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the method of integrating Mybatis with springboot. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

I. Preface

I remember when I first came into contact with SpringBoot, I was surprised that there was such an easy framework in the world. I immediately sighed: SpringBoot is the best framework in the world. Ha ha!

Mainly introduced to you about the integration of springboot Mybatis.

II. Springboot integrates Mybatis

1. Pom file dependency is introduced.

Org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java 5.1.38 org.springframework.boot spring-boot-starter-web

2. New configuration for application.yml

Spring: datasource: url: jdbc:mysql://localhost:3306/yys_springboot_mybatis username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver

3 、 UserEntity.java

/ * * user Management * Entity * @ author yys * / public class UserEntity implements Serializable {private Long id; private String name; private Integer age; private Byte status; @ JsonFormat (pattern= "yyyy-MM-dd HH:mm:ss", timezone= "GMT+8") private Date createTime; @ JsonFormat (pattern= "yyyy-MM-dd HH:mm:ss", timezone= "GMT+8") private Date updateTime; public Long getId () {return id;} public void setId (Long id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;} public Byte getStatus () {return status;} public void setStatus (Byte status) {this.status = status;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this.createTime = createTime;} public Date getUpdateTime () {return updateTime } public void setUpdateTime (Date updateTime) {this.updateTime = updateTime;}}

4 、 UserController.java

/ * Controller * @ author yys * / @ RestController@RequestMapping ("/ user") public class UserController {@ Autowired private UserService userService; @ RequestMapping ("/ add") public String addUser (String userName, Integer age) {return userService.addUser (userName, age)? Success: "fail";} @ RequestMapping ("/ get") public UserEntity getUserByName (String userName) {return userService.getUserByName (userName);}}

5 、 UserService.java

/ * * user Management * Service * @ author yys * / @ Servicepublic class UserService {@ Autowired private UserMapper userMapper; public boolean addUser (String userName, Integer age) {return userMapper.insert (userName, age) > 0? True: false;} public UserEntity getUserByName (String userName) {return userMapper.findByName (userName);}}

6 、 UserMapper.java

/ * * user Management * Mapper * @ author yys * / public interface UserMapper {@ Select ("SELECT id, user_name AS name, age, status, create_time AS createTime, update_time AS updateTime FROM yys_user WHERE user_name = # {name}") UserEntity findByName (@ Param ("name") String name) @ Insert ("INSERT INTO yys_user VALUES (NULL, # {name}, # {age}, 1, NOW (), NOW ()") int insert (@ Param ("name") String name, @ Param ("age") Integer age);}

7. Startup class

@ SpringBootApplication@MapperScan ("com.yys.mapper") public class YysApp {public static void main (String [] args) {SpringApplication.run (YysApp.class, args);}}

8. Initialize the sql file

CREATE TABLE `NOT NULL COMMENT user` (`id`bigint (11) NOT NULL AUTO_INCREMENT COMMENT'ID, self-adding', `user_ name` varchar (32) NOT NULL COMMENT 'user name', `age`int (11) NOT NULL COMMENT 'user age', `status` tinyint (2) NOT NULL DEFAULT'1' COMMENT 'status:-1-deleted; 1-normal;', `create_ time`user` creation time', `update_ time`UPDAT', PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

9. Testing

Http://localhost:8080/user/add?userName=yys&age=18

A. Page result-as shown in the following figure:

_ b, database results-as shown below: _

Http://localhost:8080/user/get?userName=yys

A. Page result-as shown in the following figure:

About springboot integration Mybatis method is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report