最近开始学习ejb,写了一个ejb的小demo,新建一个ejb工程,支持jpa1.0,同时配置
好weblogic的数据源,代码目录结构如下:
数据库对应的实体类如下:
@Entity
@Table(name="student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
学生的刀层如下,实现查找所有学生和按照id学生的简单操作。
@Remote(value=IStudentDao.class)
public interface IStudentDao {
public List<Student> getStudentList();
public Student getStudentById(int id);
}
学生刀的实现类如下,采用EntityManager,其中 @PersistenceContext(unitName="EJB_demo")是配置文件persistence.xml中的名字,
其中的sql语句查询的是实体类的名字而不是表名,也不能使用select *
@Stateless(mappedName="StudentDao")
@Remote
public class StudentDao implements IStudentDao {
@PersistenceContext(unitName="EJB_demo")
EntityManager manager;
@SuppressWarnings("unchecked")
@Override
public List<Student> getStudentList() {
Query query=manager.createQuery("SELECT s FROM Student s");
return (List<Student>)query.getResultList();
}
@Override
public Student getStudentById(int id) {
// TODO Auto-generated method stub
return manager.find(Student.class,id);
}
persistence.xml配置文件中的jta-data-source写的是weblogic数据源的jndi名称
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="EJB_demo" transaction-type="JTA">
<jta-data-source>mysql</jta-data-source>
</persistence-unit>
</persistence>
在使用时需要将weblogic安装目录下wlserver_10.3\server\lib下的weblogic.jar导入到项目中,测试代码如下:
public class TestStudentDao {
//jndi上下文对象
InitialContext context=null;
//与weblogic集成的初始化操作
@Before
public void init(){
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
context=new InitialContext(p);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//测试jndi数据源连接
@Test
public void testConn(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
DataSource dataSource=(DataSource)context.lookup("mysql");
conn=dataSource.getConnection();
ps=conn.prepareStatement("SELECT id,name,age FROM STUDENT");
ps.execute();
rs=ps.getResultSet();
while (rs.next()) {
Student student=new Student(rs.getInt("id"),rs.getString("name"),rs.getInt("age"));
System.out.println(student);
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void testGetStudentList(){
try {
IStudentDao studentDao = (IStudentDao)context.lookup("StudentDao#com.landingbj.dao.IStudentDao");
List<Student> students=studentDao.getStudentList();
for (Student student : students) {
System.out.println(student);
}
} catch (NamingException e) {
e.printStackTrace();
}
}
@Test
public void testGetStudentById(){
try {
IStudentDao studentDao = (IStudentDao)context.lookup("StudentDao#com.landingbj.dao.IStudentDao");
Student student=studentDao.getStudentById(1);
System.out.println(student);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
总体来说,刚开始写的ejb小demo就这样,还有我感觉这个编辑器不太好用,排版不行请见谅!