SpringMvc存数据到数据库

Dao层
BaseDao.java

package cn.louis.dao;

import cn.louis.util.ConfigManager;

import java.sql.*;

public class BaseDao {
    protected java.sql.Connection connection = null;
    protected Statement statement = null;
    protected PreparedStatement preparedStatement = null;
    protected ResultSet resultSet = null;

    public void getConnection(){
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/smbms";
        String username = "root";
        String password = "star123456";
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public void closeAll(){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    public int executeUpdate(String sql, Object []objlist){
        int count = -1;
        getConnection();
        try{
            preparedStatement = connection.prepareStatement(sql);
            for (int i= 0; i < objlist.length; i++){
                preparedStatement.setObject((i+1),objlist[i]);
            }
            count = preparedStatement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return count;
    }

    public ResultSet executeQuery(String sql, Object []objlist){
        getConnection();
        try{
            preparedStatement = connection.prepareStatement(sql);
            if (objlist != null && objlist.length > 0){
                for (int i = 0; i < objlist.length; i++){
                    preparedStatement.setObject((i+1),objlist[i]);
                }
            }
            resultSet = preparedStatement.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return resultSet;
    }

    public static void main(String[] args) {
        BaseDao baseDao = new BaseDao();
        baseDao.getConnection();
        System.out.println(baseDao.connection);
    }

}

user
UserMapper接口

package cn.louis.dao.user;

import cn.louis.entity.User;

public interface UserMapper {
    public int addUser(User user);
}

UserMapperImpl.java

package cn.louis.dao.user;

import cn.louis.dao.BaseDao;
import cn.louis.entity.User;

public class UserMapperImpl extends BaseDao implements UserMapper{
    @Override
    public int addUser(User user) {
        StringBuffer stringBuffer = new StringBuffer("INSERT INTO smbms_user (id,userCode,userName,userPassword,gender,birthday,phone,address,userRole)");
        stringBuffer.append("VALUES(?,?,?,?,?,?,?,?,?)");
        Object [] objects = {user.getId(),user.getUserCode(),user.getUserName(),user.getUserPassword(),user.getGender(),user.getBirthday(),user.getPhone(),user.getAddress(),user.getUserRole()};
        return executeUpdate(stringBuffer.toString(),objects);
    }
}

entity层
User.java

package cn.louis.entity;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class User {
    private Integer id;
    private String userCode;
    private String userName;
    private String userPassword;
    private Integer gender;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    private String phone;
    private String address;
    private Integer userRole;
    private Integer createdBy;
    private Date creationDate;
    private Integer modifyBy;
    private Date modifyDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Date getBirthday() {
        return birthday;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getUserRole() {
        return userRole;
    }

    public void setUserRole(Integer userRole) {
        this.userRole = userRole;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Integer getModifyBy() {
        return modifyBy;
    }

    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userCode='" + userCode + '\'' +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", gender=" + gender +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", userRole=" + userRole +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                '}';
    }
}

service层
UserService接口

package cn.louis.service;

import cn.louis.entity.User;

public interface UserService {
    public int addUser(User user);
}

UserServiceImpl.java

package cn.louis.service;

import cn.louis.dao.user.UserMapper;
import cn.louis.dao.user.UserMapperImpl;
import cn.louis.entity.User;

public class UserServiceImpl implements UserService{
    private UserMapper userMapper;

    public UserServiceImpl(){
        userMapper = new UserMapperImpl();
    }

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

controller层
UserController.java

package cn.louis.controller;


import cn.louis.entity.User;
import cn.louis.service.UserService;
import cn.louis.service.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;


@Controller
@RequestMapping("/user")
public class UserController {
    UserService userService = new UserServiceImpl();
    @RequestMapping("/create1")
    public String create1(String id,String userCode,String userPassword,String userName,String gender,String phone,String address,String userRole,String birthday) throws ParseException {
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUserCode(userCode);
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
        user.setGender(Integer.parseInt(gender));
        user.setPhone(phone);
        user.setAddress(address);
        user.setUserRole(Integer.parseInt(userRole));
        userService.addUser(user);
        return "OK";
    }

    @RequestMapping("/create2")
    public String create2(User user){
        userService.addUser(user);
        return "OK";
    }
}

web层
index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 11952
  Date: 2023/3/22
  Time: 14:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>欢迎注册</title>
  </head>
  <body>
  <form action="<%=request.getContextPath()%>/user/create2" method="post">
    <p>
      用户id
      <input type="text" name="id">
    </p>
    <p>
      用户账号:
      <input type="text" name="userCode"/>
    </p>
    <p>
      设置密码:
      <input type="text" name="userPassword">
    </p>
    <p>
      真实姓名:
      <input type="text" name="userName">
    </p>
    <p>
      性别:
      <input type="radio" name="gender" value="1"><input type="radio" name="gender" value="2"></p>
    <p>
      联系电话:
      <input type="text" name="phone">
    </p>
    <p>
      家庭住址:
      <input type="text" name="address">
    </p>
    <p>
      角色
      <select name="userRole">
        <option value="0">管理员</option>
        <option value="1">经理</option>
        <option value="2">店员</option>
      </select>
    </p>
    <p>
      生日:
      <input type="date" name="birthday">
    </p>
    <button type="submit">提交</button>
  </form>
  </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值