使用EJB3的EntityBean连接数据库

运行环境:JBOSS4.2.2 + MySQL5

沿用以前用过的数据库compass:

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for books
-- ----------------------------
CREATE TABLE `books` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NM` varchar(100) DEFAULT NULL,
  `CNT` text,
  `ISBN` char(10) DEFAULT NULL,
  `PBLDT` date DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `books` VALUES ('1', 'Lucene IN Action 中文版', '这是一本关于Java的查询引擎的书。', '7121032171', '2007-01-01');
INSERT INTO `books` VALUES ('2', 'Spring 2.0 技术手册', '林信良写的一本关于Spring的书。', '7121039580', '2007-04-01');
INSERT INTO `books` VALUES ('4', '林信良学Lunece', '林信良学习Lunece的一本书', '1234567890', '2008-06-01');
INSERT INTO `books` VALUES ('9', 'Hibernate 深入浅出', '这是一本关于Hibernate的好书。', '7121006707', '2005-06-01');
INSERT INTO `books` VALUES ('10', 'Hibernate IN Action 中文版', '这是一本关于Hibernate的中文书', '1212121212', '2008-07-10');
INSERT INTO `books` VALUES ('11', 'Hibernate + Spring 中文系统', 'Hibernate + Spring 中文系统', '1234512345', '2008-06-06');
INSERT INTO `books` VALUES ('12', 'Hibernate 深入浅出2', 'Hibernate 深入浅出2', '2343454567', '2008-05-01');
INSERT INTO `books` VALUES ('13', 'Hibernate IN Action 中文版2', 'Hibernate IN Action 中文版2', '8888006707', '2009-06-01');

=======================================================================================
在Jboss下给这个数据库配置数据源:
http://zhangljerry.javaeye.com/blog/49632
http://hi.baidu.com/imake/blog/item/8348da3397000041ac4b5fe3.html
=======================================================================================
JPA由Hibernate来实现。工程结构及使用到的jar包如下图所示:
 
 
=======================================================================================
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_1_0.xsd" version="1.0">
   
 <persistence-unit name="EntityBeanPU" transaction-type="JTA">
    <jta-data-source>java:/MySqlDS</jta-data-source>
 </persistence-unit>
 
</persistence>
=======================================================================================
Book.java
package com.cecltd.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@SuppressWarnings("serial")
@Entity
@Table(name = "books")
public class Book implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer ID;
	
	@Column(nullable=false,length=100)
	private String NM;
	
	@Column(nullable=false)
	private String CNT;
	
	@Column(nullable=false,length=10)
	private String ISBN;
	
	@Temporal(value=TemporalType.DATE)
	private Date PBLDT;
	
	public Integer getID() {
		return ID;
	}
	public void setID(Integer id) {
		ID = id;
	}
	public String getNM() {
		return NM;
	}
	public void setNM(String nm) {
		NM = nm;
	}
	public String getCNT() {
		return CNT;
	}
	public void setCNT(String cnt) {
		CNT = cnt;
	}
	public String getISBN() {
		return ISBN;
	}
	public void setISBN(String isbn) {
		ISBN = isbn;
	}
	public Date getPBLDT() {
		return PBLDT;
	}
	public void setPBLDT(Date pbldt) {
		PBLDT = pbldt;
	}
}
BookDao.java
package com.cecltd.ejb3;

import java.util.List;

public interface BookDao {

	public List getBookList();
}
BookDaoBean.java
package com.cecltd.ejb3.impl;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.cecltd.ejb3.BookDao;

@Stateless
@Remote(BookDao.class)
public class BookDaoBean implements BookDao {

	@PersistenceContext
    protected EntityManager em;

	public List getBookList() {
		
		Query query = em.createQuery("from Book order by ISBN asc");
        List list = query.getResultList();
        return list; 
	}
}
测试代码:
EntityBeanClient.java
package test;

import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.cecltd.domain.Book;
import com.cecltd.ejb3.BookDao;

public class EntityBeanClient {

	public static void main(String[] args) throws NamingException, SQLException {
		
		Properties props = new Properties();
        props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.setProperty("java.naming.provider.url", "127.0.0.1:1099");
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
		InitialContext ctx = new InitialContext(props);

        
        BookDao bookDao = (BookDao)ctx.lookup("BookDaoBean/remote");
        List list = bookDao.getBookList();
        for (int i = 0; i < list.size(); i++) {
        	Book book = (Book)list.get(i);
			System.out.println(book.getNM());
		}
	}
}
 

你可能感兴趣的:(Hibernate,String,ejb,Integer,insert,books)