⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Accessing a Database</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="Craig R. McClanahan" name="author" />
<meta content="Mike Schachter" name="author" />
<meta content="Ted Husted" name="author" />
<meta content="Martin Cooper" name="author" />
<meta content="Ed Burns" name="author" />
<meta content="Anthony Kay" name="author" />
<link href="../struts.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="heading">
<a href="http://apache.org/">
<img id="asf_logo_wide" alt="The Apache Project" src="../images/asf_logo_wide.gif" />
</a>
<a href="http://struts.apache.org/">
<img id="struts-logo" alt="Struts Framework" src="../images/struts.gif" />
</a>
</div>
<!--end heading-->
<div id="content">
<div id="menu">

    

    <p>FAQs</p>
<ul>
        <li>
<a href="kickstart.html">Kickstart</a>
</li>
        <li>
<a href="newbie.html">Newbie</a>
</li>
        <li>
<a href="helping.html">How to Help</a>
</li>
    </ul>

    <p>Howto Guides</p>
<ul>
        <li>
<a href="actionForm.html">Action Forms</a>
</li>
        <li>
<a href="apps.html">Building Apps</a>
</li>
        <li>
<a href="database.html">Database</a>
</li>
        <li>
<a href="indexedprops.html">Indexed Properties</a>
</li>
        <li>
<a href="ssl.html">SSL</a>
</li>

        <li>
<a href="struts-el.html">Struts-EL (JSTL)</a>
</li>
    </ul>

    <p>IDE Guides</p>
<ul>
        <li>
<a href="eclipse.html">Eclipse</a>
</li>
        <li>
<a href="netbeans.html">Netbeans</a>
</li>
    </ul>

    <p>Quick Links</p>
<ul>
        <li>
<a href="../index.html">Welcome</a>
</li>

        <li>
<a href="../userGuide/index.html">User and Developer Guides</a>
</li>
    </ul>

<div class="authors">
<p>
<strong>Contributors</strong>
</p>
<ul>
<li>Craig R. McClanahan</li>
<li>Mike Schachter</li>
<li>Ted Husted</li>
<li>Martin Cooper</li>
<li>Ed Burns</li>
<li>Anthony Kay</li>
</ul>
</div>
</div>
<!--end menu-->
<div id="main">
<h1 id="actionForm">How to Access a Database</h1>
<h2 id="database">Accessing a Database</h2>
<div class="indent">

    <p>
    Most developers would consider accessing a database part of the "business
    end" of an application. Most often, we don't access a databse for the sake
    of accessing a database. We use the database as part of a larger business
    transaction. So lets start with accessing business logic from Struts.
    </p>

    <p>
    The best thing is use the Action as a thin adaptor between the
    web/presentation-tier and your business classes (including those that
    access a database).
    </p>

    <p>
    So you first design a business API that uses plain Java classes.
    The best thing is to use objects that take ordinary Java types and return
    a JavaBean or collection of JavaBeans.
    The Action then calls these objects and passes the result back to the
    web/presentation tier.
    </p>

    <p>
    A common approach is to create an Action class for each of business
    transaction, or use case, in your application. A simple "CRUD"
    application might have a CreateAction, a RetrieveAction, an UpdateAction,
    and a DeleteAction. To complete each transaction, the Action can make
    whatever calls are needed to your business API classes.
    </p>

    <p>
    Ideally, all the database access code should be encapsulated behind the
    business API classes, so Struts doesn't know what persistent layer you
    are using (or even if there is a persistence layer).
    It just passes a key or search String and gets back a bean or collection
    of beans.
    This lets you use the same business API classes in other environments,
    and also to run unit tests against your business API outside of Struts or
    a HTTP environment.
    </p>

    <p>
    The MailReader example application bundled with Struts demonstrates
    how this is usually done. The MailReader uses the DAO (Data Access Object)
    pattern to separate the persistence layer from the (Struts) control layer.
    MailReader defines a DAO interface that the Actions can call, it then
    defines a implementation that uses a database stored in main memory.
    Other implementations could be defined and used instead, without
    changing any of the Struts Action classes.
    </p>

    <p>
    To get started, it's simplest to setup a 1:1 correspondence between the
    Actions and your application's use cases. Each use case may make one or
    more calls to your business API, but from the user's perspective, each
    use case is a single transaction.
    </p>

    <p>
    As you gain experience, you will find ways to combine your Action classes,
    say by using the DispatchAction.
    It's even possible to use a single "framework" Action to call all of your
    business classes, as is done with Scaffold ProcessAction in the contrib
    folder.
    </p>

    <p>
    Using fewer Actions does require a deeper understanding of how Struts and
    MVC frameworks operate.
    Don't hesitate to err on the side of creating more Action classes at first.
    The Struts configuration makes it easy to refactor your Actions later,
    since you can change the Action type without changing anything else in the
    application.
    </p>

    </div>
<h2 id="datasources">Using DataSources</h2>
<div class="indent">

    <p>
    When you use the DAO approach, all of the database access details are
    hidden behind the business interface. The implementation of the business
    classes handle all the gritty details, like using a <code>DataSource</code>
    to pool connections to the database.
    </p>

    <p>
    As a rule, you should always use a connection pool to access a database.
    The <code>DataSource</code> interface is the preferred way to implement a
    connection pool today. Many containers and database systems now bundle
    a DataSource implmentation that you can use. Most often, the DataSource
    is made available through JNDI. The JNDI approach makes it easy for your
    business classes to access the DataSource without worrying about who set it
    up.
    </p>

    </div>
<h2 id="persistence">Persistence Franeworks</h2>
<div class="indent">

    <p>
    There are many useful and mature persistence layer frameworks available.
    Before using raw JDBC or "rolling your own" solution, you should carefully
    review one or more of these packages. Here's a short list of packages
    most often mentioned on the Struts User list:
    </p>

    <ul>
        <li>
<a href="http://www.hibernate.org/">Hibernate</a>
</li>
        <li>
<a href="http://sourceforge.net/projects/ibatisdb">iBATIS</a>
</li>
        <li>
<a href="http://db.apache.org/ojb/">Object Relational Bridge</a>
</li>
        <li>
<a href="http://db.apache.org/torque/index.html">Torque / Peers</a>
</li>
    </ul>

    <p>
    For more, see the
    <a href="http://struts.sourceforge.net/community/models.html">Struts
    Community Resources area</a> on SourceForge.
    </p>

    </div>
<h2 id="manager">The Struts DataSource Manager</h2>
<div class="indent">

    <p>
    Ideally, the business logic layer should encapsulate the data access
    details, including acquiring a database connection.
    However, some older application designs expect that the caller be able to
    provide a database connection or DataSource instance.
    When you need to access a legacy design, the Struts DataSource manager can
    make it easy for your Action class to produce these resources on demand.
    </p>

    <p>
    <strong>NOTE:</strong> It is preferred that data connectivity be handled
    directly by the business classes, usually via JNDI. The Struts DataSource
    manager should only be used with legacy business classes that don't provide
    their own  connectivity. When possible, we <b>strongly</b> recommend use of
    the standard DAO pattern, so that the Action classes do not need to know
    anything about the persitence mechanism. <strong>The DataSource manager is being
    retained in Struts 1.x for backward compatibility but may not be retained
    in Struts 2.x or later.</strong>
    </p>

    <p>
    The Struts DataSource manager is configured as an element in the
    <a href="../userGuide/configuration.html#data-source_config">
    Struts configuration file</a> (struts-config.xml).
    The manager can be used to deploy any connection pool that implements the
    <code>javax.sql.DataSource</code> interface and is configurable totally

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -