create database student; //创建数据库 student
create table student( //创建表student 和 数据库同名 `id` integer(11) not null, `name` varchar2(20) default null, primary key (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1
insert into student values(1,'easynoder'); commit;
grant all privileges on *.* to root@localhost indentified by "1234"
@Entity //表明这是一个实体Bean
@Table (name = "student" ) //和数据库表student 建立映射
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 4002145187978562529L;
@Id // 表明是该实体的id
@GeneratedValue(strategy = GenerationType. AUTO ) //id生成策略
@Column(name = "id" )//对应student表id字段
private int id ;
@Column(name = "name" ) // 对应student表name字段
private String name;
public int getId() {
return id ;
}
public String getName() {
return name ;
}
public void setId(int id) {
this .id = id;
}
public void setName(String name) {
this .name = name;
}
}
public interface BaseOperation {
public List<?> findAll();
}
@Stateless //这是一个无状态Bean
@Remote (BaseOperation. class) //指明Bean的remote接口
public class StudentDaoBean implements BaseOperation {
// EntityManager是由EJB容器自动配置和管理的,unitName属性的值对应
persistence.xml 中< persistence-unit name = "MyEJBProject" transaction-type = "JTA" ></ persistence-unit > name的配置
@PersistenceContext(unitName = "MyEJBProject" )
private EntityManager em;
@SuppressWarnings( "unchecked" )
public List<?> findAll() {
System. out .println("查询开始..." );
List<StudentEntity> list = em.createQuery( " from StudentEntity ").getResultList();
if (list != null) {
Iterator<StudentEntity> it = list.iterator();
while (it.hasNext()) {
StudentEntity student = it.next();
System. out .println("学生id:" + student.getId());
System. out .println("学生名称:" + student.getName());
}
}
System. out .println("查询完毕...." );
return list;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mysqldatabase.mysql">
<resources>
<resource-root path="mysql-connector-java-5.**-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
<datasources>
<datasource jndi-name="java:jboss/KouMySQLDS" pool-name="MySQLDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/student</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>1234</password>
</security>
</datasource>
<drivers>
<driver name="mysql" module="com.mysqldatabase.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
< jta-data-source> java:jboss/KouMySQLDS </jta-data-source >
< properties>
< property name= "hibernate.hbm2ddl.auto" value ="validate" />
< property name= "hibernate.jdbc.fetch_size" value ="15" />
< property name= "hibernate.jdbc.batch_size" value ="10" />
< property name= "hibernate.show_sql" value ="true" />
< property name= "hibernate.format_sql" value ="true" ></ property>
</ 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 remote.connection.default.username= yourUsername remote.connection.default.password= yourPassword
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty(Context. URL_PKG_PREFIXES,"org.jboss.ejb.client.naming" );
try {
Context context = new InitialContext(props);
// 这里需要注意字符串的写法:ejbservice 表示ejb的包名,StudentDaoBean表示咱们实际调用的会话Bean,org.easynoder.ejb2.dao.BaseOperation表示 对应的接口
BaseOperation op = (BaseOperation) context
.lookup("ejb:/ejbservice//StudentDaoBean!org.easynoder.ejb2.dao.BaseOperation" );
op.findAll();
} catch (NamingException e) {
e.printStackTrace();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有