非JPA,UsersDao实现
This commit is contained in:
		@@ -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
 | 
			
		||||
}
 | 
			
		||||
@@ -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> {
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main/java/com/example/springdemo/dao/impl/CommonDao.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/main/java/com/example/springdemo/dao/impl/CommonDao.java
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										132
									
								
								src/main/java/com/example/springdemo/dao/impl/UsersDaoImpl.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								src/main/java/com/example/springdemo/dao/impl/UsersDaoImpl.java
									
									
									
									
									
										Normal 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"));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +1,9 @@
 | 
			
		||||
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.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
@@ -10,9 +14,13 @@ import lombok.NoArgsConstructor;
 | 
			
		||||
@Builder
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
@Entity
 | 
			
		||||
@Table(name = "Users")
 | 
			
		||||
public class Users {
 | 
			
		||||
    @Id
 | 
			
		||||
    private Long id;//主键,用户ID
 | 
			
		||||
    private String name;//用户姓名
 | 
			
		||||
    @Column(name = "phoneNumber")
 | 
			
		||||
    private String phoneNumber;//用户联系方式
 | 
			
		||||
    private String address;//家庭住址
 | 
			
		||||
    private String password;//用户密码
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										48
									
								
								src/main/java/com/example/springdemo/utils/DataBaseUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/main/java/com/example/springdemo/utils/DataBaseUtil.java
									
									
									
									
									
										Normal 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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user