最近开始学习ejb,写了一个ejb的小demo,新建一个ejb工程,支持jpa1.0,同时配置
好weblogic的数据源,代码目录结构如下:
数据库实体类:
@Entity
@Table(name="student")
public class Students implements Serializable {
private static final long serialVersionUID = 1L;
private int stuId;
private String name;
private String age;
public Students() {
}
public Students(int stuId, String name, String age) {
this.stuId = stuId;
this.name = name;
this.age = age;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
public int getStuId() {
return stuId;
}
public void setId(int stuId) {
this.stuId = stuId;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [stuId=" + stuId + ", name=" + name + ", age=" + age + "]";
}
}
StudentDao如下,实现查找所有学生和按照id学生的简单操作。
@Remote(value=StudentDao.class)
public interface StudentDao {
public List<Students> getStudentList();
public Students getStudentById(int stuId);
}
StudentDao 实现类如下:
@Stateless(mappedName="StudentDaoImpl")
@Remote
public class StudentDaoImpl implements StudentDao {
@PersistenceContext(unitName="ejb_demo")
EntityManager manager;
@SuppressWarnings("unchecked")
@Override
public List<Students> getStudentList() {
Query query=manager.createQuery("SELECT s FROM Students s");
return (List<Students>)query.getResultList();
}
@Override
public Students getStudentById(int stuId) {
// TODO Auto-generated method stub
return manager.find(Students.class,stuId);
}
}
persistence.xml配置文件
<?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>test</jta-data-source>
</persistence-unit>
</persistence>
在使用时需要将weblogic安装目录下wlserver\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 stuId,name,age FROM STUDENTS");
ps.execute();
rs=ps.getResultSet();
while (rs.next()) {
Students student=new Students(rs.getInt("stuId"),rs.getString("name"),rs.getString("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 {
StudentDao studentDao = (StudentDao)context.lookup("StudentDaoImpl#com.landingbj.dao.StudentDao");
List<Students> students=studentDao.getStudentList();
for (Students student : students) {
System.out.println(student);
}
} catch (NamingException e) {
e.printStackTrace();
}
}
@Test
public void testGetStudentById(){
try {
StudentDao studentDao = (StudentDao)context.lookup("StudentDaoImpl#com.landingbj.dao.StudentDao");
Students student=studentDao.getStudentById(1);
System.out.println(student);
} catch (NamingException e) {
e.printStackTrace();
}
}
}