这一章将要做的是在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类,看数据库中是否有新的数据产生。