JBoss 中配置数据源(datasource),及EntityManager的使用

阅读更多

前提:

EAP版本: EAP6.1

模式:standalone模式

数据库: Postgresql 9.3

数据源模式:NON-XA DataSource

 

正文:

① 将postgresql的jdbc包作成一个module部署到JBoss中。比如,我作成的module如下图所示:

    具体的方法可以参考我另一篇博文: http://rainbow702.iteye.com/blog/2065353


JBoss 中配置数据源(datasource),及EntityManager的使用_第1张图片
 

② 修改 \jboss-eap-6.1\standalone\configuration下的standalone.xml文件,在其中的“”下增加一个新的datasource(系统默认自带一个ExampleDS)。下面是新增datasource之后的配置:

 


	
		jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
		h2
		
			sa
			sa
		
	
	
		jdbc:postgresql://localhost:5432/test
		org.postgresql.Driver
		postgresql
                
		
			32
			32
			true
		
		
			postgres
			123456
		
		
			
			true
			false
			
		
	
	
		
                
		
			org.h2.jdbcx.JdbcDataSource
		
	

 

 

③ 经过上面两步,datasource就算配置好了。下面说明一下如何来创建EntityManager对象和相关的Entity实体类

④ 创建persistence.xml文件,内容如下:

 


   
      java:jboss/datasources/testDS
      
      
         
         
      
   

 ⑤ persistence.xml这份文件配置好了之后,应该将它放置在工程的什么位置呢?我一开始也不知道,后来,百度了一下,说是要放到 META-INF目录下,所以,我就将其放在了WebContent/META-INF下了,结果死活不起作用。后来又改成放到  WebContent/WEB-INF/lib下,还是不起作用。直至后来在官方找到了说明:

http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html

上面链接的最后,写了persistence.xml可放置的路径。

然后,我改成放在"src/META-INF"(部署的时候会被部署至WEB-INF/class/META-INF)下之后,再重新启动EAP,终于可以了。

 

⑥ 声明与数据库中person表相应的Entity类


     JBoss 中配置数据源(datasource),及EntityManager的使用_第2张图片
 

@Entity
@Table(name = "person")
@NamedQueries({
	@NamedQuery(name="user.checkLogin", query="select u from UserEntity u where u.name= :name and u.password=:password")
})
public class UserEntity implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Column(name = "id")
	@Id
	private Integer id;

	@Column(name = "name")
	private String name;
	
	@Column(name="password")
	private String password;

	@Column(name = "nickname")
	private String nickName;

	public UserEntity() {
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
}

 

⑦这样,Entity可以通过EntityManager来维护了。下面,通过注解来获取EntityManager对象

public abstract class BaseDao {

	private Class entityClass;

        // 获取EntityManager,unitName的值最好明确写出来,且与上面④中定义的unitName保持一致
	@PersistenceContext(unitName="testUnit")
	protected EntityManager em;

	public BaseDao(Class entityClass) {
		this.entityClass = entityClass;
	}

	public Class getEntityClass() {
		return this.entityClass;
	}

}

 然后就可以使用这个EntityManager进行数据库操作了

public class LoginDao extends BaseDao {

	public LoginDao() {
		super(UserEntity.class);
	}

	public boolean checkLogin(String userName, String password) {
		boolean res = false;
		// 使用em进行数据库的查询
		Query query = this.em.createNamedQuery("user_checkLogin", UserEntity.class);
		query.setParameter("name", userName);
		query.setParameter("password", password);
		List resList = query.getResultList();
		res = resList.size() > 0;
		return res;
	}
}

 以上,完了。

 

 

PS:

①  请确保数据库的jdbc包与工程使用的JDK版本是相符合的,不然可能会报错。比如,我上面使用的jar包是基于JDK1.7及以上的,但我的工程使用的是JDK1.6,结果就报错了。换成1.7的就没有问题了。

② 可以参考官方文档: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_BRMS_Platform/5/html/BRMS_Administrator_Guide/Configuring_a_Datasource_for_JBoss_Enterprise_Application_Platform_6.html

  • JBoss 中配置数据源(datasource),及EntityManager的使用_第3张图片
  • 大小: 29.9 KB
  • JBoss 中配置数据源(datasource),及EntityManager的使用_第4张图片
  • 大小: 24 KB
  • 查看图片附件

你可能感兴趣的:(JBoss,EAP,DataSource,JPA,EntityManager)