码迷,mamicode.com
首页 > 编程语言 > 详细

spring+hibernate基础

时间:2016-08-17 22:51:24      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享
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

比较简单

spring+hibernate基础

标签:

原文地址:http://www.cnblogs.com/wangkaipeng/p/5781992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!