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

The process of SpringBoot accessing Mysql with JdbcTemplates

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "the process of SpringBoot accessing Mysql with JdbcTemplates". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preparatory work

Jdk 1.8

Maven 3.0

Idea

Mysql

Initialize mysql:

-- create table `roomt`DROP TABLE `roomt`IF EXISTSCREATE TABLE `account` (`id`DROP (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 `account`VALUES ('1century,' aaa', '1000'); INSERT INTO `account`VALUES (' 2percent, 'bbb',' 1000'); INSERT INTO `account` VALUES ('3percent,' ccc', '1000'); create project introduction dependency:

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

Org.springframework.boot spring-boot-starter-jdbc

Introduce mysql connection classes and connection pooling:

Mysql mysql-connector-java runtime com.alibaba druid 1.0.29

Enable web:

Org.springframework.boot spring-boot-starter-web configuration related files

Configure the mysql driver class, database address, database account number and password information in the application.properties file.

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

By introducing these dependencies and configuring some basic information, springboot can access database classes.

Specific coding entity class public class Account {private int id; private String name; private double money;.... Getter is omitted. Setter} dao layer public interface IAccountDAO {int add (Account account); int update (Account account); int delete (int id); Account findAccountById (int id); List findAccountList ();}

Specific implementation classes:

Package com.forezp.dao.impl;import com.forezp.dao.IAccountDAO;import com.forezp.entity.Account;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import java.util.List;/** * Created by fangzhipeng on 2017-4-20. * / @ Repositorypublic class AccountDaoImpl implements IAccountDAO {@ Autowired private JdbcTemplate jdbcTemplate @ Override public int add (Account account) {return jdbcTemplate.update ("insert into account (name, money) values (?,)", account.getName (), account.getMoney ();} @ Override public int update (Account account) {return jdbcTemplate.update ("UPDATE account SET NAME=?, money=?) WHERE id=? ", account.getName (), account.getMoney (), account.getId ();} @ Override public int delete (int id) {return jdbcTemplate.update (" DELETE from TABLE account where id=? ", id);} @ Override public Account findAccountById (int id) {List list = jdbcTemplate.query (" select * from account where id=? ", new Object [] {id}, new BeanPropertyRowMapper (Account.class)) If (listings null & & list.size () > 0) {Account account = list.get (0); return account;} else {return null;}} @ Override public List findAccountList () {List list = jdbcTemplate.query ("select * from account", new Object [] {}, new BeanPropertyRowMapper (Account.class) If (listings null & & list.size () > 0) {return list;} else {return null;}} service layer public interface IAccountService {int add (Account account); int update (Account account); int delete (int id); Account findAccountById (int id); List findAccountList ();}

Specific implementation classes:

@ Servicepublic class AccountService implements IAccountService {@ Autowired IAccountDAO accountDAO; @ Override public int add (Account account) {return accountDAO.add (account);} @ Override public int update (Account account) {return accountDAO.update (account);} @ Override public int delete (int id) {return accountDAO.delete (id);} @ Override public Account findAccountById (int id) {return accountDAO.findAccountById (id) } @ Override public List findAccountList () {return accountDAO.findAccountList ();}} build a set of restful api to show package com.forezp.web;import com.forezp.entity.Account;import com.forezp.service.IAccountService;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 IAccountService 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.findAccountById (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) {Account account=new Account (); account.setMoney (money); account.setName (name); account.setId (id); int t=accountService.update (account) If (tweezers 1) {return account.toString ();} else {return "fail";}} @ RequestMapping (value = "", method = RequestMethod.POST) public String postAccount (@ RequestParam (value = "name") String name, @ RequestParam (value = "money") double money) {Account account=new Account () Account.setMoney (money); account.setName (name); int t = accountService.add (account); if (tweezers) {return account.toString ();} else {return "fail";}

Can be tested through postman, specific I have passed all the tests, without any problems. Note the style in which restful builds api.

This is the end of the content of "the process of SpringBoot accessing Mysql with JdbcTemplates". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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