测试
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&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;
}
}
分享到:
相关推荐
这是一个S2SH整合实现增删改查的完整例子
这是一个简单的整合,结合一个登录来实现,如有问题请留言。
ssh整合小例子,功能简单(增删改查_查是用在改的那里根据id查),通俗易懂!多多包涵!
内容为struts2.2.3+spring3.0.1+hibernate3.2框架例子工程,包括webroot和src文件夹,所需Jar包和配置文件,一个简单的查询数据列表显示到页面的例子代码
S2SH+extjs做的简单CURD功能小例子 初学EXTJS一周做的,网上看到N多例子没有源码或是有错误 这个刚写好 数据库用sqlserver2005 例子比较完整 就是没有放jar包(只有jdbc驱动jar和json的jar) 可以自己搭个S2SH环境把...
针对J2EE的S2SH框架做的一个简单增删改查 登陆等等 新手学习 简单易学
简单s2sh的例子。从该demo中能让你迅速熟悉s2sh框架原理。
用struts2.2.3 hibiernate3.3.2 spring2.5.6做的简单登陆小例子,数据库用的mysql,数据库建表语句已经包含在上传的文件中。可以登陆和注册,登陆之后,退出时,会清空sessio。如有疑问可以加QQ137084612。
build 脚本例子build 脚本例子build 脚本例子
简单例子 action里很多东西应该写到service层的 用spring管理hibernate事务 IDE: myclipse7.0 struts2.014+spring2.5+hibernate3.2+mysql 导入项目和数据库 部署一下就可以看到效果
简单例子 action里很多东西应该写到service层的 用spring管理hibernate事务 IDE: myclipse7.0 struts2.014+spring2.5+hibernate3.2+mysql 导入项目和数据库 部署一下就可以看到效果
go-sh是一个golang的调用shell的库。 使用linux的人都知道...如下是一个shell脚本转换golang代码的例子。 然后借助http://gobuild.io提供的在线golang交叉编译服务,整个过程就无敌了。 标签:gosh
一步一步的详细说明整合的过程(登录验证的例子),保证运行正常,让初学者一目了然
新手,近段时间正在自学jbpm4.4,然后花了很长一段时间做会签,并与ssh集成,今天终于做出来。会签分配任务时,我用的是:task assignment handler,可在页面上显示流程图。开发环境为:jdk1.6.0_18+mysql5.1.48+...
Struts2+Hibernate+Spring的考勤系统实现的简单例子源码,这是一个非常全面的例子,同时浅显易懂,比较适合初学者
一个简单的 docker-compose 示例。 它使用 socat 创建一个可在 localhost:12346 上访问的 sqlite 服务器。 跑步: ./setup_dbs.sh docker-compose up telnet localhost 12346 [another terminal] telnet ...
运行脚本$ sh stodo.sh句法名称日期时间格式为YYYY-MM-DD / HH:MM 例如〜/ todo文件的内容是: 工作2021-03-14 / 23:14 游戏2021-03-14 / 23:40例子$ sh todo.sh -f /home/learner/todo -F /home/user/Documents/...
三大框架整合的一个小例子,简单易懂,需要引入的包由于过大,已用截图展示。只需自己下载配置就可以了。
这个包基本上只是包裹在labix.org/v2/pipe周围的语法糖,它反过来又包裹了os/exec,但是当你有一个简单的类似shell的行而不是一个巨大的行时,它可以使你的代码更容易阅读混乱的管道、命令和转换等。 #####...
一个简单的struts2+spring+hibernate+commons-email整合的例子