- Spring初学快速入门
- 安装Spring工具套件到Eclipse
- Spring快速入门
- Spring自动装配Bean
- Spring JDBC支持
- Spring JavaConfig
- Spring 依赖注入(DI)
-
Spring Bean基础
- Spring Bean引用例子
- 如何注入值到Spring bean属性
- Spring bean加载多个配置文件
- Spring内部bean实例
- Spring Bean作用域实例
- Spring集合 (List,Set,Map,Properties) 实例
- Spring ListFactoryBean实例
- Spring SetFactoryBean实例
- Spring MapFactoryBean例子
- Spring注入日期到bean属性-CustomDateEditor
- Spring PropertyPlaceholderConfigurer实例
- Spring bean配置继承
- Spring依赖检查
- Spring使用@Required注解依赖检查
- Spring自定义@Required-style注解
- Spring Bean InitializingBean和DisposableBean实例
- Spring Bean init-method 和 destroy-method实例
- Spring @PostConstruct和@PreDestroy实例
- Spring表达式语言
- Spring自动组件扫描
- Spring AOP (面向方面编程)
- Spring AOP + AspectJ框架
- Spring Hibernate支持
- Spring E-mail支持
-
Spring与其它Web框架集成
Spring+Hibernate+MySql实例
最终项目结构
1.创建表
CREATE TABLE `zyiz`.`stock` ( `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `STOCK_CODE` varchar(10) NOT NULL, `STOCK_NAME` varchar(20) NOT NULL, PRIMARY KEY (`STOCK_ID`) USING BTREE, UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`), UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. Model & BO & DAO
Stock Model
package com.zyiz.stock.model; import java.io.Serializable; public class Stock implements Serializable { private static final long serialVersionUID = 1L; private Long stockId; private String stockCode; private String stockName; //getter and setter methods... }
Stock Business Object (BO))
package com.zyiz.stock.bo; import com.zyiz.stock.model.Stock; public interface StockBo { void save(Stock stock); void update(Stock stock); void delete(Stock stock); Stock findByStockCode(String stockCode); }
package com.zyiz.stock.bo.impl; import com.zyiz.stock.bo.StockBo; import com.zyiz.stock.dao.StockDao; import com.zyiz.stock.model.Stock; public class StockBoImpl implements StockBo{ StockDao stockDao; public void setStockDao(StockDao stockDao) { this.stockDao = stockDao; } public void save(Stock stock){ stockDao.save(stock); } public void update(Stock stock){ stockDao.update(stock); } public void delete(Stock stock){ stockDao.delete(stock); } public Stock findByStockCode(String stockCode){ return stockDao.findByStockCode(stockCode); } }
Stock Data Access Object
Stock DAO接口和实现,DAO实现类扩展了 Spring 的“HibernateDaoSupport”,以使Spring框架支持Hibernate。 现在,你可以通过getHibernateTemplate()执行 Hibernate 功能。
package com.zyiz.stock.dao; import com.zyiz.stock.model.Stock; public interface StockDao { void save(Stock stock); void update(Stock stock); void delete(Stock stock); Stock findByStockCode(String stockCode); }
package com.zyiz.stock.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.zyiz.stock.dao.StockDao; import com.zyiz.stock.model.Stock; public class StockDaoImpl extends HibernateDaoSupport implements StockDao{ public void save(Stock stock){ getHibernateTemplate().save(stock); } public void update(Stock stock){ getHibernateTemplate().update(stock); } public void delete(Stock stock){ getHibernateTemplate().delete(stock); } public Stock findByStockCode(String stockCode){ List list = getHibernateTemplate().find( "from Stock where stockCode=?",stockCode ); return (Stock)list.get(0); } }
5.资源配置
Hibernate Configuration
<?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.zyiz.stock.model.Stock" table="stock" catalog="zyiz"> <id name="stockId" type="java.lang.Long"> <column name="STOCK_ID" /> <generator class="identity" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> <property name="stockName" type="string"> <column name="STOCK_NAME" length="20" not-null="true" unique="true" /> </property> </class> </hibernate-mapping>
Spring Configuration
Database related….
创建一个属性文件(database.properties)数据库的详细信息,把它放在“resources/properties” 文件夹中。这是很好的做法,不同于数据库细节并将 Spring bean 配置成不同的文件。
database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/zyiz jdbc.username=root jdbc.password=password
DataSource.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>properties/database.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> </beans>
Hibernate 关联….
创建一个会话工厂 bean 配置文件(Hibernate.xml),把它放入 “resources/database” 文件夹中。这个 LocalSessionFactoryBean 中设置一个共享的 Hibernate SessionFactory 在一个 Spring 应用程序上下文。
Hibernate.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>/hibernate/Stock.hbm.xml</value> </list> </property> </bean> </beans>
Spring beans related….
创建一个bean配置文件(Stock.xml)的BO和DAO类,把它放入 “resources/spring” 文件夹中。依赖的 DAO(stockDao)bean 注入到 bo(stockBo)bean; SessionFactory 的 bean 成stockDao。
Stock.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Stock business object --> <bean id="stockBo" class="com.zyiz.stock.bo.impl.StockBoImpl" > <property name="stockDao" ref="stockDao" /> </bean> <!-- Stock Data Access Object --> <bean id="stockDao" class="com.zyiz.stock.dao.impl.StockDaoImpl" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
BeanLocations.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- Database Configuration --> <import resource="../database/DataSource.xml"/> <import resource="../database/Hibernate.xml"/> <!-- Beans Declaration --> <import resource="../beans/Stock.xml"/> </beans>
6. 运行它
package com.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyiz.stock.bo.StockBo; import com.zyiz.stock.model.Stock; public class App { public static void main( String[] args ) { ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml"); StockBo stockBo = (StockBo)appContext.getBean("stockBo"); /** insert **/ Stock stock = new Stock(); stock.setStockCode("7668"); stock.setStockName("HAIO"); stockBo.save(stock); /** select **/ Stock stock2 = stockBo.findByStockCode("7668"); System.out.println(stock2); /** update **/ stock2.setStockName("HAIO-1"); stockBo.update(stock2); /** delete **/ stockBo.delete(stock2); System.out.println("Done"); } }
输出结果:
Hibernate: insert into zyiz.stock (STOCK_CODE, STOCK_NAME) values (?, ?) Hibernate: select stock0_.STOCK_ID as STOCK1_0_, stock0_.STOCK_CODE as STOCK2_0_, stock0_.STOCK_NAME as STOCK3_0_ from zyiz.stock stock0_ where stock0_.STOCK_CODE=? Stock [stockCode=7667, stockId=1, stockName=HAIO] Hibernate: update zyiz.stock set STOCK_CODE=?, STOCK_NAME=? where STOCK_ID=? Done
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
扫描二维码
程序员编程王