标签:
1 @Entity //表明这是一个实体Bean 2 @Table (name = "student" ) //和数据库表student 建立映射 3 public class StudentEntity implements Serializable { 4 5 private static final long serialVersionUID = 4002145187978562529L; 6 7 @Id // 表明是该实体的id 8 @GeneratedValue(strategy = GenerationType. AUTO ) //id生成策略 9 @Column(name = "id" )//对应student表id字段 10 private int id ; 11 12 @Column(name = "name" ) // 对应student表name字段 13 private String name; 14 15 public int getId() { 16 return id ; 17 } 18 19 public String getName() { 20 return name ; 21 } 22 23 public void setId(int id) { 24 this .id = id; 25 } 26 27 public void setName(String name) { 28 this .name = name; 29 } 30 31 }
1 public interface BaseOperation { 2 3 public List<?> findAll(); 4 }
1 @Stateless //这是一个无状态Bean 2 @Remote (BaseOperation. class) //指明Bean的remote接口 3 public class StudentDaoBean implements BaseOperation { 4 5 // EntityManager是由EJB容器自动配置和管理的,unitName属性的值对应 6 persistence.xml 中< persistence-unit name = "MyEJBProject" transaction-type = "JTA" ></ persistence-unit > name的配置 7 @PersistenceContext(unitName = "MyEJBProject" ) 8 private EntityManager em; 9 10 @SuppressWarnings( "unchecked" ) 11 public List<?> findAll() { 12 System. out .println("查询开始..." ); 13 List<StudentEntity> list = em.createQuery( " from StudentEntity ").getResultList(); 14 if (list != null) { 15 Iterator<StudentEntity> it = list.iterator(); 16 while (it.hasNext()) { 17 StudentEntity student = it.next(); 18 System. out .println("学生id:" + student.getId()); 19 System. out .println("学生名称:" + student.getName()); 20 } 21 } 22 System. out .println("查询完毕...." ); 23 return list; 24 } 25 26 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <module xmlns="urn:jboss:module:1.1" name="com.mysqldatabase.mysql"> 3 <resources> 4 <resource-root path="mysql-connector-java-5.**-bin.jar"/> 5 </resources> 6 <dependencies> 7 <module name="javax.api"/> 8 <module name="javax.transaction.api"/> 9 <module name="javax.servlet.api" optional="true"/> 10 </dependencies> 11 </module>
1 <datasources> 2 <datasource jndi-name="java:jboss/KouMySQLDS" pool-name="MySQLDS" enabled="true" use-java-context="true"> 3 <connection-url>jdbc:mysql://localhost:3306/student</connection-url> 4 <driver>mysql</driver> 5 <security> 6 <user-name>root</user-name> 7 <password>1234</password> 8 </security> 9 </datasource> 10 <drivers> 11 <driver name="mysql" module="com.mysqldatabase.mysql"> 12 <driver-class>com.mysql.jdbc.Driver</driver-class> 13 <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class> 14 </driver> 15 </drivers> 16 </datasources>
1 < jta-data-source> java:jboss/KouMySQLDS </jta-data-source > 2 < properties> 3 < property name= "hibernate.hbm2ddl.auto" value ="validate" /> 4 < property name= "hibernate.jdbc.fetch_size" value ="15" /> 5 < property name= "hibernate.jdbc.batch_size" value ="10" /> 6 < property name= "hibernate.show_sql" value ="true" /> 7 < property name= "hibernate.format_sql" value ="true" ></ property> 8 </ properties>
1 public static void main(String[] args) { 2 3 Properties props = new Properties(); 4 props.setProperty(Context. URL_PKG_PREFIXES,"org.jboss.ejb.client.naming" ); 5 try { 6 Context context = new InitialContext(props); 7 // 这里需要注意字符串的写法:ejbservice 表示ejb的包名,StudentDaoBean表示咱们实际调用的会话Bean,org.easynoder.ejb2.dao.BaseOperation表示 对应的接口 8 BaseOperation op = (BaseOperation) context 9 .lookup("ejb:/ejbservice//StudentDaoBean!org.easynoder.ejb2.dao.BaseOperation" ); 10 op.findAll(); 11 } catch (NamingException e) { 12 e.printStackTrace(); 13 } 14 }
标签:
原文地址:http://www.cnblogs.com/benshan/p/4376823.html