首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

一个容易的SH例子

2012-09-06 
一个简单的SH例子测试package tarena.testimport java.util.Listimport org.springframework.context.Ap

一个简单的SH例子
测试

package tarena.test;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import tarena.dao.BookDao;import tarena.domain.Book;public class BookDaoTest {public static void main(String[] args) {ApplicationContext txt = new ClassPathXmlApplicationContext("applicationContext.xml");BookDao dao = (BookDao) txt.getBean("bookDao");Book book = new Book("struts core",560);//book.setId(1);//dao.save(book);//dao.update(book);System.out.println(dao.findById(1).getName());//dao.delete(3);List list = dao.findByPrice(10, 3000);for (Object object : list) {Book b = (Book)object;System.out.println(b.getId()+":"+b.getName()+":"+b.getPrice());}}}


applicationContext.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.0.xsd"><!--配置数据源--><bean id="dataSource"value="openlab" /><property name="password" value="open123" /></bean><!--把数据源注入到sessionFactory当中--><bean id="sessionFactory"ref="dataSource" /><property name="mappingResources"><list><value>tarena/domain/Book.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!--使用Hibernate模板--><bean id="ht"ref="sessionFactory" /></bean><bean id="bookDao" name="code">package tarena.dao.hb;import java.util.List;import org.springframework.orm.hibernate3.HibernateTemplate;import tarena.dao.BookDao;import tarena.domain.Book;public class BookDaoImpl implements BookDao {private HibernateTemplate ht;public BookDaoImpl(HibernateTemplate ht) {super();this.ht = ht;}public void delete(int id) {Book b = findById(id);ht.delete(b);}public Book findById(int id) {return (Book)ht.get(Book.class,id);}public List findByPrice(double from, double to) {String hql="from Book b where b.price between ? and ?";return ht.find(hql, new Double[]{from,to});}public void save(Book book) {ht.save(book);}public void update(Book book) {ht.update(book);}}


dao接口
package tarena.dao;import java.util.List;import tarena.domain.Book;public interface BookDao {void save(Book book);void update(Book book);void delete(int id);Book findById(int id);List findByPrice(double from, double to);}


hibernate的映射文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping><class name="tarena.domain.Book" table="laz_book" catalog="openlab"><id name="id"><generator length="32"></column></property><property name="price"><column name="price" length="25"></column></property></class></hibernate-mapping>


pojo类
package tarena.domain;public class Book {private int id;private String name;private double price;public Book() {super();}public Book(String name, double price) {super();this.name = name;this.price = price;}public Book(int id, String name, double price) {super();this.id = id;this.name = name;this.price = price;}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 double getPrice() {return price;}public void setPrice(double price) {this.price = price;}}

热点排行