非JPA,UsersDao实现

This commit is contained in:
myh 2023-10-07 18:51:01 +08:00
parent c66a060470
commit 556adcdc57
6 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.example.springdemo.dao.InterfaceDao;
import java.util.List;
public interface AbstractDao<T> {
int insert(T t); //
int delete(T t);//
int update(T t);//
List<T> search(T t);//
T searchID(Long id);// ByID
}

View File

@ -0,0 +1,7 @@
package com.example.springdemo.dao.InterfaceDao;
import com.example.springdemo.dao.InterfaceDao.AbstractDao;
import com.example.springdemo.entities.Users;
public interface UsersDao extends AbstractDao<Users> {
}

View File

@ -0,0 +1,28 @@
package com.example.springdemo.dao.impl;
import com.example.springdemo.utils.DataBaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CommonDao<T> {
DataBaseUtil dataBaseUtil = new DataBaseUtil();
public int delete(Connection ct, PreparedStatement ps, ResultSet rs,
String sql, Long id) {
int flag;
try {
ct = dataBaseUtil.getConnection();
ps = ct.prepareStatement(sql);
ps.setLong(1, id);
flag = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
dataBaseUtil.close(ct, ps, rs);
}
return flag;
}
}

View File

@ -0,0 +1,132 @@
package com.example.springdemo.dao.impl;
import com.example.springdemo.dao.InterfaceDao.UsersDao;
import com.example.springdemo.entities.Users;
import com.example.springdemo.utils.DataBaseUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class UsersDaoImpl implements UsersDao {
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
DataBaseUtil dataBaseUtil = new DataBaseUtil();
//新建用户
@Override
public int insert(@NotNull Users users) {
int flag;
try {
connection = dataBaseUtil.getConnection();
String insert_sql = "insert into Users(name, phoneNumber, address, password) " + "values (?,?,?,?)";
preparedStatement = connection.prepareStatement(insert_sql);
preparedStatement.setString(1, users.getName());
preparedStatement.setString(2, users.getPhoneNumber());
preparedStatement.setString(3, users.getAddress());
preparedStatement.setString(4, users.getPassword());
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
dataBaseUtil.close(connection, preparedStatement, resultSet);
}
return flag;
}
//删除
@Override
public int delete(@NotNull Users user) {
String delete_sql = "delete from Users where id=?";
CommonDao<Users> dao = new CommonDao<>();
return dao.delete(connection, preparedStatement, resultSet,
delete_sql, user.getId());
}
//更新用户个人信息
//@TODO 只能更新所有信息不能选择性更新
@Override
public int update(@NotNull Users users) {
int flag;
String update_sql = "update Users " + "set name = ?, phoneNumber = ?, address = ? " + "where id = ?";
try {
connection = dataBaseUtil.getConnection();
preparedStatement = connection.prepareStatement(update_sql);
preparedStatement.setString(1, users.getName());
preparedStatement.setString(2, users.getPhoneNumber());
preparedStatement.setString(3, users.getAddress());
preparedStatement.setLong(4, users.getId());
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return flag;
}
//查询所有用户
@Override
public @NotNull List<Users> search(@NotNull Users user) {
List<Users> list = new ArrayList<>();
String selectAll_sql = "select * from Users";
try {
connection = dataBaseUtil.getConnection();
preparedStatement = connection.prepareStatement(selectAll_sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
user = new Users();
SetAttribute(user, resultSet);
list.add(user);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
dataBaseUtil.close(connection, preparedStatement, resultSet);
}
return list;
}
//用ID查询用户
@Override
public @NotNull Users searchID(@NotNull Long id) {
Users user = null;
String selectID_sql = "select * from Users where id = ?";
try {
connection = dataBaseUtil.getConnection();
preparedStatement = connection.prepareStatement(selectID_sql);
preparedStatement.setLong(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
user = new Users();
SetAttribute(user, resultSet);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
dataBaseUtil.close(connection, preparedStatement, resultSet);
}
return user;
}
private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException {
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
user.setPhoneNumber(rs.getString("phoneNumber"));
user.setAddress(rs.getString("address"));
}
}

View File

@ -1,5 +1,9 @@
package com.example.springdemo.entities; package com.example.springdemo.entities;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -10,9 +14,13 @@ import lombok.NoArgsConstructor;
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Entity
@Table(name = "Users")
public class Users { public class Users {
@Id
private Long id;//主键用户ID private Long id;//主键用户ID
private String name;//用户姓名 private String name;//用户姓名
@Column(name = "phoneNumber")
private String phoneNumber;//用户联系方式 private String phoneNumber;//用户联系方式
private String address;//家庭住址 private String address;//家庭住址
private String password;//用户密码 private String password;//用户密码

View File

@ -0,0 +1,48 @@
package com.example.springdemo.utils;
import com.example.springdemo.config.DataBaseProperties;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import java.sql.*;
@Component
public class DataBaseUtil {
@Resource
DataBaseProperties dataBaseProperties;
// Connect to database
private final String connectionUrl =
dataBaseProperties.getUrl() +
dataBaseProperties.getUsername() +
dataBaseProperties.getPassword();
public Connection getConnection() {
Connection connection = null;
try {
// Code here.
connection = DriverManager.getConnection(connectionUrl);
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//释放资源
public void close(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}