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

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

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

1.png

    数据库对应的实体类如下:

@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就这样,还有我感觉这个编辑器不太好用,排版不行请见谅!




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