一个简单的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"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>
				jdbc:mysql://localhost:3306/openlab?useUnicode=true&amp;characterEncoding=utf8
			</value>
		</property>
		<property name="username" value="openlab" />
		<property name="password" value="open123" />
	</bean>

	<!--把数据源注入到sessionFactory当中-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" 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"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<bean id="bookDao" class="tarena.dao.hb.BookDaoImpl">
		<constructor-arg ref="ht"/>
	</bean>

</beans>



接口的实现类
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 class="native"></generator>
		</id>
		<property name="name">
			<column name="name" 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;
	}
}

你可能感兴趣的:(java,DAO,Hibernate,mysql,xml)