回顾以下DAO代码,以查找所有用户为例,直接使用JDBC查询用户的代码如下:
List users = new ArrayList();
User user = null;
try{
Connection conn = DBUtil.getConnection();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from users");
while(resultSet.next()){
user = new User();
user.setId(resultSet.getInt(1));
user.setUserName(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setTelephone(resultSet.getString(4));
user.setRegisterDate(resultSet.getDate(5));
user.setSex(resultSet.getInt(6));
users.add(user);
}
}catch(Exception e){
//省略异常处理代码
}finally{
DBUtil.close(resultSet,statement,conn);
}
EntityManager entityManager = entityManagerFactory.createEntityManager();
TypedQuery<Users> query = entityManager.createQuery("from Users ",Users.class);
List<Users> list = query.getResultList();
在IDE工具中新建Maven工程,使用JPA,需做以下准备工作:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!-- RESOURCE_LOCAL:采用本地事务管理 -->
<persistence-unit name="JPA" transaction-type="RESOURCE_LOCAL">
<!--JPA需要Provider实现,Hibernate是其中之一-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--持久化实体类-->
<class>org.hdax.entity.Users</class>
<properties>
<!--数据库方言-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--数据库连接驱动类-->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!--数据库连接url-->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8" />
<!--数据库登录用户名-->
<property name="javax.persistence.jdbc.user" value="root" />
<!--数据库登录密码-->
<property name="javax.persistence.jdbc.password" value="root" />
<!--是否显示sql语句-->
<property name="hibernate.show_sql" value="true" />
<!--自动创建数据库表-->
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
@Entity
@Table(name = "smbms_users")
public class Users implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "userCode",columnDefinition = "varchar(15)")
private String userCode;
@Column(name = "userName",columnDefinition = "varchar(15)")
private String userName;
@Column(name = "userPassword",columnDefinition = "varchar(15) NOT NULL")
private String userPassword;
@Column(name = "gender",columnDefinition = "int(10)")
private Integer gender;
@Column(name = "birthday",columnDefinition = "date")
private Date birthday;
@Column(name = "address",columnDefinition = "varchar(30) DEFAULT '地址不详'")
private String address;
@Column(name = "usrRole",columnDefinition = "int(20)")
private Integer usrRole;
@Column(name = "idPicPath",columnDefinition = "varchar(100)")
private String idPicPath;
public Users(){
}
//省略getter & setter 方法
}
注解名称 | 作用 |
---|---|
@Entity | 将当前实体类标注为持久化类 |
@Table | 实体类与数据库表进行关联 当表名和实体类名称相同时,可省略name |
@name | 用于指明数据库表名 |
@Entity | 将当前实体类标注为持久化类 |
@catalog | 用于指明数据库名称 |
@Id | 用于将Java字段标记为数据库表主键列 |
@GeneratedValue | 数据库将在插入时自动为id字段生成一个值数据到表 |
@strategy | 当使用id字段的自动生成值时 |
@SequenceGenerator | 序列创建序列生成器 |
@name | 序列程序定义名称 |
@sequenceName | 数据库序列名称 |
@Column | 属性名称与字段名称映射 |
@GeneratedValue | 数据库将在插入时自动为id字段生成一个值数据到表 |
@name | 映射的列名 |
@unique | 是否唯一 |
@nullable | 是否允许为空 |
@length | 对于字符型列,length属性指定列的最大字符长度 |
@insertable | 是否允许插入 |
@updateable | 是否允许更新 |
@columnDefinition | 定义建表时创建此列的DDL |
@secondaryTable | 从表名。如果此列不建 在主表上(默认是主表),该属性定义该列所在从表的名字 |
@Transient | 该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性 |
//此处JPA名称要和配置文件中标签persistence-unit属性name值一致
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JPA");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(users);
//提交
transaction.commit();
//回滚
transaction.rollback();
在进行修改或删除操作时,应先加载对象,然后再执行修改或删除操作。JPA提供了两种方法按照主键加载对象:
find()
EntityManager entityManager = entityManagerFactory.createEntityManager();
Users users = entityManager.find(Users.class,1);
System.out.println("查询完成!");
/*运行结果如下:
Hibernate:
select
users0_.id as id1_0_0_,
users0_.address as address2_0_0_,
users0_.birthday as birthday3_0_0_,
users0_.gender as gender4_0_0_,
users0_.idPicPath as idPicPat5_0_0_,
users0_.userCode as userCode6_0_0_,
users0_.userName as userName7_0_0_,
users0_.userPassword as userPass8_0_0_,
users0_.userRole as userRole9_0_0_
from
smbms_user users0_
where
users0_.id=?
查询完成!
*/
getReference()
EntityManager entityManager = entityManagerFactory.createEntityManager();
Users users = entityManager.getReference(Users.class,1);
System.out.println("查询完成!");
/*运行结果如下:
查询完成!
*/
EntityManager entityManager = entityManagerFactory.createEntityManager();
Users users = entityManager.getReference(Users.class,1);
System.out.println(users);
System.out.println("查询完成!");
/*运行结果如下:
Hibernate:
select
users0_.id as id1_0_0_,
users0_.address as address2_0_0_,
users0_.birthday as birthday3_0_0_,
users0_.gender as gender4_0_0_,
users0_.idPicPath as idPicPat5_0_0_,
users0_.userCode as userCode6_0_0_,
users0_.userName as userName7_0_0_,
users0_.userPassword as userPass8_0_0_,
users0_.userRole as userRole9_0_0_
from
smbms_user users0_
where
users0_.id=?
org.hdax.entity.Users@e041f0c
查询完成!
*/
EntityManager entityManager = entityManagerFactory.createEntityManager();
Users users = entityManager.find(Users.class,1);
System.out.println(users.getUserName());
users.setUserName("超级系统管理员");
/*运行结果如下:
Hibernate:
select
users0_.id as id1_0_0_,
users0_.address as address2_0_0_,
users0_.birthday as birthday3_0_0_,
users0_.gender as gender4_0_0_,
users0_.idPicPath as idPicPat5_0_0_,
users0_.userCode as userCode6_0_0_,
users0_.userName as userName7_0_0_,
users0_.userPassword as userPass8_0_0_,
users0_.userRole as userRole9_0_0_
from
smbms_user users0_
where
users0_.id=?
系统管理员
*/
此时虽然通过set方法修改了用户的用户名,但是通过运行结果我们可以看出,数据库并没有执行响应的更新数据操作,所以现阶段的数据库修改只是在程序内存中完成的。那么如果添加事务之后程序的运行又会发生哪些变化呢?
EntityManager entityManager = entityManagerFactory.createEntityManager();
//获得事务对象
EntityTransaction transaction = entityManager.getTransaction();
//开启事务
transaction.begin();
Users users = entityManager.find(Users.class,1);
System.out.println(users.getUserName());
users.setUserName("超级系统管理员");
//提交事务
transaction.commit();
/*运行结果如下:
Hibernate:
select
users0_.id as id1_0_0_,
users0_.address as address2_0_0_,
users0_.birthday as birthday3_0_0_,
users0_.gender as gender4_0_0_,
users0_.idPicPath as idPicPat5_0_0_,
users0_.userCode as userCode6_0_0_,
users0_.userName as userName7_0_0_,
users0_.userPassword as userPass8_0_0_,
users0_.userRole as userRole9_0_0_
from
smbms_user users0_
where
users0_.id=?
系统管理员
Hibernate:
update
smbms_user
set
address=?,
birthday=?,
gender=?,
idPicPath=?,
userCode=?,
userName=?,
userPassword=?,
userRole=?
where
id=?
*/
不难发现,程序自动执行了更新操作,通过查看数据库表数据,数据库中的用户信息同样发生了相应的修改,所以我们在执行增删改操作的时候应当加入相应的事务控制语句。
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
Users users = entityManager.find(Users.class,1);
entityManager.remove(users);
transaction.commit();
System.out.println("删除数据成功!");
/*运行结果如下:
Hibernate:
select
users0_.id as id1_0_0_,
users0_.address as address2_0_0_,
users0_.birthday as birthday3_0_0_,
users0_.gender as gender4_0_0_,
users0_.idPicPath as idPicPat5_0_0_,
users0_.userCode as userCode6_0_0_,
users0_.userName as userName7_0_0_,
users0_.userPassword as userPass8_0_0_,
users0_.userRole as userRole9_0_0_
from
smbms_user users0_
where
users0_.id=?
Hibernate:
delete
from
smbms_user
where
id=?
删除数据成功!
*/