企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 概述 三层思想:高内聚(把相同的功能写在一起),低耦合(别人的事,不要参和,减少功能交叉). ### 不同层次使用不同包表示 * com.xxx 公司域名倒写. * com.xxx.dao :dao层 * com.xxx.service :service层 * com.xxx.domain : javabean * com.xxx.utils : 工具类 * com.xxx.web/view : 视图层 dao层 ~~~ package com.like.dao; import com.like.utils.ConnectionManager; import org.apache.commons.dbutils.QueryRunner; import java.sql.Connection; import java.sql.SQLException; public class AccountDao { //转账出去 public void fromAccount(String name, double money) throws SQLException { Connection connection = ConnectionManager.getConnection(); QueryRunner queryRunner = new QueryRunner(); int res = queryRunner.update(connection, "update account set money = money - ? where name = ?", money, name); } //收钱回来 public void toAccount(String name, double money) throws SQLException { Connection connection = ConnectionManager.getConnection(); QueryRunner queryRunner = new QueryRunner(); int res = queryRunner.update(connection, "update account set money = money + ? where name = ?", money, name); } } ~~~ 工具类 ~~~ package com.like.utils; import java.sql.Connection; import java.sql.SQLException; //连接管理类 //主要负责获取连接,开启事务,提交事务,回滚事务 public class ConnectionManager { //定义一个ThreadLocal来保存当前当前线程的连接 private static ThreadLocal<Connection> tl = new ThreadLocal<>(); //获取连接 public static Connection getConnection() throws SQLException { Connection conn = tl.get(); //判断是否为空,如果是空,说明是service层第一次获取 if (conn == null) { conn = C3P0Utils.getConnection(); tl.set(conn); } //如果不为空,说明是dao层第二次获取 return conn; } //开启事务 public static void start() throws SQLException { ConnectionManager.getConnection().setAutoCommit(false); } //提交事务 public static void commit() throws SQLException { ConnectionManager.getConnection().commit(); } //回滚事务 public static void rollback() throws SQLException { ConnectionManager.getConnection().rollback(); } //关闭连接 public static void close() throws SQLException { ConnectionManager.getConnection().close(); } } ~~~ service层 ~~~ package com.like.service; import com.like.dao.AccountDao; import com.like.utils.ConnectionManager; import java.sql.Connection; import java.sql.SQLException; public class AccountService { public void transfer(String fromName, String toName, double money) { Connection connection = null; try { AccountDao accountDao = new AccountDao(); ConnectionManager.start(); //转出去 accountDao.fromAccount(fromName, money); //收回来 accountDao.toAccount(toName, money); ConnectionManager.commit(); } catch (SQLException e) { try { ConnectionManager.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { try { ConnectionManager.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ~~~ view层 ~~~ package com.like.view; import com.like.service.AccountService; public class AccountView { public static void main(String[] args) { String fromName = "jack"; String toName = "tom"; double money = 1000; AccountService accountService = new AccountService(); accountService.transfer(fromName, toName, money); } } ~~~