您的位置:首页 > 编程语言 > Java开发

Spring与Hibernate整合(注解模式)

2017-09-14 20:40 369 查看
整个程序代码

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  框架整合