首先更改配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <context:annotation-config />
  13. <context:component-scan base-package="com.bjsxt"/>
  14. <aop:aspectj-autoproxy />
  15.  
  16. </beans>

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <context:annotation-config />
  13. <context:component-scan base-package="com.bjsxt" />
  14.  
  15. <!--
  16. <bean id="dataSource"
  17. class="org.apache.commons.dbcp.BasicDataSource"
  18. destroy-method="close">
  19.  
  20. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  21. <property name="url" value="jdbc:mysql://localhost:3306/spring" />
  22. <property name="username" value="root" />
  23. <property name="password" value="bjsxt" />
  24. </bean>
  25. -->
  26.  
  27. <bean
  28. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  29. <property name="locations">
  30. <value>classpath:jdbc.properties</value>
  31. </property>
  32. </bean>
  33.  
  34. <bean id="dataSource" destroy-method="close"
  35. class="org.apache.commons.dbcp.BasicDataSource">
  36. <property name="driverClassName"
  37. value="${jdbc.driverClassName}" />
  38. <property name="url" value="${jdbc.url}" />
  39. <property name="username" value="${jdbc.username}" />
  40. <property name="password" value="${jdbc.password}" />
  41. </bean>
  42.  
  43. <bean id="sessionFactory"
  44. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  45. <property name="dataSource" ref="dataSource" />
  46. <property name="annotatedClasses">
  47. <list>
  48. <value>com.bjsxt.model.User</value>
  49. </list>
  50. </property>
  51. <property name="hibernateProperties">
  52. <props>
  53. <prop key="hibernate.dialect">
  54. org.hibernate.dialect.MySQLDialect
  55. </prop>
  56. <prop key="hibernate.show_sql">true</prop>
  57. </props>
  58. </property>
  59. </bean>
  60.  
  61. </beans>

User.java

  1. package com.bjsxt.model;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6.  
  7. @Entity
  8. public class User {
  9. private int id;
  10. private String name;
  11.  
  12. @Id
  13. @GeneratedValue
  14. public int getId() {
  15. return id;
  16. }
  17. public void setId(int id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26.  
  27. }

UserDao.java

  1. package com.bjsxt.dao;
  2. import com.bjsxt.model.User;
  3.  
  4. public interface UserDAO {
  5. public void save(User user);
  6. }

UserDaoImpl

  1. package com.bjsxt.dao.impl;
  2.  
  3. import java.sql.SQLException;
  4.  
  5. import javax.annotation.Resource;
  6.  
  7. import org.hibernate.Session;
  8. import org.hibernate.SessionFactory;
  9. import org.springframework.stereotype.Component;
  10.  
  11. import com.bjsxt.dao.UserDAO;
  12. import com.bjsxt.model.User;
  13.  
  14. @Component("u")
  15. public class UserDAOImpl implements UserDAO {
  16.  
  17. private SessionFactory sessionFactory;
  18.  
  19. public SessionFactory getSessionFactory() {
  20. return sessionFactory;
  21. }
  22.  
  23. @Resource
  24. public void setSessionFactory(SessionFactory sessionFactory) {
  25. this.sessionFactory = sessionFactory;
  26. }
  27.  
  28. public void save(User user) {
  29.  
  30. //Hibernate
  31. //JDBC
  32. //XML
  33. //NetWork
  34. System.out.println("session factory class:" + sessionFactory.getClass());
  35. Session s = sessionFactory.openSession();
  36. s.beginTransaction();
  37. s.save(user);
  38. s.getTransaction().commit();
  39. System.out.println("user saved!");
  40. //throw new RuntimeException("exeption!");
  41. }
  42.  
  43. }

jdbc.properties

  1. jdbc.driverClassName=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/spring
  3. jdbc.username=root
  4. jdbc.password=bjsxt

Test

  1. @Test
  2. public void testAdd() throws Exception {
  3. ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  4.  
  5. UserService service = (UserService)ctx.getBean("userService");
  6. System.out.println(service.getClass());
  7. service.add(new User());
  8.  
  9. ctx.destroy();
  10.  
  11. }

Spring再接触 整合Hibernate的更多相关文章

  1. Spring学习7-Spring整合Hibernate

    一.Springl为什么要整合Hibernate   二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...

  2. Spring Data初步--整合Hibernate

    Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...

  3. Spring再接触 IOC DI

    直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...

  4. Spring再接触 模拟Spring

    项目分层: 1.最土的方法是直接写到main中去 2.分出model层 2.如下 4.在抽象一个对数据库的访问层(跨数据库实现) 面向抽象编程 User.java package com.bjsxt. ...

  5. Spring再接触 Annotation part2

    resource resource beans.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...

  6. Spring再接触 自动装配

    UserDaoImpl package com.bjsxt.dao.impl; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; p ...

  7. Spring再接触 Scope范围

    <bean id="userService" class="com.bjsxt.service.UserService" scope="prot ...

  8. Spring再接触 Annotation part1

    使用annotation首先得加这两条代码 beans.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  9. Spring再接触 生命周期

    Userservice.java package com.bjsxt.service; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.Use ...

随机推荐

  1. SparkStream:4)foreachRDD详解

    转载自:http://blog.csdn.net/jiangpeng59/article/details/53318761 foreachRDD通常用来把SparkStream运行得到的结果保存到外部 ...

  2. Python中安装MySQL

    Windows 下Python3.6安装 mysql_python 存在各种不成功,切换到 SQLAlchemy也不行需要安装MySQL_python.需要安装mysqlclient. 执行 pip ...

  3. Linux DNS 服务器安装、配置和维护

    每个 IP 地址都可以有一个主机名,主机名由一个或多个字符串组成,字符串之间用小数点隔开.有了主机名,就不要死记硬背每台 IP 设备的 IP 地址,只要记住相对直观有意义的主机名就行了.这就是 DNS ...

  4. 常用java的正则表达式

    package everyDayPratise; import java.util.regex.Pattern; public class RegexExample { public static v ...

  5. 【java】之位运算^,&,<<,>>,<<<,>>>总结

    1.^(亦或运算) ,针对二进制,相同的为0,不同的为1 public static void main(String[] args) { System.out.println("2^3运算 ...

  6. python request Payload 数据处理

    普通的http的post请求的请求content-type类型是:Content-Type:text/html; charset=UTF-8, 而另外一种形式request payload,其Cont ...

  7. linux中的查找命令find,locate,which,whereis

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了.  which       查看可执行文 ...

  8. 二、初步认识springBoot的pom.xml

    1.  大体结构 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  9. 02_编写Table的CRUD

    1.使用EF的Code First模式生成DbContext和表对应的实体类 2.编写CRUD接口: 3.集成Swagger接口生成工具,方便测试使用: https://www.cnblogs.com ...

  10. MapReduce论文学习

    MapReduce和区块链有什么相同的地方? 我的天哪,他俩还有相同的地方呢.我书读的少,你别骗我. 他俩还真有相同点,绝不忽悠. 他俩都有一个高大上的名字. 区块链就是一个分布式数据库,并不是什么神 ...