springMVC模式之dao终极版

业务需求:reg.jsp注册→reg.do获取post请求的数据(controller)→service(业务处理)→daoimp(数据库连接层)
要求:
全程使用spring分配对象,
处理结构:
在这里插入图片描述

创建spring MVC(略)

min文件主要结构

tree图

在这里插入图片描述

bean

/**
 * Copyright (C), 2021/9/1 1:11
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/1 1:11
 */

package txk.com.bean;


import org.springframework.stereotype.Component;

/**
 * @Author TianXinKun
 * @createTime 2021/9/1 1:11 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Component
public class User {
    int id;
    String name;
    String password;
    String age;
    String sex;

    public User(int id, String name, String password, String age, String sex) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }

    public User(String name, String password, String age, String sex) {
        this.name = name;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }

    public User() {
    }

    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 String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        User user = (User) o;

        if (id != user.id) {
            return false;
        }
        if (name != null ? !name.equals(user.name) : user.name != null) {
            return false;
        }
        if (password != null ? !password.equals(user.password) : user.password != null) {
            return false;
        }
        if (age != null ? !age.equals(user.age) : user.age != null) {
            return false;
        }
        return sex != null ? sex.equals(user.sex) : user.sex == null;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (age != null ? age.hashCode() : 0);
        result = 31 * result + (sex != null ? sex.hashCode() : 0);
        return result;
    }
}

CONTROLLER

/**
 * Copyright (C), 2021/9/3 1:40
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/3 1:40
 */

package txk.com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import txk.com.bean.User;
import txk.com.service.UserService;

/**
 * @Author TianXinKun
 * @createTime 2021/9/3 1:40 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Controller
public class Controller1 {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/reg.do",method = RequestMethod.POST)
    public String reg(User user){
        if (userService.insertuser(user)) {
            return "source";
        }else {
            return "error";
        }
    }
}

DAO

package txk.com.dao;

import txk.com.bean.User;

public interface UserDAO {
    /*
    * 根据无id User新增一条数据
    * */
    void insertuser(User user);
}

DAOIMP

/**
 * Copyright (C), 2021/9/3 1:44
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/3 1:44
 */

package txk.com.daoimp;

import org.springframework.stereotype.Repository;
import txk.com.bean.User;
import txk.com.dao.UserDAO;
import txk.com.db.Config;
import txk.com.db.Dbconfig;

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

/**
 * @Author TianXinKun
 * @createTime 2021/9/3 1:44 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Repository
public class UserDAOIMP implements UserDAO {
    Connection conn;

    public UserDAOIMP() {
        this.conn = Dbconfig.getCon();
    }

    @Override
    public void insertuser(User user) {
        try {
            String sql = "INSERT INTO " + Config.tb_user + " (id, name, password, age, sex) VALUES (null,?,?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getAge());
            preparedStatement.setString(4,user.getSex());
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Dbconfig.colce(conn);
        }
    }
}

db

/**
 * Copyright (C), 2021/9/3 1:44
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/3 1:44
 */

package txk.com.daoimp;

import org.springframework.stereotype.Repository;
import txk.com.bean.User;
import txk.com.dao.UserDAO;
import txk.com.db.Config;
import txk.com.db.Dbconfig;

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

/**
 * @Author TianXinKun
 * @createTime 2021/9/3 1:44 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Repository
public class UserDAOIMP implements UserDAO {
    Connection conn;

    public UserDAOIMP() {
        this.conn = Dbconfig.getCon();
    }

    @Override
    public void insertuser(User user) {
        try {
            String sql = "INSERT INTO " + Config.tb_user + " (id, name, password, age, sex) VALUES (null,?,?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getAge());
            preparedStatement.setString(4,user.getSex());
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Dbconfig.colce(conn);
        }
    }
}

/**
 * Copyright (C), 2021/9/3 1:44
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/3 1:44
 */

package txk.com.daoimp;

import org.springframework.stereotype.Repository;
import txk.com.bean.User;
import txk.com.dao.UserDAO;
import txk.com.db.Config;
import txk.com.db.Dbconfig;

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

/**
 * @Author TianXinKun
 * @createTime 2021/9/3 1:44 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Repository
public class UserDAOIMP implements UserDAO {
    Connection conn;

    public UserDAOIMP() {
        this.conn = Dbconfig.getCon();
    }

    @Override
    public void insertuser(User user) {
        try {
            String sql = "INSERT INTO " + Config.tb_user + " (id, name, password, age, sex) VALUES (null,?,?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,user.getName());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getAge());
            preparedStatement.setString(4,user.getSex());
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Dbconfig.colce(conn);
        }
    }
}

service

/**
 * Copyright (C), 2021/9/3 3:26
 *
 * @author 田信坤
 * Author:   TianXinKun
 * Date:     2021/9/3 3:26
 */

package txk.com.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import txk.com.bean.User;
import txk.com.daoimp.UserDAOIMP;

/**
 * @Author TianXinKun
 * @createTime 2021/9/3 3:26 
 *             This program is protected by copyright laws.
 * @version : 1.0
 */
@Service
public class UserService {
    @Autowired
    private UserDAOIMP userDAOIMP;

    public UserService() {
    }

    public boolean insertuser(User user){
        boolean b ;
        try {
            userDAOIMP.insertuser(user);
            b= true;
        } catch (Exception e) {
            e.printStackTrace();
            b= false;
        }
        return b;
    }
}

最终效果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值