java:面试题

1、union 和 union all 有什么不同?

UNION 在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复 的记录再返回结果。

而UNION ALL只是简单的将两个结果合并后就返回。这样,如果返回的两个结果集中有重复的数据,那么返回的结果集就会包含重复的数据了。

从效率上说,UNION ALL 要比 UNION 快很多,所以,如果可以确认合并的两个结果集中不包含重复的数据的话,那么就使用 UNION ALL。

2、说出数据连接池的工作机制是什么?

J2EE 服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。当使用的池连接调用完成后,池驱动程序将此连接表记为空闲, 其他调用就可以使用这个连接。

实现方式,返回的 Connection 是原始 Connection 的代理,代理 Connection 的 close 方法不是真正关连接,而是把它代理的 Connection 对象还回到连接池中。

3、为什么要用 ORM? 和 JDBC 有何不一样?

orm 是一种思想,就是把 object 转变成数据库中的记录,或者把数据库中的记录转变成 object,我们可以用 jdbc来实现这种思想,其实,如果我们的项目是严格按照 oop 方式编写的话,我们的 jdbc 程序不管是有意还是无意,就已经在实现 orm 的工作了。现在有许多 orm 工具,它们底层调用 jdbc 来实现了 orm 工作,我们直接使用这些工具,就省去了直接使用 jdbc的繁琐细节,提高了开发效率。

4、xml 有哪些解析技术?区别是什么?

有 DOM,SAX,STAX 等

DOM:处理大型文件时其性能下降的非常厉害。这个问题是由 DOM 的树结构所造成的,这种结构占用的内存较多, 而且 DOM 必须在解析文件之前把整个文档装入内存,适合对 XML 的随机访问。

SAX:不同于 DOM,SAX 是事件驱动型的 XML 解析方式。它顺序读取 XML 文件,不需要一次全部装载整个文件。当遇到像文件开头,文档结束,或者标签开头与标签结束时,它会触发一个事件,用户通过在其回调事件中写入处理代码来处理 XML 文件,适合对 XML 的顺序访问。

STAX:Streaming API for XML (StAX)

5、你在项目中用到了 xml 技术的哪些方面?如何实现的?

用到了数据存贮,信息配置两方面。

在做数据交换平台时,将不同数据源的数据组装成 XML 文件,然后将 XML 文件压缩打包加密后通过网络传送给接收者,接收解密与解压缩后再同 XML 文件中还原相关信息进行处理。在 做软件配置时,利用 XML 可以很方便的进行,软件的各种配置参数都存贮在 XML 文件中。

6、用 jdom 解析 xml 文件时如何解决中文问题?如何解析?

