Spring-hibernate使用全xml配置结合方式搭建开发
来源:互联网 发布:中越海警船 知乎 编辑:程序博客网 时间:2024/05/17 02:45
Spring-hibernate使用全xml配置结合方式搭建开发
1.新建mysql数据库表:
DROP TABLE IF EXISTS `person`;CREATE TABLE `person` ( `pid` int(11) NOT NULL, `name` varchar(20) DEFAULT NULL, `description` varchar(50) DEFAULT NULL, PRIMARY KEY (`pid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;2.新建web工程导入jar包。jdbc.properties 在MATE-INF下:
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/sshjdbc.username=rootjdbc.password=tiger3.applicationContext.xml配置数据源,ioc和aop,也可以在这里引用 hibernate.cfg.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 数据源 --><beanclass="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><!-- sessionFactory配置 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 数据源 --><property name="dataSource" ref="dataSource"/><!-- ORM映射文件 --><property name="mappingResources"><list><value>com/zl/test2/entity/User.hbm.xml</value></list></property><!-- 其他配置 --><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hbm2ddl.auto">update</prop></props></property></bean><!-- 也可以使用这种方式配置,多了一个xml<bean id="sessionFactory2"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property></bean>--><!-- DAO数据处理层 --><bean id="userDao2" class="com.zl.test2.dao.impl.UserDaoImplForHibernate"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><!-- =====================================1织入 --><aop:config><!-- 定义切入点 expression="切入表达式" --><aop:pointcut expression="execution(* com.zl.test2.dao.*.*(..))" id="txCutPoint"/><!-- 织入:将通知切入到指定的切入点 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txCutPoint"/></aop:config><!-- 通知【细化到方法】 ====================2--><tx:advice id="txAdvice" transaction-manager="transcationManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED"/><tx:method name="find*" read-only="true" propagation="SUPPORTS"/><tx:method name="get*" read-only="true" propagation="SUPPORTS"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 事务管理器 =============1--><bean id="transcationManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean></beans>4.entity:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.zl.test2.entity.User"><id column="id" name="id"><generator class="identity"></generator></id><property name="name" type="string"></property><property name="age" type="int"></property><property name="phone" type="string"></property></class></hibernate-mapping>
package com.zl.test2.entity;/** * 用户类 * * @author wenber * */public class User {private int id;private String name;private int age;private String phone;public User() {}public User(String name, int age, String phone) {super();this.name = name;this.age = age;this.phone = phone;}public User(int id, String name, int age, String phone) {super();this.id = id;this.name = name;this.age = age;this.phone = phone;}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;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}5.dao层:
package com.zl.test2.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.zl.test2.dao.IUserDao;import com.zl.test2.entity.User;public class UserDaoImplForHibernate extends HibernateDaoSupport implementsIUserDao {@Overridepublic List<User> findUserList() {return this.getHibernateTemplate().find("from User");}@Overridepublic void saveUser(User user) {this.getHibernateTemplate().save(user);// this.getHibernateTemplate().save(user);}}6.调用:
package com.zl.test2.view;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zl.test2.dao.IUserDao;import com.zl.test2.entity.User;public class PersonServiceTest {public static void main(String[] args) {testSavePerson();}public static void testSavePerson() {ApplicationContext context = new ClassPathXmlApplicationContext("com/zl/test2/applicationContext.xml");IUserDao userDao = (IUserDao) context.getBean("userDao2");User u = new User(12, "zl", 1, "ee");userDao.saveUser(u);}}
7:源代码下载:点击打开链接
http://download.csdn.net/detail/qq_31968809/9772706
0 0
- Spring-hibernate使用全xml配置结合方式搭建开发
- struts2+spring+hibernate s2sh结合方式框架搭建开发
- struts2+spring+hibernate s2sh半注解结合方式框架搭建开发
- Spring 结合 Hibernate 配置 C3P0
- spring结合hibernate cache配置
- Spring 结合 Hibernate 配置 C3P0
- Spring 结合 Hibernate 配置 C3P0
- Spring 结合 Hibernate 配置 C3P0
- spring jpa结合hibernate配置
- hibernate xml配置方式
- spring使用xml方式配置属性值
- spring+hibernate 配置、搭建
- struts,hibernate , spring结合使用
- Spring与Hibernate结合使用
- spring全注解配置(不使用xml配置)
- spring+hibernate xml和注解方式配置事务
- Spring整合Hibernate管理事务(xml配置方式)
- Spring整合Hibernate配置(使用hibernate.cfg.xml文件)
- 大神博客网址
- 汉诺塔问题
- WebView在三星note4只显示一半的bug
- 浏览器 之 无头浏览器
- AngularJS的Filter用法详解
- Spring-hibernate使用全xml配置结合方式搭建开发
- data augmentation 数据增强方法总结
- Android SDK Manager Extras中没有 Intel x86 Emulator Accelerator (HAXM installer)
- ES6的一些常用特性
- 用于 TI Stellaris 和 Concerto 微控制器可扩展的 RTOS 解决方案
- SpringMVC02-Controller与RequestMapping
- Python访问Mysql分表数据的方法示例
- ./asadmin: 52: exec: java: not found
- XAMPP的使用