Java--servlet + ThreadLocal解决多线程并发问题及实例

本文通过思维导图详细介绍了如何利用Java的ThreadLocal解决Servlet中的多线程并发问题。核心内容包括:ThreadLocal原理、实体层、DAO层、业务逻辑层和服务控制层的实现,特别关注了线程池中datasource的ThreadLocalData管理。此外,还提供了JSP页面result.jsp的使用以及后续将涉及Ajax引擎的内容。
摘要由CSDN通过智能技术生成

思维导图 – Servlet+ThreadLocal分包管理实例


一:本文思维导图
文件结构图


二:ThreadLocal原理图
ThreadLocal原理图



三:代码:


实体层entity Book.java

package entity;

public class Book {
   
    /*私有属性*/
    private int id;
    private String name;
    private String author;
    private String publisher;
    private int price;

    /*默认构造函数*/
    public Book() {
        super();
    }

    public Book(String name, String author, String publisher, int price) {
        super();
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.price = price;
    }

    public Book(int id, String name, String author, String publisher, int price) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
        this.publisher = publisher;
        this.price = price;
    }

    /*对象属性操作*/
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

数据访问层data access object(dao)—BaseDao.java

package dao;

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

public class BaseDao {
   
    // 数据库驱动
    private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    // 连接数据库的url路径
    private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";

    // 用户名
    private s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值