这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。
mysql-ds.xml(注意命名规范,数据库厂商-ds.xml) 本文件放置于JBOSS安装路径\server\default\deploy下
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <!--数据源的JNDI名称 --> <jndi-name>mysql</jndi-name> <!--数据源链接 --> <connection-url>jdbc:mysql://localhost:3306/ejb?useUnicode=true&characterEncoding=utf-8 </connection-url> <!--数据库驱动 --> <driver-class>org.gjt.mm.mysql.Driver</driver-class> <user-name>root</user-name> <password>123456</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>
<?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/persistence_1_0.xsd" version="1.0"> <!-- Name属性用于定义持久化单元的名字 (name必选,空值也合法); transaction-type 指定事务类型(可选) --> <persistence-unit name="ejb" transaction-type="JTA"> <!--数据源 JNDI名称 --> <jta-data-source>java:/mysql</jta-data-source> <!-- 显式列出实体类,在Java SE 环境中应该显式列出.(可选) --> <class>po.Student</class> <!-- 厂商专有属性(可选) --> <properties> <!--hibernate的自动更新表 --> <property name="hibernate.hbm2ddl.auto" value="update" /> <!--hibernate的自动打印sql --> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> </persistence>
package po; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * 学生实体类 * @author 强子 * */ @Entity public class Student implements java.io.Serializable{ @Id @GeneratedValue(strategy=GenerationType.AUTO)//注解生成策略 private Integer id; private String name; private String sex; private int age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
package dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import po.Student; public class StudentDaoImpl implements StudentDao{ @PersistenceContext(unitName="ejb")//依赖注入 读取persistence.xml private EntityManager em; @Override public EntityManager getEntityManager() { return em; } /** * 保存数据 */ @Override public void save(Student s) { em.persist(s); } /** * 删除数据 */ @Override public void delete(Student s) { //先merge(s)后remove的原因是 穿过的对象已经是脱离管理 //错误信息 :java.lang.IllegalArgumentException: Removing a detached instance em.remove(em.merge(s)); em.flush(); } /** * 根据主键查询 */ @Override public Student findById(int id) { return (Student)em.find(Student.class, id); } /** * 更新数据 */ @Override public void update(Student s) { System.out.println(s); em.merge(s); em.flush(); } }