The EJB3 container manages entity beans through the Entitymanager object, the primary function of which is to map each other between records and JavaBean. For example, we find a record from a database that has field1 and field2 two fields, assuming a JavaBean, and also contains Field1 and Field2 properties. Then Entitymanager can map the values in this record to the Field1 and Field2 properties of JavaBean. In other words, a record in the database corresponds to an JavaBean object instance. Here's a simple example to show how to implement a program that applies entity beans. In this example, a stateless session bean is used to look up a record from a datasheet through a Entitymanager object, map the record to an object instance of an entity bean, and then return a field value for that record through the session Bean's method.
Table Name: T_users
Fields and records in a table
id name password_md5
1 bill TaokQpoNJQb02eafO/JgYw==
First, configure the JBoss database connection pool
Create a Mysql-ds.xml file in the <jboss installation directory >\server\default\deploy directory, which reads as follows:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyPIM</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/pim</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>1234</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>
The Mysql-ds.xml file is used to configure the MySQL data source. In fact, the file can be found in the <jboss installation directory >\docs\examples\jca directory, and the reader needs to change the database name, username and password, and Jndi name to their own. Finally, copy the modified Mysql-ds.xml file into the Deploy directory (this file name must be called).
A JDBC driver (jar file) is required to connect to the MySQL database and copy the file to the <jboss installation directory >\server\default\lib directory.