ejb 3 开发快速入门 4 将数据保存到数据库中

这一章将要做的是在ejb 3中将数据保存到mysql数据库中

session Bean参考了

http://blog.csdn.net/zzj806683450/article/details/21344853

表结构如下:

CREATE TABLE `staff` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

最好是在数据库中测试下,保证数据库方面应没有什么问题。

工程结构如下:


注意红色的部分,还要加入jboss-client.jar文件(这个文件在JBoss_HOME/bin/client中),persistence.xml是和数据库有关的配置文件。

在ejbModule目录下右键创建session Bean
为session Bean取一个名字,状态这里选择无状态(stateless)的session,并勾上Remote,它会自动帮助我们

生成对应的接口

各个类内容如下:

@Entity
@Table(name="Staff")
public class Staff implements Serializable{
    @Id
    private int id;
    private String username;
    private String password;

.....//要生成get set方法


@Stateless
public class HelloWorldBean implements HelloWorldBeanRemote {
    @PersistenceContext
    (unitName="shopping")
    private EntityManager manager;
    /**
     * Default constructor.
     */
    public HelloWorldBean() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String sayHello(Staff staff) {
        // TODO Auto-generated method stub
        manager.persist(staff);
        return " Success ";  
    }

}



@Remote
public interface HelloWorldBeanRemote {
    public String sayHello(Staff staff);  
}

public class Client {
    public static void main(String[] args) {  
        HelloWorldBeanRemote bean = doLookup();
        Staff staff=new Staff();
        staff.setUsername("xxx");
        staff.setPassword("xxx");
        System.out.println(bean.sayHello(staff));  
    }  
 
    private static HelloWorldBeanRemote doLookup() {  
        HelloWorldBeanRemote bean = null;  
        try {  
            bean = ClientUtility.getInitialContext();  
        } catch (NamingException e) {  
            e.printStackTrace();  
        }  
        return bean;  
    }  
}



public class ClientUtility {  
 
    @SuppressWarnings({ "rawtypes", "unchecked" })  
    public static HelloWorldBeanRemote getInitialContext() throws NamingException {  
        final Hashtable jndiProperties = new Hashtable();  
          
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");  
          
        final Context context = new InitialContext(jndiProperties);  
          
        final String appName = "";  
          
        final String moduleName = "HelloWorldEJB";//工程名,这里写错会出现 IllegalStateException  
          
        final String distinctName = "";  
          
        final String beanName = HelloWorldBean.class.getSimpleName();  
          
        final String viewClassName = HelloWorldBeanRemote.class.getName();  
          
        System.out.println(moduleName);  
        System.out.println(beanName);  
        System.out.println(viewClassName);  
          
        /**
         * 要搞清楚这几个name的具体含义,可以参见如下论坛
         * @see https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t
         */  
        return (HelloWorldBeanRemote) context.lookup("ejb:"+appName+"/"+moduleName+"/"+  
                distinctName+"/"+ beanName + "!"+viewClassName);  
    }  
 


jboss-ejb-client.properties  内容如下:

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence>
   <persistence-unit name="shopping">
         <!-- Transactions of the Java EE server -->
      <jta-data-source>java:jboss/datasources/MysqlDS</jta-data-source>
      <properties>
          <!--
         <property name="org.hibernate.hbm2ddl">update</property>
        -->
      </properties>
   </persistence-unit>
</persistence>
注意各个文件的位置。

最后运行Client类,看数据库中是否有新的数据产生。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值