SPRINGMYBATIS01 Unit05: 实战技巧 、 资费列表 、 拦截器

本文介绍了一个基于Spring框架的系统分层实例,展示了表示层、业务层及持久层的具体实现方式,并详细解释了各层间的调用关系及职责划分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 系统分层 (扩展)

(1)为什么要分层?(了解)

为了系统好维护,系统的设计应该要做到“高内聚,低耦合”。
“高内聚”:指的是类的职责要单一,这样,一个类就可以会拆分成多个类(比如AccountService拆分成了AccountService和AccountDAO),这样,就形成了分层的概念。
“低耦合”:指的是类与类之间不要直接依赖。(AccountService要调用AccountDAO,应该使用依赖注入)。

(2)如何分层?

表示层:数据展现和操作界面,以及请求分发。
业务层:封装了业务逻辑。  
持久层: 封装了数据访问逻辑。
注:
    表示层调用业务层,业务层调用持久层。
    上一层通过接口来调用下一层提供的服务(这样,下一层的
    实现发生了改变,不影响上一层)。

这里写图片描述
这里写图片描述


案例:

/netctoss-spring/src/main/java/cn/tedu/netctoss/controller/LoginController.java
package cn.tedu.netctoss.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.netctoss.entity.Admin;
import cn.tedu.netctoss.service.ApplicationException;
import cn.tedu.netctoss.service.LoginService;

@Controller
public class LoginController {

    @Resource(name="loginService")
    private LoginService loginService;

    @RequestMapping("/toLogin.do")
    public String toLogin(){
        System.out.println("toLogin()");
        return "login";
    }

    @RequestMapping("/login.do")
    public String login(
            HttpServletRequest request){
        System.out.println("login()");
        //读取帐号,密码
        String adminCode = 
                request.getParameter(
                        "adminCode");
        System.out.println("adminCode:"
                        + adminCode);
        String pwd = 
                request.getParameter("pwd");
        //调用业务层提供的服务

        try{
            Admin admin = 
                loginService.checkLogin(
                        adminCode, pwd);
        }catch(Exception e){
            e.printStackTrace();
            if(e instanceof ApplicationException){
                //应用异常,明确提示用户
                /*
                 * getMessage方法继承自
                 * RuntimeException,作用是
                 * 获得异常的描述信息。
                 */
                request.setAttribute(
                        "login_failed", 
                        e.getMessage());
                return "login";
            }
            //系统异常,提示用户稍后重试
            return "error";         
        }


        return "redirect:toIndex.do";
    }

    @RequestMapping("/toIndex.do")
    public String toIndex(){
        System.out.println("toIndex()");
        return "index";
    }
}
/netctoss-spring/src/main/java/cn/tedu/netctoss/dao
AdminDAO.java
package cn.tedu.netctoss.dao;

import cn.tedu.netctoss.entity.Admin;
/**
 * 持久层接口
 */
public interface AdminDAO {
    public Admin findByAdminCode(
            String adminCode);

}
AdminDAOJdbcImpl.java
package cn.tedu.netctoss.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import cn.tedu.netctoss.entity.Admin;

/**
 * 持久层实现
 */

@Repository("adminDAO")
public class AdminDAOJdbcImpl implements 
AdminDAO{

    @Resource(name="ds")
    private DataSource ds;


    public Admin findByAdminCode(
            String adminCode) {
        Admin admin = null;
        Connection conn = null;
        try {
            conn = ds.getConnection();
            String sql = 
                    "select * from admin_info_lhh "
                    + "where admin_code=?";
            PreparedStatement ps = 
                    conn.prepareStatement(
                            sql);
            ps.setString(1, adminCode);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                admin = new Admin();
                admin.setAdminId(rs.getInt("admin_id"));
                admin.setAdminCode(rs.getString("admin_code"));
                admin.setPassword(rs.getString("password"));
                admin.setName(rs.getString("name"));
                admin.setTelephone(rs.getString("telephone"));
                admin.setEmail(rs.getString("email"));
                admin.setEnrolldate(rs.getTimestamp("enrolldate"));
            }
        } catch (SQLException e) {
            //step1. 记日志(保留现场)
            e.printStackTrace();
            /*
             * step2.看异常能否恢复,如果
             * 不能够恢复(比如数据库服务
             * 暂停,网络中断,这样的异常
             * 一般我们称之为系统异常),
             * 则提示用户稍后重试。如果能够
             * 恢复,则立即恢复。
             * 
             */
            throw new RuntimeException(e);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        return admin;
    }

}
/netctoss-spring/src/main/java/cn/tedu/netctoss/entity/Admin.java
package cn.tedu.netctoss.entity;

import java.io.Serializable;
import java.sql.Timestamp;

public class Admin implements Serializable{
    private Integer adminId;
    private String adminCode;
    private String password;
    private String name;
    private String telephone;
    private String email;
    private Timestamp enrolldate;

