[b]一、JPA2.0简介[/b]
JPA2.0 加入了大量必须的 ORM 映射增强特性,包括:支持通过使用 @ElementCollection 标注来增强 collections, maps 和 lists( 这里不是指实体之间的关联关系 ) 集合,支持 map 的单向 one-to-many 关联 (JPA1.0 只允许双向 one-to-many 关联 ) .
EntityManager 和 Query API 都得到改进。比如说,现在可以从结果集中直接取得第一条记录 (JPA1.0 只允许从一个 unique 结果集中反回单个记录 ) ,指定 query 结果集的最大值,访问各个供应商的底层实体对象 manager 或 query ,最后就是加入悲观锁 (JPA1.0 只支持乐观锁 ) .
JPQL 也提供类似于 SQL 的 CASE , NULLIF , COALESCE 等函数 .
JPA2.0 应广大开发者要求增加了 Criteria API .要是你对 Hibernate 或 TopLink 的 Criteria API 不熟悉的话,可以将它想像成一个以 Java 为中心的面向对象,线程安全并可以与 JPQL 划上等号的一组 API .这组 API 适合于编写复杂的动态查询语句,还可避免解析 JPQL 语句时,所抛出的运行期异常。
更完整的 JAP2.0 特性还包括:标准的二级缓存,标准的 JDBC properties ,指定超时时间等等。你可以随时通过下面的 JSR 站点看看关于 JPA2.0 公开草案的更多细节: [url]http://jcp.org/en/jsr/detail?id=317[/url]
[b]二、新建JPA2.0项目[/b]
按快捷键ctrl+shift+n打开新建项目的对话框如下图:
[img]http://dl.iteye.com/upload/attachment/182582/c31817e2-f8f1-3348-ab76-bd25d3b0bc5b.jpg[/img]
选择普通的WEB项目,点击“下一步”,弹出如下图对话框:
[img]http://dl.iteye.com/upload/attachment/182584/1d31547e-d4d4-3ce6-b7c4-9f0b7dedb4c8.jpg[/img]
在项目名称中输入“testjap2”点击“下一步”,弹出如图对话框:
[img]http://dl.iteye.com/upload/attachment/182586/6ada5434-4799-3557-b09c-c5c6c29aef6e.jpg[/img]
在服务器选择“GlashFish V3”,在Java EE版本选择“Java EE 6 Web”;在上下文路径选择“/testjap2”点击完成。
[b]三、新建数据表[/b]
我在Sql Server数据库demo中新建一个表user代码如下:
create table [user]
(
userid int identity(1,1) not null primary key,
usercode varchar(60),
username varchar(60),
password varchar(100),
email varchar(100),
remark varchar(200)
);
[b]四、创建持久层[/b]
新建三个包com.ejb、com.pojo、com.servlet在com.pojo点击右键选择“新建”—>“通过数据库生成实体类”如下图:
[img]http://dl.iteye.com/upload/attachment/182588/49a60a82-b28c-3cca-968a-f4983daa7369.jpg[/img]
在弹出对话框中选择自己建立的数据源并选择表User如下图:
[img]http://dl.iteye.com/upload/attachment/182590/3dad3f54-3220-360e-bbd8-1f86a9d87fb0.jpg[/img]
点击“下一步”弹出如下对话框:
[img]http://dl.iteye.com/upload/attachment/182592/b0963317-4fa2-3581-8ab8-da64c56ce310.jpg[/img]
点击创建持久性单元弹出如下图对话框:
[img]http://dl.iteye.com/upload/attachment/182596/603ea068-f61d-3129-96cd-2f1b717709de.jpg[/img]
持久性单元名称输入:“testja2PU”,持久性提供器:Eclipselink、数据源选择jdbc/demo点击“创建”后点击“完成”。
[b]五、创建数据库访问层。[/b]
在com.ejb包单击右键“新建”—>“会话EJB”如下图对话框:
[img]http://dl.iteye.com/upload/attachment/182600/020ce0ff-2f05-335b-9349-bcda9a5b16a4.jpg[/img]
则弹出如下图对话框:
[img]http://dl.iteye.com/upload/attachment/182602/d08e192b-396c-3f84-be96-a82aea8f9beb.jpg[/img]
在EJB名称中输入:UserDao、在会话类型中选择“无状态”点击确定。
成生成代码下增加一些代码结果如下:
package com.ejb;
import com.pojo.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class Userdao {
@PersistenceContext(unitName = "testjap2PU")
private EntityManager em;
public void save(User entity)
{
this.em.persist(entity);
}
public void delete(User entity)
{
em.remove(entity);
}
public void update(User entity)
{
em.merge(entity);
}
public List<User> getAll()
{
Query query =em.createNamedQuery("User.findAll");
return query.getResultList();
}
public User getById(int id)
{
return (User)em.createNamedQuery("User.findByUserid").getSingleResult();
}
public User getByUserCode(String usercode)
{
Query query = em.createNamedQuery("User.findByUsercode");
return (User)query.getSingleResult();
}
public User getByUserName(String username)
{
Query query = em.createNamedQuery("User.findByUsername");
return (User)query.getSingleResult();
}
}
@Stateless定义了一个会话Bean, @PersistenceContext(unitName = "testjap2PU")持久性单元上下文环境注入了SessionBean。
[b]六、新建Servlet测试类。[/b]
在com.servlet 包点击右键新建Servlet.如下代码:
package com.servlet;
import com.ejb.Userdao;
import com.pojo.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
public class TestServlet extends HttpServlet {
@EJB
private Userdao userDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String msg = "";
String para = request.getParameter("methed");
if (para.equals("insert")) {
String username = request.getParameter("username");
String usercode = request.getParameter("usercode");
String password = request.getParameter("password");
String email = request.getParameter("email");
User user = new User();
user.setUsercode(usercode);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
this.userDao.save(user);
msg = "插入成功:" + username;
}
if (para.equals("update")) {
String username = request.getParameter("username");
String usercode = request.getParameter("usercode");
String password = request.getParameter("password");
String email = request.getParameter("email");
User user = new User();
user.setUsercode(usercode);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
this.userDao.save(user);
msg = "修改成功:" + username;
}
if (para.equals("delete")) {
String usercode = request.getParameter("usercode");
User user = this.userDao.getByUserCode(usercode);
if (user != null) {
this.userDao.delete(user);
}
msg = "删除成功:" + user.getUsername();
}
if (para.equals("findall")) {
List<User> list = this.userDao.getAll();
for(User user :list)
{
msg += "—>"+user.getUsername();
}
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println(msg);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String mothed = request.getParameter("mothed");
if (mothed.equals("insert")) {
processRequest(request, response);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
七、测试
保存启动GlassFish3,启动浏览器;
输入:http://localhost:8080/testjpa2/TestServlet?methed=insert?usercode=zhangshan&username=张三&password=1234&email=zhangshan@gmail.com则页面则提示“插入成功”;
输入:http://localhost:8080/testjpa2/TestServlet?methed=update?usercode= zhangshan &username=张三&password=123456&email=zhangshan@sina.com则页面提示“修改成功”;
输入:http://localhost:8080/testjpa2/TestServlet?methed=delete?usercode= zhangshan则页面提示“删除成功”;
输入:http://localhost:8080/testjpa2/TestServlet?methed= findall则页面显示所有用户名称。
对数据的操作感觉如Hibernate一样的舒服!呵呵。。。。。下一节继续体验rest full webservice。