1.配置hibernate的事务管理器 -->
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeDao" class="com.ssh.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="departmentDao" class="com.ssh.dao.DepartmentDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="employeeService" class="com.ssh.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="departmentService" class="com.ssh.service.DepartmentService">
<property name="departmentDao" ref="departmentDao"></property>
</bean>
<bean id="employeeAction" class="com.ssh.actions.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
<property name="departmentService" ref="departmentService"></property>
</bean>
<?xml version="1.0" encoding="UTF-8" ?>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="stru
</property></bean><!-- 配置spring的声明式事务1.配置hibernate的事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFa...
首先我们这是一个小的demo,用来体验实际开发的过程。于是我们需要按照正规流程的简易版进行开发我们的,播放器管理软件。
正规软件开发过程为:需求开发——》体系结构设计——》详细设计——》代码
实现
——》测试——》
接下来的步骤分为:需求分析——》详细设计——》代码
实现
(边
实现
边测试)
一、我们的需求:
播放列表管理:
• 将歌曲添加到主播放列表
• 将歌曲添加到普通播放列表
• 通过歌曲id查询播放...
写过一个
Java
工程师
学习指南,我把它分为了四个部分,第一步是打好
Java
基础,掌握
Java
核心技术,第二步是掌握
Java
Web技术栈,能够做一些
项目
,第三步是掌握
Java
方面的进阶技术,包括
网
络编程、并发编程、JVM等,第四步是掌握后端进阶技术,比如分布式、缓存、消息队列等技术。
按照这样的一个脉络,我觉得能够掌握这里面的绝大部分内容,并且有过相应的一些实践,那么可以算是学好了
Java
...
好的,以下是基于
Hibernate
框架创建 Student 类
实现
CRUD
:
1. 首先创建一个 Student 类,包含 id、name 和 age 三个属性,并在类上添加 `@Entity` 注解,用于将该类映射为数据库表。
```
java
@Entity
public class Student {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int age;
// 构造方法、getter 和 setter 方法省略
2. 在 `
hibernate
.cfg.xml` 配置文件中配置数据库连接信息和
Hibernate
相关属性。
```xml
<
hibernate
-configuration>
<session-factory>
<property name="
hibernate
.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="
hibernate
.connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="
hibernate
.connection.username">root</property>
<property name="
hibernate
.connection.password">password</property>
<property name="
hibernate
.dialect">org.
hibernate
.dialect.MySQL5InnoDBDialect</property>
<property name="
hibernate
.show_sql">true</property>
<property name="
hibernate
.hbm2ddl.auto">update</property>
</session-factory>
</
hibernate
-configuration>
3. 创建一个 DAO(数据访问对象)类,用于对 Student 对象进行
CRUD
操作。在该类中通过 `SessionFactory` 获取 `Session` 对象,然后通过 `Session` 对象进行数据库操作。
```
java
public class StudentDAO {
private final SessionFactory sessionFactory;
public StudentDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
public void save(Student student) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
public Student get(int id) {
Session session = sessionFactory.openSession();
Student student = null;
try {
student = session.get(Student.class, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
return student;
public void update(Student student) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.update(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
public void delete(int id) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Student student = session.get(Student.class, id);
session.delete(student);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
4. 在应用程序中使用 StudentDAO 类进行
CRUD
操作。
```
java
public class Application {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
StudentDAO studentDAO = new StudentDAO(sessionFactory);
// 创建 Student 对象并保存到数据库
Student student = new Student("Tom", 18);
studentDAO.save(student);
// 根据 id 获取 Student 对象并更新其 age 属性
student = studentDAO.get(1);
student.setAge(19);
studentDAO.update(student);
// 根据 id 删除 Student 对象
studentDAO.delete(1);
以上就是使用
Hibernate
框架创建 Student 类
实现
CRUD
的过程。