业务需求: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;
}
}