创建web项目
引入类库
antlr-2.7.6.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.javax.validation-1.0.0.GA.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-collections-3.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
dom4j-1.6.1.jar
hibernate-validator-4.1.0.Final.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j.jar
mysql-connector-java-5.0.8-bin.jar
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.context.support-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.orm-3.1.0.RELEASE.jar
org.springframework.transaction-3.0.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.servlet-3.1.0.RELEASE.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
com.springsource.javax.annotation-1.0.0.jar
com.springsource.javax.xml.bind-2.1.7.jar
配置web.xml文件
编写beans.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 组件扫描 -->
<context:component-scan base-package="com.cc.springmvc"></context:component-scan>
<!-- 定义数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost/springmvc" />
<property name="user" value="root" />
<property name="password" value="123" />
<property name="initialPoolSize" value="10"/>
<property name="maxPoolSize" value="50"/>
<property name="minPoolSize" value="10"/>
</bean>
<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate映射文件的位置 -->
<property name="mappingDirectoryLocations">
<value>classpath:com/cc/springmvc/domain/</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.Dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl">update</prop>
</props>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf"></property>
</bean>
<!-- 配置事物的传播特性 (事物通知)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.cc.springmvc.service.*.*(..))" advice-ref="txAdvice"/>
</aop:config>
</beans>
编写spring-mvc.xml
定义Person实体类
public class Person {
private String id;
private String name;
private String address;
/**
* @return the id
*/
public final String getId() {
return id;
}
/**
* @param id the id to set
*/
public final void setId(String id) {
System.out.println("正在调用setId方法 ,id=" + id);
this.id = id;
}
/**
* @return the name
*/
public final String getName() {
return name;
}
/**
* @param name the name to set
*/
public final void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public final String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public final void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "{id:"+id+",name:"+name+",address:"+address+"}";
}
}
定义Person实体类的映射文件Person.hbm.xml
创建数据库和表
定义Dao的接口和实现类 ,并注入sessionFactory
public interface IPersonDao {
public void savePerson(Person p);
public void deletePerson(Person p);
public void updatePerson(Person p);
public Person findPersonById(String id);
public List<Person> findAllPerson();
}
@Repository(value="personDao")
public class PersonDaoImpl implements IPersonDao {
@Resource(name="sf")
private SessionFactory sf;
public void savePerson(Person p) {
sf.getCurrentSession().save(p);
}
public void deletePerson(Person p) {
sf.getCurrentSession().delete(p);
}
public List<Person> findAllPerson() {
List<Person> list = sf.getCurrentSession().createQuery("from Person").list();
return list;
}
public Person findPersonById(String id) {
return (Person) sf.getCurrentSession().get(Person.class, id);
}
public void updatePerson(Person p) {
sf.getCurrentSession().update(p);
}
}
定义Service的接口和实现类,并注入dao
public interface IPersonService {
public void savePerson(Person p);
public void deletePerson(Person p);
public void updatePerson(Person p);
public Person findPersonById(String id);
public List<Person> findAllPerson();
}
@Service(value="personService")
public class PersonServiceImpl implements IPersonService {
@Resource(name="personDao")
private IPersonDao personDao;
public void savePerson(Person p) {
personDao.savePerson(p);
}
public void deletePerson(Person p) {
personDao.deletePerson(p);
}
public List<Person> findAllPerson() {
return personDao.findAllPerson();
}
public Person findPersonById(String id) {
return personDao.findPersonById(id);
}
public void updatePerson(Person p) {
personDao.updatePerson(p);
}
}
定义Controller,并注入service
@Controller
@RequestMapping(value="/person")//根路径
public class PersonController {
@Resource(name="personService")
private IPersonService personService;
@RequestMapping(value="/savePersonUI")
public String savePersonUI(){
return "savePersonForm";
}
@RequestMapping(value="/save")
public String savePerson(Person p){
personService.savePerson(p);
System.out.println("savePerson()...");
return "redirect:/person/findAllPerson";
}
@RequestMapping(value="/findAllPerson")
public String findAllPerson(HttpServletRequest req){
List<Person> persons = personService.findAllPerson();
req.setAttribute("persons", persons);
return "personList";
}
@RequestMapping(value="/deletePerson")
public String deletePerson(Person p){
personService.deletePerson(p);
return "redirect:/person/findAllPerson";
}
@RequestMapping(value="/deletePersons")
public String deletePersons(String ids){
ids = ids.substring(0, ids.length() -1);
String[] idss = ids.split(",");
Person p = new Person();
for(int i=0;i<idss.length;i++){
p.setId(idss[i]);
personService.deletePerson(p);
}
return "redirect:/person/findAllPerson";
}
@RequestMapping(value="/updatePersonUI")
public String updatePersonUI(String id,HttpServletRequest req){
Person p = personService.findPersonById(id);
req.setAttribute("p", p);
return "updatePersonForm";
}
@RequestMapping(value="/updatePerson")
public String updatePerson(Person p){
personService.updatePerson(p);
return "redirect:/person/findAllPerson";
}
}
创建jsp页面
测试
说明:SpringMVC+Hibernate+Spring整合完成