1.ORM框架必然发展趋势:
jdbc->hibernate(是产品,实现jpa规范)->jpa(是规范,不是产品)。ps:运用jpa规范的API进行编程,不对Hiberbate,topLink等orm框架构成威胁。
2.JPA环境搭建[hibernate-distribution-3.6.10.Final]
1.准备lib包2.jar包引入时,千万注意目录不能有中文或者空格
3.开发步骤:
1.先建表,再编写配置文件和bean-(面向过程,传统的数据库建模思想)2.先编写配置文件和bean,在建表(OOP思想)-要求比较高
4.demo实例
事务种类:1.本地事务:支持对同一个数据库的事务操作——大部分应用2.全局事务:支持对多个数据库的事务操作(银行转账)-两次提交协议
步骤:
第一步:项目结构
2.持久化文件配置:
- <?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_2_0.xsd"
- version="2.0">
- <persistence-unit name="MyJpa" transaction-type="RESOURCE_LOCAL">
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
- <property name="hibernate.hbm2ddl.auto" value="update" /><!--已存在则更新,不存在则创建 -->
- <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />
- <property name="hibernate.connection.username" value="root" />
- <property name="hibernate.connection.password" value="123456" />
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpadb?useUnicode=true&characterEncoding=UTF-8" />
- </properties>
- </persistence-unit>
- </persistence>
3.实体bean
知识点:字段的长度,是否为空,关键字,自增,字段名称的映射修改,表名称的映射修改,字段类型(Date类型)-不同格式要求,枚举类的注释(索引,枚举值)-性别,大文本类型数据,二进制数据映射,不想某个字段跟表有映射关系,为了防止某个字段数据量过大而占用内存过大因此对其进行延迟加载(懒惰加载,需要获取数据时才得到数据)。
- import java.util.Date;
- import javax.persistence.Basic;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.EnumType;
- import javax.persistence.Enumerated;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Lob;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- import javax.persistence.Transient;
- @Entity
- @Table(name="person")
- public class Person {
- private Integer id;
- private String name;
- private Date birthday;
- private Sex sex;
- private String info;
- private Byte[] file;
- private String other;
- public Person() {
- super();
- }
- public Person(String name) {
- super();
- this.name = name;
- }
- public Person(String name, Date birthday) {
- super();
- this.name = name;
- this.birthday = birthday;
- }
- public Person(String name, Date birthday, Sex sex) {
- super();
- this.name = name;
- this.birthday = birthday;
- this.sex = sex;
- }
- /**
- * 主键并自增
- * @return the id
- */
- @Id @GeneratedValue
- public Integer getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(Integer id) {
- this.id = id;
- }
- /**
- * @return the name
- */
- @Column(length=10,nullable=false,name="personName")
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the birthday
- */
- @Temporal(TemporalType.DATE)
- public Date getBirthday() {
- return birthday;
- }
- /**
- * @param birthday the birthday to set
- */
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- /**
- * @return the sex
- */
- @Enumerated(EnumType.STRING)
- public Sex getSex() {
- return sex;
- }
- /**
- * @param sex the sex to set
- */
- public void setSex(Sex sex) {
- this.sex = sex;
- }
- /**
- * @return the info
- */
- @Lob
- public String getInfo() {
- return info;
- }
- /**
- * @param info the info to set
- */
- public void setInfo(String info) {
- this.info = info;
- }
- /**
- * @return the file
- */
- @Lob @Basic(fetch=FetchType.LAZY) //当文件很大时,进行懒惰加载
- public Byte[] getFile() {
- return file;
- }
- /**
- * @param file the file to set
- */
- public void setFile(Byte[] file) {
- this.file = file;
- }
- /**
- * @return the other
- */
- @Transient //排除某个字段的映射
- public String getOther() {
- return other;
- }
- /**
- * @param other the other to set
- */
- public void setOther(String other) {
- this.other = other;
- }
- }
- public enum Sex {
- MAN,WORMAN
- }
4.单元测试类
知识点:
1.把握异常出现的时机。
2.通过ID得到实体bean(1.彻底查询 2.用到查询)
3.保存实体bean到数据库
4.更新实体bean到数据库中
涉及到对象的状态:
1.新建
2.托管(设置实体的字段值,并通过提交可以同步到数据库)
3.游离(无法更新到数据库中,除非使用merge方法重新可将其更新到数据库中)
4.删除
- public class PersonTest {
- @Test
- public void save(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- em.persist(new Person("techbirds",new Date(),Sex.MAN));
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test
- public void getPerson1(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.find(Person.class, 1);
- em.getTransaction().commit();
- em.close();
- factory.close();
- System.out.println(p.getName());
- }
- @Test
- public void getPerson2(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.getReference(Person.class, 1);
- //代理对象,用到才查询
- System.out.println(p.getName());
- em.getTransaction().commit();
- em.close();
- //System.out.println(p.getName());出错,事务已经关闭
- factory.close();
- }
- @Test
- public void updatePerson1(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.find(Person.class, 1);
- p.setName("bao");
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test
- public void updatePerson2(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.find(Person.class, 1);
- em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步
- p.setName("techbirds");
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test
- public void updatePerson3(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.find(Person.class, 1);
- em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步
- p.setName("techbirds");
- em.merge(p);//此时又可以进行同步
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test
- public void delPerson(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- Person p=em.find(Person.class, 1);
- em.remove(p);
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- }
5.jpa的(sql)查询
jpaSQL语句:面向对象的sql语句,jpa标准的sql语法
查询方法:
1.位参数查询 select o from Person o where o.id=?1—>query.setParameter(1,2);
2.命名查询 select o from Person o where o.id=:id—>query.setParameter("id",2);
查询结果:1.列表 2.唯一值(对象)
查询类型:普通查询,删除查询,更新查询
ps:进行数据的更改必须启动事务。-删除查询和更新查询必须开启事务
- @Test
- public void querysql(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- //面向对象的sql语句
- Query query=em.createQuery("select o from Person o where o.id=?");
- query.setParameter(1, 2);
- Person p=(Person) query.getSingleResult();
- System.out.println(p.getName());
- em.close();
- factory.close();
- }
- @Test
- public void deletesql(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- //面向对象的sql语句
- Query query=em.createQuery("delete from Person o where o.id=?");
- query.setParameter(1, 3);
- query.executeUpdate();
- em.getTransaction().commit();
- em.close();
- factory.close();
- }
- @Test
- public void updatesql(){
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- //面向对象的sql语句
- Query query=em.createQuery("update Person o set o.sex=? where o.id=?");
- query.setParameter(1, Sex.WORMAN);
- query.setParameter(2, 3);
- query.executeUpdate();
- em.getTransaction().commit();
- em.close();
- factory.close();
- }