ORM基础

本文介绍了ORM的基础,展示了DBUtils获取连接和关闭资源的代码。接着讨论了Service业务逻辑层和事务管理,提出两种解决方案:传递Connection和使用ThreadLocal。然后讲解了三层架构的定义和项目搭建过程,强调了易修改和易扩展性的设计原则。
摘要由CSDN通过智能技术生成

ORM:

DBUtils:

public static Connection getConnenction(){
   
try {
        Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
   
} catch (ClassNotFoundException e) {
        e.printStackTrace()
;
   
}
    Connection connection =
null;
    try
{
        connection = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=school","sa","123");
   
} catch (SQLException e) {
        e.printStackTrace()
;
   
}
   
return connection;
}
public static void closeAll(Connection connection , Statement statement , ResultSet resultSet){
   
if(resultSet!=null){
       
try {
            resultSet.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()
;
       
}
    }
}

Entity:

Connection connection =null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<User> userList = new ArrayList<>();
connection = DBUtils2.getConnection();
preparedStatement=connection.prepareStatement("select  * from [user]");
resultSet = preparedStatement.executeQuery();
while
(resultSet.next()){
    String id = resultSet.getString(
"id");
   
String username = resultSet.getString("username");
   
String password = resultSet.getString("password");
   
User user = new User();
   
user.setId(id);
   
user.setUsername(username);
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值