Spring对数据库的操作在jdbc下面做了深层次的封装,应用spring的注入性能,能够把DataSource注册到JdbcTemplate之中。

JdbcTemplate在Spring-jdbc包上面,还须要Spring-tx包反对,外面蕴含事务和异样管制.

建一个rumenz_springboot

  • 创立user表
create table user(  id int primary key auto_increment,  name varchar(100) not null default '',  domain varchar(100) not null default '')engine=innodb default charset=utf8;

退出pom的依赖

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <scope>runtime</scope></dependency>

SpringBoot配置文件

  • application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/rumenz_springbootspring.datasource.username=rootspring.datasource.password=root1234spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

创立User实体类

@Builder@Data@AllArgsConstructorpublic class User  implements RowMapper {    private Integer id;    private String name;    private String domain;    @Override    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {        User user=new User();        user.setId(rs.getInt("id"));        user.setName(rs.getString("name"));        user.setDomain(rs.getString("domain"));        return user;    }}

Service接口

  • UserService.java
package com.rumenz.lession14.controller.service;import com.rumenz.lession14.controller.entity.User;import java.util.List;/** * @className: UserService * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/12/13 **/public interface UserService {    Integer save(User user);    List<User> list();    Integer update(User user);    Integer batchSave();}

Service接口实现类

  • UserServiceImpl.java
package com.rumenz.lession14.controller.service.Impl;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.json.JsonMapper;import com.rumenz.lession14.controller.entity.User;import com.rumenz.lession14.controller.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;/** * @className: UserServiceImpl * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/12/13 **/@Servicepublic class UserServiceImpl implements UserService {    @Autowired    private JdbcTemplate jdbcTemplate;    @Override    public Integer save(User user) {        int reint = jdbcTemplate.update("insert into user(name,domain) values (?,?)", user.getName(), user.getDomain());        return reint;    }    @Override    public Integer batchSave() {        String sql="insert into user(name,domain) values(?,?)";        List<Object[]> par=new ArrayList<>();        for (int i = 0; i < 10; i++) {            String[] s=new String[2];            s[0]="入门小站"+i;            s[1]="https://rumenz.com/"+i;            par.add(s);        }        int[] ints = jdbcTemplate.batchUpdate(sql, par);        System.out.println(ints.toString());        return 0;    }    @Override    public List<User> list() {        //User实现RowMapper接口,实现接口里的mapRow办法。        List<User> list = jdbcTemplate.query("select * from user",new User());        return list;    }    @Override    public Integer update(User user) {        Integer reint=jdbcTemplate.update("update user set name=?,domain=? where id=?", user.getName(),user.getDomain(),user.getId());        //        return reint;    }}

Controller测试

  • RumenzController.java
package com.rumenz.lession14.controller;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.rumenz.lession14.controller.entity.User;import com.rumenz.lession14.controller.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * @className: RumenzController * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/12/13 **/@RestController@RequestMapping("/rumenz")public class RumenzController {    @Autowired    private UserService userService;    //增加数据    @GetMapping("/save")    public String save(){        User user=User.builder().name("入门小站").domain("https://rumenz.com").build();        Integer reint = userService.save(user);        return reint.toString();    }    //批量增加数据    @GetMapping("/batchSave")    public String batchSave(){        Integer reint = userService.batchSave();        return reint.toString();    }    //查问数据    @GetMapping("/list")    public String list() throws JsonProcessingException {        List<User> list = userService.list();        ObjectMapper objectMapper=new ObjectMapper();        String val = objectMapper.writeValueAsString(list);        return val;    }    //更新数据    @GetMapping("/update")    public String update() throws JsonProcessingException {        User user=User.builder().id(1).name("入门小站-批改").domain("https://tooltt.com").build();        Integer reint = userService.update(user);        return reint.toString();    }}

总结

罕用CURD操作大抵应用以下三个办法:
  • 1.execute办法,用于间接执行SQL语句
  • 2.update办法,用户新增批改删除操作
  • 3.query办法,用于查询方法
本小结源码地址:
  • GitHub:https://github.com/mifunc/spr...
  • Gitee:https://gitee.com/rumenz/spri...
  • https://rumenz.com/rumenbiji/...
介绍
  • 我的博客 https://rumenz.com/ ,
  • 我的工具箱 https://tooltt.com/
  • 微信公众号:【入门小站】

  • 关注【入门小站】回复【1001】获取 linux常用命令速查手册
  • 关注【入门小站】回复【1003】获取 LeetCode题解【java语言实现】
  • 关注【入门小站】回复【1004】获取 Java根底外围总结
  • 关注【入门小站】回复【1009】获取 阿里巴巴Java开发手册