It is not difficult to develop beans in JBoss, but for unknown users, database configuration is a tough issue. Now let's develop an ejb3 + MySQL bean step by step.
1. MySQL Database Configuration 1. Configure the data source
Find a file named "mysql-db.xml" in the % jboss_home %/docs/examples/JCA Directory, which is an official Mysql Data Source configuration file for us, let's just change it.
Copy the mysql-db.xml to the/Server/default/deploy directory and change the file content:
<?xml version="1.0" encoding="UTF-8"?><!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ --><!-- Datasource config for MySQL using 3.0.9 available from:http://www.mysql.com/downloads/api-jdbc-stable.html--><datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/ejb</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>0000</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource></datasources>
<User-Name> is the user name of your MySQL database. Similarly, <password> is the connection password.
2. Configure persistence. xml
Persistence. XML is a deployment description file defined by the Java persistence specification. It is used to configure some basic information, such as the name of the entitymanager service. Create this file in the metainf/directory of the project directory:
<? 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"> <! -- Entity is the service name of entitymanager --> <persistence-unit name = "entity" transaction-type = "JTA"> <! -- This must be the same as <JNDI-Name> In the mysql-db.xml file, that is, mysqlds --> <JTA-data-source> JAVA:/mysqlds </JTA-data-source> <! -- The following is the configuration of hibernate, which can be omitted --> <Properties> <property name = "hibernate. dialect "value =" org. hibernate. dialect. mysqldialect "/> <property name =" hibernate. hbm2ddl. auto "value =" Update "/> <property name =" hibernate. connection. characterencoding "value =" UTF-8 "/> </Properties> </persistence-unit> </persistence>
So far, the database configuration is complete.
2. Develop bean1. develop Entity Bean
We create an Entity Bean named user to save basic information of a user, such as the user name and password.
package dev.entity;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "DB_USER")public class User implements Serializable { @Id @Column(name = "C_ID") private int id; @Column(name = "C_NAME") private String name; @Column(name = "C_PASSWORD") private String pwd; //get() and set()}
Then write the following statement to create the data table db_user:
create table DB_USER(C_ID int key,C_NAME varchar(40),C_PASSWORD varchar(40));
2. Develop Session Bean
Our session bean will have two functions: one is to create and persist a user class, and the other is to retrieve a user object from the database.
First, create the remote interface userbeanremote:
Package Dev. bean; import javax. EJB. remote; import Dev. entity. *; @ remotepublic interface userbeanremote {void createuser (User user); // create user getuser (int id); // query and obtain user}
Then create the Session Bean: userbean
package dev.bean;import javax.ejb.Stateless;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import dev.entity.*;@Statelesspublic class UserBean implements UserBeanRemote { @PersistenceContext(unitName = "entity") private EntityManager manager; public void createUser(User user) {manager.persist(user); } public User getUser(int id) {return manager.find(User.class, id); }}
So far bean development is complete.
3. Deploy EJB
In eclipse, right-click the project name and select export. In the displayed wizard, select ejb jar file. Then, follow the prompts to complete the deployment.
4. Write test code
Public class client {public static void main (string [] ARGs) {try {context = initcontext (); userbeanremote UBR = (userbeanremote) context. lookup ("userbean/remote"); User user = new user (); User. setid (4); User. setname ("bruce lee"); User. setpwd ("123456"); UBR. createuser (User);/* User user = (User) UBR. getuser (2); system. out. println (user. getname (); */system. out. println ("success! ");} Catch (exception ex) {ex. printstacktrace () ;}}/** is a fixed write */Public Static Context initcontext () throws javax. naming. namingexception {properties prop = new properties (); prop. put (context. initial_context_factory, "org. jnp. interfaces. namingcontextfactory "); prop. put (context. url_pkg_prefixes, "org. JBoss. naming: Org. jnp. interfaces "); prop. put (context. provider_url, "jnp: // localhost: 1099"); return New javax. naming. initialcontext (PROP );}}
So far, a simple ejb3 has been developed.