public class DOMTest {
private String inFile = "c:\\people.xml"
private String outFile = "c:\\people.xml"
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span> <span class="n">args</span><span class="o">[])</span> <span class="o">{</span>
<span class="k">new</span> <span class="n">DOMTest</span><span class="o">();</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nf">DOMTest</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">DocumentBuilder</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">DocumentBuilderFactory</span><span class="o">.</span><span class="na">newInstance</span><span class="o">().</span><span class="na">newDocumentBuilder</span><span class="o">();</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Document</span> <span class="n">doc</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="na">newDocument</span><span class="o">();</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">root</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"老师"</span><span class="o">);</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">wang</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"王"</span><span class="o">);</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">liu</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"刘"</span><span class="o">);</span>
<span class="n">wang</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">doc</span><span class="o">.</span><span class="na">createTextNode</span><span class="o">(</span><span class="s">"我是王老师"</span><span class="o">));</span>
<span class="n">root</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">wang</span><span class="o">);</span>
<span class="n">doc</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">root</span><span class="o">);</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">Transformer</span> <span class="n">transformer</span> <span class="o">=</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">TransformerFactory</span><span class="o">.</span><span class="na">newInstance</span><span class="o">().</span><span class="na">newTransformer</span><span class="o">();</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">setOutputProperty</span><span class="o">(</span><span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">OutputKeys</span><span class="o">.</span><span class="na">ENCODING</span><span class="o">,</span> <span class="s">"gb2312"</span><span class="o">);</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">setOutputProperty</span><span class="o">(</span><span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">OutputKeys</span><span class="o">.</span><span class="na">INDENT</span><span class="o">,</span> <span class="s">"yes"</span><span class="o">);</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="k">new</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">DOMSource</span><span class="o">(</span><span class="n">doc</span><span class="o">),</span>
<span class="k">new</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">stream</span><span class="o">.</span><span class="na">StreamResult</span><span class="o">(</span><span class="n">outFile</span><span class="o">));</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span> <span class="n">args</span><span class="o">[])</span> <span class="o">{</span>
<span class="k">new</span> <span class="n">DOMTest</span><span class="o">();</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nf">DOMTest</span><span class="o">()</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">DocumentBuilder</span> <span class="n">builder</span> <span class="o">=</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">parsers</span><span class="o">.</span><span class="na">DocumentBuilderFactory</span><span class="o">.</span><span class="na">newInstance</span><span class="o">().</span><span class="na">newDocumentBuilder</span><span class="o">();</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Document</span> <span class="n">doc</span> <span class="o">=</span> <span class="n">builder</span><span class="o">.</span><span class="na">newDocument</span><span class="o">();</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">root</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"老师"</span><span class="o">);</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">wang</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"王"</span><span class="o">);</span>
<span class="n">org</span><span class="o">.</span><span class="na">w3c</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">Element</span> <span class="n">liu</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="na">createElement</span><span class="o">(</span><span class="s">"刘"</span><span class="o">);</span>
<span class="n">wang</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">doc</span><span class="o">.</span><span class="na">createTextNode</span><span class="o">(</span><span class="s">"我是王老师"</span><span class="o">));</span>
<span class="n">root</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">wang</span><span class="o">);</span>
<span class="n">doc</span><span class="o">.</span><span class="na">appendChild</span><span class="o">(</span><span class="n">root</span><span class="o">);</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">Transformer</span> <span class="n">transformer</span> <span class="o">=</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">TransformerFactory</span><span class="o">.</span><span class="na">newInstance</span><span class="o">().</span><span class="na">newTransformer</span><span class="o">();</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">setOutputProperty</span><span class="o">(</span><span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">OutputKeys</span><span class="o">.</span><span class="na">ENCODING</span><span class="o">,</span> <span class="s">"gb2312"</span><span class="o">);</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">setOutputProperty</span><span class="o">(</span><span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">OutputKeys</span><span class="o">.</span><span class="na">INDENT</span><span class="o">,</span> <span class="s">"yes"</span><span class="o">);</span>
<span class="n">transformer</span><span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="k">new</span> <span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">dom</span><span class="o">.</span><span class="na">DOMSource</span><span class="o">(</span><span class="n">doc</span><span class="o">),</span>
<span class="k">new</span>
<span class="n">javax</span><span class="o">.</span><span class="na">xml</span><span class="o">.</span><span class="na">transform</span><span class="o">.</span><span class="na">stream</span><span class="o">.</span><span class="na">StreamResult</span><span class="o">(</span><span class="n">outFile</span><span class="o">));</span>
<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="na">getMessage</span><span class="o">());</span>
<span class="o">}</span>
<span class="o">}</span>
}

7、编程用 JAVA 解析 XML 的方式。