    @Override
    public String toString() {
        return "Admin [adminId=" + adminId + ", adminCode=" + adminCode + ", password=" + password + ", name=" + name
                + ", telephone=" + telephone + ", email=" + email + ", enrolldate=" + enrolldate + "]";
    }

    public Integer getAdminId() {
        return adminId;
    }
    public void setAdminId(Integer adminId) {
        this.adminId = adminId;
    }
    public String getAdminCode() {
        return adminCode;
    }
    public void setAdminCode(String adminCode) {
        this.adminCode = adminCode;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Timestamp getEnrolldate() {
        return enrolldate;
    }
    public void setEnrolldate(Timestamp enrolldate) {
        this.enrolldate = enrolldate;
    }

}
/netctoss-spring/src/main/java/cn/tedu/netctoss/service
ApplicationException.java
package cn.tedu.netctoss.service;
/**
 * 应用异常类
 *
 */
public class ApplicationException 
    extends RuntimeException {

    public ApplicationException() {

    }

    public ApplicationException(
            String message) {
        super(message);
    }

}
LoginService.java
package cn.tedu.netctoss.service;

import cn.tedu.netctoss.entity.Admin;

/**
 * 业务层接口
 *
 */
public interface LoginService {
    public Admin checkLogin(String adminCode,
            String pwd);
}
LoginServiceImpl.java
package cn.tedu.netctoss.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.tedu.netctoss.dao.AdminDAO;
import cn.tedu.netctoss.entity.Admin;

/**
 * 业务层实现
 *
 */
@Service("loginService")
public class LoginServiceImpl implements 
LoginService{

    @Resource(name="adminDAO")
    private AdminDAO aDAO;

    public Admin checkLogin(
            String adminCode, String pwd) {
        Admin admin = 
            aDAO.findByAdminCode(adminCode);
        if(admin == null){
            //帐号不存在,抛一个应用异常。
            //注:应用异常,指的是因为用户
            //不正确的操作引起的异常,比如
            //帐号、密码填写错误。需要明确
            //提示用户采取正确的操作。
            throw new ApplicationException(
                    "帐号错误");
        }
        if(!admin.getPassword().equals(pwd)){
            //密码错误
            throw new ApplicationException(
                    "密码错误");
        }
        //登录成功
        return admin;
    }   
}
/netctoss-spring/src/main/resources
db.properties
# db connection parameters
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
user= 
pwd= 
# datasource parameters
initsize=1
maxsize=2
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置组件扫描 -->
    <context:component-scan base-package="cn.tedu.netctoss" />
    <!-- 配置MVC注解扫描 -->
    <mvc:annotation-driven />
    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 读取db.properties文件 -->
    <util:properties id="db" 
    location="classpath:db.properties" />

    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="#{db.driver}" />
        <property name="url" value="#{db.url}" />
        <property name="username" value="#{db.user}" />
        <property name="password" value="#{db.pwd}" />
    </bean>

</beans>
内容概要:本文档详细介绍了基于弹性架构搜索(Elastic Architecture Search, EAS)结合Transformer编码器进行多变量时间序列预测的项目实例。项目旨在自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性,降低计算资源消耗,实现模型轻量化。通过MATLAB实现,项目采用Transformer编码器的多头自注意力机制,结合EAS的弹性权重共享和分阶段搜索策略,解决了高维多变量时间序列的复杂依赖建模、架构搜索计算资源需求高、模型过拟合、多步预测误差积累、数据异构性与缺失值处理、复杂模型训练收敛等挑战。最终,项目构建了一个高度模块化和可扩展的系统设计,适用于智能制造、能源管理、智慧交通等多个工业场景。 适合人群:具备一定编程基础,对时间序列预测、深度学习及MATLAB有一定了解的研发人员和研究人员。 使用场景及目标:①自动化优化多变量时间序列预测模型结构,提升预测精度与鲁棒性;②降低计算资源消耗,实现模型轻量化;③实现高度模块化与可扩展的系统设计,促进人工智能在工业领域的深度应用;④提供科研与教学的典范案例与工具,探索深度学习架构搜索在时序预测的前沿技术;⑤促进多变量时序数据融合与异质信息处理能力,推动MATLAB深度学习工具箱的应用与扩展。 其他说明:项目不仅聚焦于模型性能提升,更注重计算资源节约和应用落地的可行性。借助弹性架构搜索自动化调参,减少人工经验依赖,加快模型迭代速度,降低开发门槛。结合Transformer编码器的表达能力,显著改善多变量时间序列预测中的长期依赖捕捉和异质数据融合问题,为各类时间序列分析任务提供一种全新的解决方案。项目通过详细的代码实现和注释,帮助用户理解Transformer机制与弹性架构搜索如何协同工作,实现多变量时间序列预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值