[原创]简单的EJB测试_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2276 | 回复: 0   主题: [原创]简单的EJB测试        下一篇 
jun.wu
注册用户
等级:上尉
经验:535
发帖:24
精华:0
注册:1970-1-1
状态:离线
发送短消息息给jun.wu 加好友    发送短消息息给jun.wu 发消息
发表于: IP:您无权察看 2017-4-24 14:44:15 | [全部帖] [楼主帖] 楼主

最近开始学习ejb,写了一个ejb的小demo,新建一个ejb工程,支持jpa1.0,同时配置

好weblogic的数据源,代码目录结构如下:


blob.png

数据库实体类:

@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();

    }

}

}






赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论