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

Springboot's process of integrating mybatis and accessing the database

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the process of springboot integrating mybatis and accessing database". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the process of springboot integrating mybatis and accessing database".

Introduce dependency

Introduce mybatis-spring-boot-starter dependencies in the pom file:

Org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0

Introduce database connection dependencies:

Mysql mysql-connector-java runtime com.alibaba druid 1.0.29 introduces data sources

Introduce a data source into the application.properties configuration file:

Spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver

In this way, springboot can access the data.

Create a database table

Build a table sentence:

-- create table `roomt` # DROP TABLE `roomt` IF EXISTSCREATE TABLE `roomt` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (20) NOT NULL, `money`double DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;INSERT INTO `roomt`VALUES ('1million,' aaa', '1000'); INSERT INTO `roomt`VALUES (' 2percent, 'bbb',' 1000'); INSERT INTO `roomt` VALUES ('3percent,' ccc', '1000'); specific implementation

This article is realized in the form of annotations.

Create entity: public class Account {private int id; private String name; private double money; setter... Getter... } Mapperpublic interface AccountMapper {@ Insert ("insert into account (name, money) values (# {name}, # {money})") int add (@ Param ("name") String name, @ Param ("money") double money) @ Update ("update account set name = # {name}, money = # {money} where id = # {id}") int update (@ Param ("name") String name, @ Param ("money") double money, @ Param ("id") int id); @ Delete ("delete from account where id = # {id}") int delete (int id) @ Select ("select id, name as name, money as money from account where id = # {id}") Account findAccount (@ Param ("id") int id); @ Select ("select id, name as name, money as money from account") List findAccountList ();} service layer @ Servicepublic class AccountService {@ Autowired private AccountMapper accountMapper; public int add (String name, double money) {return accountMapper.add (name, money) } public int update (String name, double money, int id) {return accountMapper.update (name, money, id);} public int delete (int id) {return accountMapper.delete (id);} public Account findAccount (int id) {return accountMapper.findAccount (id);} public List findAccountList () {return accountMapper.findAccountList ();}} controller layer to build restful APIpackage com.forezp.web Import com.forezp.entity.Account;import com.forezp.service.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/** * Created by fangzhipeng on 2017-4-20. * / @ RestController@RequestMapping ("/ account") public class AccountController {@ Autowired AccountService accountService; @ RequestMapping (value = "/ list", method = RequestMethod.GET) public List getAccounts () {return accountService.findAccountList () } @ RequestMapping (value = "/ {id}", method = RequestMethod.GET) public Account getAccountById (@ PathVariable ("id") int id) {return accountService.findAccount (id) } @ RequestMapping (value = "/ {id}", method = RequestMethod.PUT) public String updateAccount (@ PathVariable ("id") int id, @ RequestParam (value = "name", required = true) String name, @ RequestParam (value = "money", required = true) double money) {int t = accountService.update (name,money,id); if (tweeters 1) {return "success" } else {return "fail";}} @ RequestMapping (value = "/ {id}", method = RequestMethod.DELETE) public String delete (@ PathVariable (value = "id") int id) {int t = accountService.delete (id); if (tweezers 1) {return "success";} else {return "fail" } @ RequestMapping (value = "", method = RequestMethod.POST) public String postAccount (@ RequestParam (value = "name") String name, @ RequestParam (value = "money") double money) {int t = accountService.add (name,money); if (tweezers 1) {return "success";} else {return "fail";}

Passed the postman test.

Thank you for your reading, the above is the content of "the process of springboot integrating mybatis and accessing the database". After the study of this article, I believe you have a deeper understanding of the process of springboot integrating mybatis and accessing the database. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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