目录
一、前言
二、目录结构
三、代码配置:
1、不过事先要先准备好生成数据库脚本
2、pojo 实体
3、dao 接口及实现类
4、service 接口及实现类
5、controller 方法调用
6、application.yml(使用多文件配置,在yml主页面引用dev文件里的内容,觉得麻烦可以把dev里的复制出来放进yml里)
7、pom文件引用
四、运行测试(强烈推荐restful风格使用 postman工具进行测试):
1、查询列表
2、新增一条信息
3、查询一条信息
4、删除一条信息
五、结束语
本篇主要讲解要点如下:
1、最原生的jdbc下springboot的增删改查案例
2、基于restful风格的curd(故返回都是json格式,页面懒得写了),前后端开发 居家杀人者必备模式
3、本篇采用的mariadb,其实跟mysql完全一样,只是pom文件中的驱动由mysql改为了mariadb,本地数据库环境若是mysql的同学不用慌,mariadb是基于mysql上开发的,pom文件只要是mariadb驱动就行,它会良好的自适应你本地的mysql,也就是说,本地装的mysql还是mariadb都一样,没区别(啰嗦完毕)
4、代码清晰简洁,一眼解析到位jdbc的本质(个人回顾后才选择使用它,后续还会更换常用的jpa、mybatis等持久性框架等)
代码逻辑从dao——>service——>controller 开发
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50717
Source Host : localhost:3306
Source Schema : springboot
Target Server Type : MySQL
Target Server Version : 50717
File Encoding : 65001
Date: 13/03/2019 17:09:47
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`money` double(20, 0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '张三', 88);
INSERT INTO `user` VALUES (2, '李四', 15);
SET FOREIGN_KEY_CHECKS = 1;
package com.yb7s.springboot.jdbc.pojo;
/**
* @Date: 2019/3/3 12:47
* @Auther: ybzhu
* @Description:
*/
public class User {
private int id ;
private String name ;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
package com.yb7s.springboot.jdbc.dao;
import com.yb7s.springboot.jdbc.pojo.User;
import java.util.List;
/**
* @Date: 2019/3/3 12:49
* @Auther: ybzhu
* @Description:
*/
public interface UserDao {
int add(User user);
int update(User user);
int delete(int id);
User findUserById(int id);
List findUserList();
}
package com.yb7s.springboot.jdbc.dao.impl;
import com.yb7s.springboot.jdbc.dao.UserDao;
import com.yb7s.springboot.jdbc.pojo.User;
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;
/**
* @Date: 2019/3/3 12:49
* @Auther: ybzhu
* @Description:
*/
@Repository
public class UserDaoImpl implements UserDao {
/**
* 注入 jdbc
*/
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 新增
* @param user
* @return
*/
@Override
public int add(User user) {
return jdbcTemplate.update("insert into user(name, money) values(?, ?)",user.getName(),user.getMoney());
}
/**
* 修改
* @param user
* @return
*/
@Override
public int update(User user) {
return jdbcTemplate.update("UPDATE user SET NAME=? ,money=? WHERE id=?",user.getName(),user.getMoney(),user.getId());
}
/**
* 删除
* @param id
* @return
*/
@Override
public int delete(int id) {
return jdbcTemplate.update("DELETE from user where id=?",id);
}
/**
* 查询一个
* @param id
* @return
*/
@Override
public User findUserById(int id) {
List list=jdbcTemplate.query("select * from user where id = ?", new Object[]{id}, new BeanPropertyRowMapper(User.class));
if(list!=null && list.size()>0){
User user = list.get(0);
return user;
}else{
return null;
}
}
/**
* 查询列表
* @return
*/
@Override
public List findUserList() {
List list = jdbcTemplate.query("select * from user", new Object[]{}, new BeanPropertyRowMapper(User.class));
if(list!=null && list.size()>0){
return list;
}else{
return null;
}
}
}
package com.yb7s.springboot.jdbc.service;
import com.yb7s.springboot.jdbc.pojo.User;
import java.util.List;
/**
* @Date: 2019/3/3 12:48
* @Auther: ybzhu
* @Description:
*/
public interface UserService {
int add(User user);
int update(User user);
int delete(int id);
User findUserById(int id);
List findUserList();
}
package com.yb7s.springboot.jdbc.service.impl;
import com.yb7s.springboot.jdbc.dao.UserDao;
import com.yb7s.springboot.jdbc.pojo.User;
import com.yb7s.springboot.jdbc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Date: 2019/3/3 12:48
* @Auther: ybzhu
* @Description:
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/**
* 添加 用户信息
* @param user
* @return
*/
@Override
public int add(User user) {
return userDao.add(user);
}
/**
* 修改用户信息
* @param user
* @return
*/
@Override
public int update(User user) {
return userDao.update(user);
}
/**
* 删除一个用户信息
* @param id
* @return
*/
@Override
public int delete(int id) {
return userDao.delete(id);
}
/**
* 查询一个用户信息
* @param id
* @return
*/
@Override
public User findUserById(int id) {
return userDao.findUserById(id);
}
/**
* 查询所有用户列表
* @return
*/
@Override
public List findUserList() {
return userDao.findUserList();
}
}
package com.yb7s.springboot.jdbc.controller;
import com.yb7s.springboot.jdbc.pojo.User;
import com.yb7s.springboot.jdbc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Date: 2019/3/3 12:50
* @Auther: ybzhu
* @Description:
*/
@RestController
@RequestMapping("/user")
public class UserController {
/**
* 注入 user service
*/
@Autowired
private UserService userService;
/**
* 查询用户列表
* @return
*/
@RequestMapping(value = "/list",method = RequestMethod.GET)
public List getUsers(){
return userService.findUserList();
}
/**
* 查询一个用户
* @param id
* @return
*/
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public User getUserById(@PathVariable("id") int id){
return userService.findUserById(id);
}
/**
* 删除一个用户
* @param id
* @return
*/
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
public String delUserById(@PathVariable("id") int id){
int flag=userService.delete(id);
if(flag==1){
return "删除成功!";
}else {
return "删除失败!";
}
}
/**
* 修改用户信息
* @param id
* @param name
* @param money
* @return
*/
@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
public String updateUser(@PathVariable("id")int id , @RequestParam(value = "name",required = true)String name,
@RequestParam(value = "money" ,required = true)double money){
User User=new User();
User.setMoney(money);
User.setName(name);
User.setId(id);
int t=userService.update(User);
if(t==1){
return "修改成功!";
}else {
return "修改失败!";
}
}
/**
* 新增用户信息
* @param name
* @param money
* @return
*/
@RequestMapping(value = "",method = RequestMethod.POST)
public String postUser( @RequestParam(value = "name")String name,
@RequestParam(value = "money" )double money){
User User=new User();
User.setMoney(money);
User.setName(name);
int t= userService.add(User);
if(t==1){
return "新增成功";
}else {
return "新增失败";
}
}
}
#spring
spring:
profiles:
active: dev
server:
port: 8081
spring:
datasource:
# 这里的驱动链接 是 mariadb,因为pom文件里引用的也是mariadb,它可以良好的识别你本地的mysql或mariadb
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/springboot
username: root
password: root
hikari:
connection-test-query: SELECT 1
connection-timeout: 60000
idle-timeout: 60000
max-lifetime: 60000
maximum-pool-size: 10
read-only: false
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.yb7s
springboot-jdbc
0.0.1-SNAPSHOT
springboot-jdbc
Demo project for Spring Boot
1.8
2.3.0
1.1.10
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.mariadb.jdbc
mariadb-java-client
${mariadb.version}
com.alibaba
druid
${druid.version}
org.springframework.boot
spring-boot-maven-plugin
好了,springboot下基于restfuk风格的jdbc 的curd写好了,欢迎对小伙伴们有所帮助