67,549
社区成员




<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="book" namespace="" extends="struts-default">
<action name="login" class="saveBook">
<result name="success">/jsp/success.jsp</result>
</action>
</package>
</struts>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/book"></property>
<property name="username" value="root"></property>
<property name="password" value="263015"></property>
<property name="initialSize" value="3"></property>
<property name="maxActive" value="500"></property>
<property name="maxIdle" value="2"></property>
<property name="minIdle" value="1"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>domain/Book.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="BookDAO" class="domain.BookDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="Serviceimpl" class="service.ServiceImpl">
<property name="bookDAO" ref="BookDAO"></property>
</bean>
<bean id="saveBook" class="action.SaveBookAction">
<property name="funService" ref="Serviceimpl"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
public class SaveBookAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private Book book;
private ServiceImpl funService;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public FunService getFunService() {
return funService;
}
public void setFunService(ServiceImpl funService) {
this.funService = funService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("****************");
this.funService.save(book);
return SUCCESS;
}
}
public class Book implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private String password;
private Integer age;
// Constructors
/** default constructor */
public Book() {
}
/** minimal constructor */
public Book(String username, Integer age) {
this.username = username;
this.age = age;
}
/** full constructor */
public Book(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return this.age;
}
public void setAge(String age) {
this.age = Integer.parseInt(age);
System.out.println(this.age);
}
}
package domain;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
/**
* A data access object (DAO) providing persistence and search support for Book
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see domain.Book
* @author MyEclipse Persistence Tools
*/
@Transactional
public class BookDAO extends HibernateDaoSupport{
private static final Logger log = LoggerFactory.getLogger(BookDAO.class);
// property constants
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AGE = "age";
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
protected void initDao() {
// do nothing
}
public void save(Book transientInstance) {
//System.out.println("-------------");
//Transaction tr = currentSession().beginTransaction();
//Transaction transaction= getCurrentSession().beginTransaction();
log.debug("saving Book instance");
try {
getCurrentSession().save(transientInstance);
//getHibernateTemplate().save(transientInstance);
//tr.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Book persistentInstance) {
log.debug("deleting Book instance");
try {
getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Book findById(java.lang.Integer id) {
log.debug("getting Book instance with id: " + id);
try {
Book instance = (Book) getCurrentSession().get("domain.Book", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Book instance) {
log.debug("finding Book instance by example");
try {
List results = getCurrentSession().createCriteria("domain.Book")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Book instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Book as model where model."
+ propertyName + "= ?";
Query queryObject = getCurrentSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findByAge(Object age) {
return findByProperty(AGE, age);
}
public List findAll() {
log.debug("finding all Book instances");
try {
String queryString = "from Book";
Query queryObject = getCurrentSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Book merge(Book detachedInstance) {
log.debug("merging Book instance");
try {
Book result = (Book) getCurrentSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Book instance) {
log.debug("attaching dirty Book instance");
try {
getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Book instance) {
log.debug("attaching clean Book instance");
try {
getCurrentSession().buildLockRequest(LockOptions.NONE).lock(
instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
然后是自己变得service接口和类:
package service;
import domain.Book;
public interface FunService {
public void save(Book b);
}
package service;
import domain.Book;
import domain.BookDAO;
public class ServiceImpl implements FunService{
private BookDAO bookDAO;
public BookDAO getBookDAO() {
return bookDAO;
}
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
@Override
public void save(Book b) {
// TODO Auto-generated method stub
//System.out.println("----------------");
this.bookDAO.save(b);
}
}