spring+hibernate基础

把数据库的配置信息写在一个文件中

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/world
jdbc.username=root
jdbc.password=1234
jdbc.properties

实体类,加上注解

package com.ouc.wkp.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name="user2")
public class User2 {
    private int id;
    private String username;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}
User2.java

 

package com.ouc.wkp.dao;

import com.ouc.wkp.model.User2;

public interface UserDAO {
    public void save(User2 user2);
    public void delete();
}
UserDAO.java
package com.ouc.wkp.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.ouc.wkp.dao.UserDAO;
import com.ouc.wkp.model.User2;

@Component("u")
public class UserDAOImpl implements UserDAO {

    // private DataSource dataSource;
    //
    // public DataSource getDataSource() {
    // return dataSource;
    // }
    //
    // @Resource
    // public void setDataSource(DataSource dataSource) {
    // this.dataSource = dataSource;
    // }

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void save(User2 user2) {
        // Hibernate
        // JDBC
        // XML
        // NetWork
//        System.out
//                .println("session factory class:" + sessionFactory.getClass());
        Session s = sessionFactory.getCurrentSession();
//        s.beginTransaction();
        s.save(user2);
//        s.getTransaction().commit();
//        System.out.println("user saved!");

    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub

    }

}
UserDAOImpl.java
package com.ouc.wkp.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ouc.wkp.dao.UserDAO;
import com.ouc.wkp.model.User2;

@Component("userService")
public class UserService {

    private UserDAO userDAO;

    public void init() {
        System.out.println("init");
    }

    public void add(User2 user2) {
        userDAO.save(user2);
    }

    public UserDAO getUserDAO() {
        return userDAO;
    }

    @Resource(name = "u")
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void destroy() {
        System.out.println("destroy");
    }
}
UserService.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <context:annotation-config />
    <!-- 使用注解需要下面四条 -->
    <!-- xmlns:context="http://www.springframework.org/schema/context" -->
    <!-- http://www.springframework.org/schema/context -->
    <!-- http://www.springframework.org/schema/context/spring-context-3.1.xsd"> -->
    <!-- <context:annotation-config /> -->

    <!-- 使用aop需要一下 -->
    <!-- xmlns:aop="http://www.springframework.org/schema/aop" -->
    <!-- http://www.springframework.org/schema/aop -->
    <!-- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> -->
    <!-- <aop:aspectj-autoproxy /> -->

    <!-- 扫描 -->
    <context:component-scan base-package="com.ouc.wkp"></context:component-scan>

    <aop:aspectj-autoproxy />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.ouc.wkp.model.User2</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
</beans>
beans.xml

测试程序

package com.ouc.wkp.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ouc.wkp.model.User2;
import com.ouc.wkp.service.UserService;

//Dependency Injection
//Inverse of Control
public class UserServiceTest {

    @Test
    public void testAdd() throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                "beans.xml");

        UserService service = (UserService) ctx.getBean("userService");
        User2 user2=new User2();
        user2.setId(1);
        user2.setUsername("wkp");
        System.out.println(user2);
        service.add(user2);

        ctx.destroy();

    }

}
UserServiceTest.java

比较简单

posted @ 2016-08-17 21:33  docyard  阅读(168)  评论(0编辑  收藏  举报