<?xml version="1.0" encoding="utf-8"?>
<person>
<name>王小明</name>
<college>信息学院</college>
<telephone>6258113</telephone>
<notes>男,1955 年生,博士,95 年调入海南大学</notes>
</person>
package com.example.springbootgit.test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import org.xml.sax.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class XmlTest extends HandlerBase {
private Hashtable table = new Hashtable();
private String currentElement = null;
private String currentValue = null;
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setTable</span><span class="o">(</span><span class="n">Hashtable</span> <span class="n">table</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">table</span> <span class="o">=</span> <span class="n">table</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">Hashtable</span> <span class="nf">getTable</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">table</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">startElement</span><span class="o">(</span><span class="n">String</span> <span class="n">tag</span><span class="o">,</span> <span class="n">AttributeList</span> <span class="n">attrs</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="n">currentElement</span> <span class="o">=</span> <span class="n">tag</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">characters</span><span class="o">(</span><span class="kt">char</span><span class="o">[]</span> <span class="n">ch</span><span class="o">,</span> <span class="kt">int</span> <span class="n">start</span><span class="o">,</span> <span class="kt">int</span> <span class="n">length</span><span class="o">)</span>
<span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="n">currentValue</span> <span class="o">=</span> <span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="n">ch</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">length</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">endElement</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">currentElement</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">name</span><span class="o">))</span> <span class="o">{</span>
<span class="n">table</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">currentElement</span><span class="o">,</span> <span class="n">currentValue</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span><span class="o">,</span> <span class="n">ParserConfigurationException</span><span class="o">,</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="c1">//文件形式
File file = new File("c:\people.xml");
FileReader reader = new FileReader(file);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XmlTest xmlTest = new XmlTest();
sp.parse(new InputSource(reader), xmlTest);
Hashtable hashTable = xmlTest.getTable();
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"name"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"college"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"telephone"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"notes"</span><span class="o">)));</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setTable</span><span class="o">(</span><span class="n">Hashtable</span> <span class="n">table</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">table</span> <span class="o">=</span> <span class="n">table</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">Hashtable</span> <span class="nf">getTable</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">table</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">startElement</span><span class="o">(</span><span class="n">String</span> <span class="n">tag</span><span class="o">,</span> <span class="n">AttributeList</span> <span class="n">attrs</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="n">currentElement</span> <span class="o">=</span> <span class="n">tag</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">characters</span><span class="o">(</span><span class="kt">char</span><span class="o">[]</span> <span class="n">ch</span><span class="o">,</span> <span class="kt">int</span> <span class="n">start</span><span class="o">,</span> <span class="kt">int</span> <span class="n">length</span><span class="o">)</span>
<span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="n">currentValue</span> <span class="o">=</span> <span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="n">ch</span><span class="o">,</span> <span class="n">start</span><span class="o">,</span> <span class="n">length</span><span class="o">);</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">endElement</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">currentElement</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">name</span><span class="o">))</span> <span class="o">{</span>
<span class="n">table</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">currentElement</span><span class="o">,</span> <span class="n">currentValue</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span><span class="o">,</span> <span class="n">ParserConfigurationException</span><span class="o">,</span> <span class="n">SAXException</span> <span class="o">{</span>
<span class="c1">//文件形式
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"name"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"college"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"telephone"</span><span class="o">)));</span>
<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">hashTable</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="k">new</span> <span class="n">String</span><span class="o">(</span><span class="s">"notes"</span><span class="o">)));</span>
<span class="o">}</span>
}

8、XML 文档定义有几种形式?它们之间有何本质区别?解析 XML 文档有哪几种方式?

两种形式:dtd、schema

本质区别:schema 本身是 xml 的,可以被 XML 解析器解析(这也是从 DTD 上发 展 schema 的根本目的),

有 DOM,SAX,STAX 等
DOM:处理大型文件时其性能下降的非常厉害。这个问题是由 DOM 的树结构所造成的,这种结构占用的内存较多,而且 DOM 必须在解析文件之前把整个文档装入内存,适合对 XML 的随机访问
SAX:不现于 DOM,SAX 是事件驱动型的 XML 解析方式。它顺序读取 XML 文件,不需要一次全部装载整个文 件。当遇到像文件开头,文档结束,或者标签开头与标签结束时,它会触发一个事件,用户通过在其回调事 件中写入处理代码来处理 XML 文件,适合对 XML 的顺序访问
STAX:Streaming API for XML (StAX)

9、谈谈你对 Struts 的理解。

