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 ...
随机推荐
- SparkStream:4)foreachRDD详解
转载自:http://blog.csdn.net/jiangpeng59/article/details/53318761 foreachRDD通常用来把SparkStream运行得到的结果保存到外部 ...
- Python中安装MySQL
Windows 下Python3.6安装 mysql_python 存在各种不成功,切换到 SQLAlchemy也不行需要安装MySQL_python.需要安装mysqlclient. 执行 pip ...
- Linux DNS 服务器安装、配置和维护
每个 IP 地址都可以有一个主机名,主机名由一个或多个字符串组成,字符串之间用小数点隔开.有了主机名,就不要死记硬背每台 IP 设备的 IP 地址,只要记住相对直观有意义的主机名就行了.这就是 DNS ...
- 常用java的正则表达式
package everyDayPratise; import java.util.regex.Pattern; public class RegexExample { public static v ...
- 【java】之位运算^,&,<<,>>,<<<,>>>总结
1.^(亦或运算) ,针对二进制,相同的为0,不同的为1 public static void main(String[] args) { System.out.println("2^3运算 ...
- python request Payload 数据处理
普通的http的post请求的请求content-type类型是:Content-Type:text/html; charset=UTF-8, 而另外一种形式request payload,其Cont ...
- linux中的查找命令find,locate,which,whereis
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了. which 查看可执行文 ...
- 二、初步认识springBoot的pom.xml
1. 大体结构 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...
- 02_编写Table的CRUD
1.使用EF的Code First模式生成DbContext和表对应的实体类 2.编写CRUD接口: 3.集成Swagger接口生成工具,方便测试使用: https://www.cnblogs.com ...
- MapReduce论文学习
MapReduce和区块链有什么相同的地方? 我的天哪,他俩还有相同的地方呢.我书读的少,你别骗我. 他俩还真有相同点,绝不忽悠. 他俩都有一个高大上的名字. 区块链就是一个分布式数据库,并不是什么神 ...