下面是“spring+hibernate两种整合方式配置文件的方法”的完整攻略。
1. 环境准备
在开始之前,你需要确保已经安装好了以下环境:
- JDK 1.8+
- Maven
- Spring Framework 5.x
- Hibernate 5.x
- MySQL Database
2. 整合方式一:基于Spring数据访问模块的整合
步骤一:创建Maven项目
在Maven项目中,添加以下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
步骤二:配置数据源及Hibernate配置文件
在Spring配置文件中,配置数据源及Hibernate连接池:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.demo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
步骤三:创建实体类和DAO层接口
创建一个实体类和对应的DAO层接口。具体实现可以参考以下代码:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
// 省略getter和setter方法
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional(readOnly = true)
public User findUserById(Long id) {
Session session = sessionFactory.getCurrentSession();
User user = session.get(User.class, id);
return user;
}
@Override
@Transactional
public void saveUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
}
步骤四:测试
编写一个单元测试方法,测试实体类和DAO层接口是否正常工作:
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void testSaveUser() {
User user = new User();
user.setUsername("test");
user.setPassword("123456");
userDao.saveUser(user);
User resultUser = userDao.findUserById(user.getId());
Assert.assertEquals(user.getUsername(), resultUser.getUsername());
Assert.assertEquals(user.getPassword(), resultUser.getPassword());
}
}
3. 整合方式二:基于Hibernate模板的整合
步骤一:创建Maven项目
在Maven项目中,添加以下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
步骤二:配置数据源及Hibernate配置文件
在Spring配置文件中,配置数据源及Hibernate连接池:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.demo.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
步骤三:创建实体类和DAO层接口
创建一个实体类和对应的DAO层接口。具体实现可以参考以下代码:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
// 省略getter和setter方法
}
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public User findUserById(Long id) {
User user = hibernateTemplate.get(User.class, id);
return user;
}
@Override
public void saveUser(User user) {
hibernateTemplate.save(user);
}
}
步骤四:测试
编写一个单元测试方法,测试实体类和DAO层接口是否正常工作:
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void testSaveUser() {
User user = new User();
user.setUsername("test");
user.setPassword("123456");
userDao.saveUser(user);
User resultUser = userDao.findUserById(user.getId());
Assert.assertEquals(user.getUsername(), resultUser.getUsername());
Assert.assertEquals(user.getPassword(), resultUser.getPassword());
}
}
至此,“spring+hibernate两种整合方式配置文件的方法”的完整攻略结束。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:spring+hibernate 两种整合方式配置文件的方法 - Python技术站