Spring与Hibernate整合(注解模式)

整个程序代码
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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值