下面是“Spring4整合Hibernate5详细步骤”的攻略,分别针对Spring和Hibernate进行详细讲解。
Spring配置
- 在
pom.xml
文件中添加Spring和Hibernate的依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.5.Final</version>
</dependency>
- 创建
DataSource
对象:
<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>
- 配置
SessionFactory
对象:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
- 配置
TransactionManager
对象:
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Hibernate配置
- 配置
hibernate.cfg.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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.example.entity.User" />
</session-factory>
</hibernate-configuration>
- 创建实体类
User
:
@Entity
@Table(name="user")
public class User {
@Id
private int id;
private String username;
private String password;
// 省略getter和setter方法
}
整合配置
- 配置
applicationContext.xml
文件:
<bean id="userDao" class="com.example.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userService" class="com.example.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
- 创建
UserDao
层和UserService
层:
用户数据访问接口UserDao
:
public interface UserDao {
void save(User user);
List<User> getUsers();
}
用户数据访问实现类UserDaoImpl
:
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void save(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public List<User> getUsers() {
Query query=sessionFactory.getCurrentSession().createQuery("from User");
return query.list();
}
}
用户服务接口UserService
:
public interface UserService {
void save(User user);
List<User> getUsers();
}
用户服务实现类UserServiceImpl
:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void save(User user) {
userDao.save(user);
}
@Override
public List<User> getUsers() {
return userDao.getUsers();
}
}
示例
以下是两个示例:
- 添加用户:
User user=new User();
user.setUsername("test");
user.setPassword("test");
userService.save(user);
- 获取用户:
List<User> users=userService.getUsers();
for (User user : users) {
System.out.println(user.getUsername()+":"+user.getPassword());
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring4整合Hibernate5详细步骤 - Python技术站