EJB+JPA实现总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzq__janeGreen_/article/details/71939711

首先 我遇见了第一个问题就是 为什么我的程序发布的时候一直提示找不到我的源文件jdbc/MySqlDB_pm,首先这里我说明这个数据源我并没有配置,后来我配置了数据源但是不是这个而是jdbc/MySqlDB,然后我i一直调试还是还是提示同样的错误,第二天我重启电脑我发现那个问题不存在了,所以我觉得应该是我没有重启电脑的缘故。

第二个问题就是 我访问我的servlet时一直提示404错误,我发布的时候服务器用的是glassfish4,然后ejb.jar包与演示的动态网页是一起发布的。后面我发现一个问题,因为按照书上的做法,动态网页是已经引用了EJB项目的,但是在发布时候又将ejb.jar包又发布了一次,我觉得这样是错误的,后面我只发布了war包后面真的就访问成功了


以下是我的代码

Student类:


package com;


import java.io.Serializable;
import java.util.Date;
import java.util.List;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;




@Entity
@NamedQuery(name="findAllStudents", query="SELECT s FROM Student s")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private String gender;
@Temporal(TemporalType.DATE)
private Date birthday;



//bi-directional many-to-one association to Address
    
public Student() {
super();
}

public Student(String name,String gender,Date birthday){
this.name=name;
this.gender=gender;
this.birthday=birthday;


}
public Integer getId() {
return this.id;
}


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


public Date getBirthday() {
return this.birthday;
}


public void setBirthday(Date birthday) {
this.birthday = birthday;
}


public String getGender() {
return this.gender;
}


public void setGender(String gender) {
this.gender = gender;
}


public String getName() {
return this.name;
}


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


}


studentBean类:


package com;


import java.util.List;


import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;


/**
 * Session Bean implementation class StudentBean
 */
@Stateless
@LocalBean
public class StudentBean {

@PersistenceContext(unitName="testEJBPU")
private EntityManager em;


    /**
     * Default constructor. 
     */
    public StudentBean() {
   
        // TODO Auto-generated constructor stub
    }
    
    public List<Student> findStudents() {
    TypedQuery<Student> query=em.createNamedQuery("findAllStudents",Student.class);
    return query.getResultList();
    }
    public Student findStudentById(Integer id) {
    return em.find(Student.class,id);
    }
    
    public Student createStudent(Student student) {
    em.persist(student);
    return student;
    }
    
    public void deleteStudent(Student student) {
    em.remove(em.merge(student));
    }
    
    public Student updateStudent(Student student) {
    return em.merge(student);
    }
    
}


服务端类:

package com.wzq;


import java.io.IOException;


import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
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;


import com.Student;
import com.StudentBean;
/**
 * Servlet implementation class QServlet
 */
@WebServlet("/QServlet")
public class QServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
@EJB
StudentBean sBean;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
processRequest(request,response);

}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
processRequest(request,response);
}

protected void  processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
 
PrintWriter out=response.getWriter();
try{
out.println("<html>");
out.println("<head>");
out.println("<title>学生EJB测试</title>");
out.println("</head>");
out.println("<body>");
 
out.println("<h1>插入学生</h1>");
sBean.createStudent(new Student("李铭","男",simpleDateFormat.parse("1995-1-2")));
sBean.createStudent(new Student("丁霞","女",simpleDateFormat.parse("1996-2-2")));
 
out.println("<h1>查询学生</h1>");
List<Student> students=sBean.findStudents();
if(students.size()>0){
out.println("<ul>");
for(Student st:students){
out.println("<p/>");
out.println("<li>"+st.getId()+"</li>");
out.println("<li>"+st.getName()+"</li>");
out.println("<li>"+st.getGender()+"</li>");
out.println("<li>"+st.getBirthday()+"</li>");
}
out.println("</ul>");
out.println("</div>");
}
 
out.println("<h1>修改学生</h1>");
students=sBean.findStudents();
if(students.size()>0){
out.println("<ul>");
for(Student st:students){
if(st.getName().equals("李铭")){
st.setName("黎明");
sBean.updateStudent(st);
}
out.println("<p/>");
out.println("<li>"+st.getId()+"</li>");
out.println("<li>"+st.getName()+"</li>");
out.println("<li>"+st.getGender()+"</li>");
out.println("<li>"+st.getBirthday()+"</li>");
}
out.println("</ul>");
out.println("</div>");
}
// out.println("<h1>删除学生</h1>");
// students=sBean.findStudents();
// for(Student st:students){
// sBean.updateStudent(st);
// }
 
     out.println("</body>");
out.println("</html>");

}catch(Exception e){
e.printStackTrace();
}finally{
out.close();
}
}

}


持久化xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="testEJBPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/MySqlDB</jta-data-source>

<class>com.Student</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/></properties>
</persistence-unit>
</persistence>

展开阅读全文

没有更多推荐了,返回首页