Spring再接触 整合Hibernate
首先更改配置文件
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<aop:aspectj-autoproxy /> </beans>
beans.xml
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt" /> <!--
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="bjsxt" />
</bean>
--> <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.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</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>
User.java
package com.bjsxt.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class User {
private int id;
private String name; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
UserDao.java
package com.bjsxt.dao;
import com.bjsxt.model.User; public interface UserDAO {
public void save(User user);
}
UserDaoImpl
package com.bjsxt.dao.impl; import java.sql.SQLException; import javax.annotation.Resource; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component("u")
public class UserDAOImpl implements UserDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} public void save(User user) { //Hibernate
//JDBC
//XML
//NetWork
System.out.println("session factory class:" + sessionFactory.getClass());
Session s = sessionFactory.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
System.out.println("user saved!");
//throw new RuntimeException("exeption!");
} }
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=bjsxt
Test
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User()); ctx.destroy(); }
Spring再接触 整合Hibernate的更多相关文章
- Spring学习7-Spring整合Hibernate
一.Springl为什么要整合Hibernate 二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...
- Spring Data初步--整合Hibernate
Spring Data课程中的技术介绍 Hibernate: Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了非常轻量级的对象封装,它将 pojo 与数据库表建立映射关系 ...
- Spring再接触 IOC DI
直接上例子 引入spring以及Junite所需要的jar包 User.java package com.bjsxt.model; public class User { private String ...
- Spring再接触 模拟Spring
项目分层: 1.最土的方法是直接写到main中去 2.分出model层 2.如下 4.在抽象一个对数据库的访问层(跨数据库实现) 面向抽象编程 User.java package com.bjsxt. ...
- Spring再接触 Annotation part2
resource resource beans.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...
- Spring再接触 自动装配
UserDaoImpl package com.bjsxt.dao.impl; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; p ...
- Spring再接触 Scope范围
<bean id="userService" class="com.bjsxt.service.UserService" scope="prot ...
- Spring再接触 Annotation part1
使用annotation首先得加这两条代码 beans.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring再接触 生命周期
Userservice.java package com.bjsxt.service; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.Use ...
随机推荐
- QQ浏览器中时区bug
在QQ浏览器 4.4.119.400 版本中,通过new Date('2018-11-11').getTime(); 获取的时间不是东8区的时间戳,而是0时区的时间戳,这就导致了获取的时间与实际的时间 ...
- is_numeric — 检测变量是否为数字或数字字符串
is_numeric — 检测变量是否为数字或数字字符串 bool is_numeric ( mixed $var ) 如果 var 是数字和数字字符串则返回 TRUE ,否则返回 FALSE . 参 ...
- 1.2.7 Excel表格打印技巧
1.打印居中显示 把打印的内容放在纸张中间,居中对齐. 方法: ①点击[页面布局]选项卡: ②找到[页面设置]群组点击右下角显示更多: ③在页边距对话框中选择[页边距]: ④勾选[居中方式]中的[水平 ...
- C# 控件消失等问题
控件消失原因: 1.新控件的触发导致页面重载,该重载有没有达到原有控件的触发状态进而消失. 2.(目前只发现这一点,后续又发现再更...) 1.示例: ASPX: <div> <!- ...
- 定时任务模块 schedule
# coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...
- mysql 8.0 主从复制的优化
mysql 8.0复制改进一简介: 基于GTID下的并行复制,本文不考虑MGR架构二 主要特性 1 基于writeset的下的改进型并行复制 我在之前的一篇文章关于并行复制中详细的介绍了关 ...
- CentOS7.3安装Go运行和开发环境
https://blog.csdn.net/warnerwu/article/details/73825105
- 你云我云•兄弟夜谈会 第三季 企业IT架构
你云我云•兄弟夜谈会 第三季 企业IT架构 你云我云•兄弟夜谈会 第二季 5G 你云我云•兄弟夜谈会 第一季 企业云 0. 概况 时间:2019年2月23日 22:00~23:30 主题:企业IT架构 ...
- airflow整体架构
run命令运行过程 读取dag文件生成task依赖关系,然后生成封装airflow run的command命令,通过celery发送到executor端,重新执行该airflow run命令. sch ...
- Scaffold-DbContext 命令参数
NAME Scaffold-DbContext SYNOPSIS Scaffolds a DbContext and entity types for a database. SYNTAX Scaff ...