(5) ebj学习:ejb用jpa操作数据库1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jianfpeng241241/article/details/51894337

一 JPA开发持久化Bean

二 配置数据源


三 新建Ejb项目


四 指定JNDI数据源


五 编写entity bean


六 代码

1 目录结构


2 Person.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
	
	@Id
	@GeneratedValue
	private int id;
	
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

}


3 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd" version="2.0">
    
	<persistence-unit name="EjbLession6_jpa" transaction-type="JTA">
  		<jta-data-source>java:/MySqlDS</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.hbm2ddl.auto" value="update"/>
		</properties>
	</persistence-unit>
  
</persistence>

4 运行结果

 启动JBoss后, JPA自动与数据库进行映射







展开阅读全文

没有更多推荐了,返回首页