1. struts 是一个按 MVC 模式设计的 Web 层框架,其实它就是一个大大的 servlet,这个 Servlet 名为 ActionServlet, 或是 ActionServlet 的子类。我们可以在 web.xml 文件中将符合某种特征的所有请求交给这个 Servlet 处理,这个 Servlet 再参照一个配置文件(通常为/WEB-INF/struts-config.xml)将各个请求分别分配给不同的 action 去处理。 一个扩展知识点:struts 的配置文件可以有多个,可以按模块配置各自的配置文件,这样可以防止配置文件的过度膨胀。

2. ActionServlet 把请求交给 action 去处理之前,会将请求参数封装成一个 formbean 对象(就是一个 java 类,这个 类中的每个属性对应一个请求参数),封装成一个什么样的 formbean 对象呢?看配置文件。 3.要说明的是,ActionServlet把formbean对象传递给action的execute方法之前,可能会调用formbean的validate 方法进行校验,只有校验通过后才将这个 formbean 对象传递给 action 的 execute 方法,否则,它将返回一个错误页面,这个错误页面由 input 属性指定,(看配置文件)作者为什么将这里命名为 input 属性,而不是 error 属性。

4.action 执行完后要返回显示的结果视图,这个结果视图是用一个 ActionForward 对象来表示的,actionforward 对 象通过struts-config.xml配置文件中的配置关联到某个jsp页面,因为程序中使用的是在struts-config.xml配置文件 为 jsp 页面设置的逻辑名,这样可以实现 action 程序代码与返回的 jsp 页面名称的解耦。

10、谈谈你对 Hibernate 的理解。

1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象、建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管程序运行的过程和操作怎么样,本质上都是要得到一个结果, 程序上一个时刻和下一个时刻的运行结果的差异就表现在内存中的对象状态发生了变化。

2.为了在关机和内存空间不够的状况下,保持程序的运行状态,需要将内存中的对象状态保存到持久化设备和从持久化设备中恢复出对象的状态,通常都是保存到关系数据库来保存大量对象信息。从 Java 程序的运行功能上来讲,保存对象状态的功能相比系统运行的其他功能来说,应该是一个很不起眼的附属功能,java 采用 jdbc 来实现 这个功能,这个不起眼的功能却要编写大量的代码,而做的事情仅仅是保存对象和恢复对象,并且那些大量的 jdbc 代码并没有什么技术含量,基本上是采用一套例行公事的标准代码模板来编写,是一种苦活和重复性的工作。

3.通过数据库保存 java 程序运行时产生的对象和恢复对象,其实就是实现了 java 对象与关系数据库记录的映射关 系,称为 ORM(即 Object Relation Mapping),人们可以通过封装 JDBC 代码来实现了这种功能,封装出来的产品称之为 ORM 框架,Hibernate 就是其中的一种流行 ORM 框架。使用 Hibernate 框架,不用写 JDBC 代码,仅仅是调用一个 save 方法,就可以将对象保存到关系数据库中,仅仅是调用一个 get 方法,就可以从数据库中加载出一 个对象。

4.使用 Hibernate 的基本流程是:配置 Configuration 对象、产生 SessionFactory、创建 session 对象,启动事务,完 成 CRUD 操作,提交事务,关闭 session。

5.使用 Hibernate 时,先要配置 hibernate.cfg.xml 文件,其中配置数据库连接信息和方言等,还要为每个实体配置 相应的 hbm.xml 文件,hibernate.cfg.xml 文件中需要登记每个 hbm.xml 文件。

6.在应用 Hibernate 时,重点要了解 Session 的缓存原理,级联,延迟加载和 hql 查询。

本文来自网络,不代表软件信息网立场,转载请注明出处。软件定制开发交流:15528175269(微信同号)https://www.saasyo.com/xz/15981.html

作者: 王鹏程序员

上一篇
下一篇
联系我们

联系我们

15889726201

在线咨询: QQ交谈

邮箱: 187395037@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部