Spring与Hibernate整合(注解模式)
2017-09-14 20:40
369 查看
整个程序代码
User.java
User.hbm.xml
UserDao.java
UserService.java
Test.java
bean-base.xml
User.java
public class User { private int uId; private String uName; private String uPassword; public int getuId() { return uId; } public void setuId(int uId) { this.uId = uId; } public String getuName() { return uName; } public void setuName(String uName) { this.uName = uName; } public String getuPassword() { return uPassword; } public void setuPassword(String uPassword) { this.uPassword = uPassword; } public User(int uId, String uName, String uPassword) { super(); this.uId = uId; this.uName = uName; this.uPassword = uPassword; } public User() { super(); } @Override public String toString() { return "User [uId=" + uId + ", uName=" + uName + ", uPassword=" + uPassword + "]"; } }
User.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wc.pojo"> <class name="User" table="t_user"> <id name="uId"> <generator class="native"> </generator> </id> <!--配置的是普通的属性 --> <property name="uName"></property> <property name="uPassword"></property> </class> </hibernate-mapping>
UserDao.java
/** * 这是持久层实现 * @author Administrator * */ @Repository public class UserDao implements IUserDao{ @Autowired private SessionFactory sessionFactory=null; @Override public void method(User user) throws Exception { sessionFactory.getCurrentSession().save(user); } }
UserService.java
/** * 用户的业务逻辑层实现 * @author Administrator * */ @Service @Transactional public class UserService implements IUserService{ @Autowired private UserDao userDao=null; @Override public void method2(User user) throws Exception { if(!("".equals(user.getuPassword())||"".equals(user.getuName()))) { userDao.method(user); } } }
Test.java
public class Test { public static void main(String[] args) throws Exception { //获取一下我们的IOC容器 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("config/bean-base.xml"); //接下来就获取userService对象 IUserService service=(IUserService) context.getBean("userService"); //调用这个函数 service.method2(new User(123,"中国", "123578")); } }
bean-base.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 将Hibernate中的连接数据库的相关信息配置到Spring中来 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="acquireIncrement" value="2"></property> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="2"></property> <property name="maxStatements" value="100"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test01"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--Spring和Hibernate整合的关键点是SessionFactory的创建问题 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--下面我要将hibernate的配置文件写到这个里面来--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernaye.hbm2ddl.auto" >update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 编写hibernate的映射配置 --> <property name="mappingDirectoryLocations"> <list> <!-- 配置mapping映射文件的根路径就好了 --> <value>classpath:com/wc/pojo</value> </list> </property> </bean> <!-- 配置事务 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--配置IOC/DI的扫描器--> <context:component-scan base-package="com.wc"></context:component-scan> <!--配置aop的扫描--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 应用这个事务 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
相关文章推荐
- Hibernate注解使用以及Spring整合
- Spring4 学习系列之——整合基于注解的Hibernate实例
- spring和hibernate注解整合
- Hibernate注解使用以及Spring整合
- Hibernate注解使用以及Spring整合
- Spring 整合 Hibernate(注解方式)
- Hibernate注解使用以及Spring整合
- Hibernate注解使用以及Spring整合
- Hibernate注解使用以及Spring整合
- spring与hibernate整合配置基于Annotation注解方式管理实务
- Hibernate注解使用以及Spring整合
- struts2.2.1+spring 3.0.3+hibernate3.6+dwr3.0全注解整合详解
- Hibernate注解使用以及Spring整合
- spring spring mvc hibernate整合(注解)(xml)
- struts2.2.1+spring 3.0.3+hibernate3.6+dwr3.0全注解整合详解
- Hibernate注解使用以及Spring整合
- Spring与Hibernate整合(配置模式)
- spring mvc高级篇(三):Spring+SpringMVC+Hibernate整合(采用注解优化)
- Spring 整合 Hibernate 使用全注解Bean出现 sessionFactory' or 'hibernateTemplate' is required
- spring整合hibernate(注解、xml)applicationContext.xml配置