Springboot+mysql简单的数据库增加操作

Springboot+mysql简单的spring操作

1.目录结构

Springboot+mysql简单的数据库增加操作

jdbcUtil代码

package com.springboot.first.jdbcUtil;
import java.sql.*;

public class JdbcUtil {
private static String DRIVER = “com.mysql.jdbc.Driver”;
private static String USERNAME = “root”;
private static String PASSWORD = “1234”;
private static String URL = “jdbc:mysql://localhost:3306/USER”;
private static Connection connection ;

/**
 *
 * Describe:获得数据库的连接
 * @Author: wwh
 * @Date: 2018/10/29 13:05
 * @Param: []
 * @Return: java.sql.Connection
 */
public static Connection getConnection(){


    try {
        Class.forName(DRIVER);
        connection =  DriverManager.getConnection(URL,USERNAME,PASSWORD);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return  connection;
}

/**
 *
 * Describe:关闭数据库操作
 * @Author: wwh
 * @Date: 2018/10/29 13:06
 * @Param: [connection, preparedStatement, resultSet]
 * @Return: void
 */
public static void close(Connection connection, PreparedStatement preparedStatement,
                         ResultSet resultSet){
    if(resultSet != null){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if(preparedStatement != null ){
        try {
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if(connection != null){
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

UserDao代码

package com.springboot.first.userDao;

import com.springboot.first.entity.User;

public interface UserDao {

//增加操作,成功返回1,失败返回0
int addUser(User user);

}

UserDaoImpl

package com.springboot.first.userDao.userDaoImpl;

import com.springboot.first.entity.User;
import com.springboot.first.jdbcUtil.JdbcUtil;
import com.springboot.first.userDao.UserDao;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class UserDaoImpl implements UserDao {

private PreparedStatement preparedStatement;
private Connection connection;
private ResultSet resultSet;

@Override
public int addUser(User user) {

    int result = 0;

    String sql = "INSERT INTO USER  VALUES(?,?,?,?,NOW())";
    try {
        connection = JdbcUtil.getConnection();
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,user.getId());
        preparedStatement.setString(2,user.getUserName());
        preparedStatement.setString(3,user.getPassWord());
        preparedStatement.setInt(4,user.getAge());
        result =  preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        if(preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

}

UserService

package com.springboot.first.userService;

import com.springboot.first.entity.User;

public interface UserService {

int addUser(User user);

}

UserServiceImpl

package com.springboot.first.userService;

import com.springboot.first.entity.User;
import com.springboot.first.userDao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserDao userDao;

@Override
public int addUser(User user) {
    return userDao.addUser(user);
}

}

Controller

package com.springboot.first.controller;

import com.springboot.first.entity.User;
import com.springboot.first.userService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/springboot")
public class UserController {

@Autowired
private UserService userService;


@RequestMapping("/addUser")
public int addUser(){
    User user = new User();
    user.setAge(12);
    user.setId("1");
    user.setPassWord("123");
    user.setUserName("root");
    return userService.addUser(user);
}

}

页面执行后结果

Springboot+mysql简单的数据库增加操作

数据库结果

Springboot+mysql简单的数据库增加操作