本文采用正向工程
1.搭建工程 工程名称:mvcsh 2.添加支持的jar包 1).spring-3.2.0.jar注:struts相关的jar不需要
\libs\*.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-logging.jar 2).hibernate-distribution-3.6.10.Final.zip\hibernate3.jar
lib\required\*.jar
backport-util-concurrent.jar ehcache-1.5.0.jarc3p0-0.9.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
log4j.jar slf4j-log4j12-1.6.1.jarmysql-connector-java-5.1.10-bin.jar
3.添加及修改相关配置文件
1).修改web.xml文件 a.添加spring监听与上下文 <!-- spring 监听器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> b.添加springmvc的配置信息 <!-- springmvc的配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 2).添加spring核心配置文件 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" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> 3).添加springmvc核心配置文件 springmvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> </beans> 4).在springmvc-servlet.xml中添加配置信息 a.扫描注解信息 <!-- 1.自动扫描注解 --> <context:component-scan base-package="cn.jbit.mvcwithspring.web.annotation"></context:component-scan> <mvc:annotation-driven/> b.配置视图解析器 <!-- 2.视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> 5).添加属性文件jdbc.properties jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/test jdbc.user = root jdbc.password =root 6).配置spring的配置,在applicationContext.xml文件中添加如下配置 <!-- 1.加载属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.自动描述注解 --> <context:annotation-config/> <context:component-scan base-package="cn.jbit"></context:component-scan> <!-- 3.配置c3p0数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 4.配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 加载连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- hibernate属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 扫描注解po类所在的包 --> <property name="packagesToScan"> <list> <value>cn.jbit.mvcwithspring.domain</value> </list> </property> </bean> <!-- 5.配置Hibernater模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 6.声明事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 7.事务驱动 --> <tx:annotation-driven transaction-manager="transactionManager"/>4.实体类
/mvcsh/src/cn/jbit/mvcwithspring/domain/Student.java @Entity @Table(name="m_student") public class Student implements Serializable { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer studentNo; private String studentName; private Integer age; //省略get和set方法 } 5.持久层 a.接口 public interface IStudentDao { //添加 public void insert(Student student); //查询 public List<Student> select(); } b.实现类 @Repository public class StudentDaoImpl implements IStudentDao { @Autowired private HibernateTemplate hibernateTemplate; public void insert(Student student) { this.hibernateTemplate.save(student); } public List<Student> select() { return this.hibernateTemplate.find("FROM Student"); } } 6.业务逻辑层 a.接口 public interface IStudentService { public void save(Student student); public List<Student> findAll(); } b.实现类 @Service @Transactional public class StudentServiceImpl implements IStudentService { @Autowired private IStudentDao studentDao; public void save(Student student) { this.studentDao.insert(student); } public List<Student> findAll() { return this.studentDao.select(); } }7.控制器
@Controller @RequestMapping("/student") public class StudentController { @Autowired private IStudentService studentService; @RequestMapping("/save") public String save(Student student){ this.studentService.save(student); return "/savesuccess"; } @RequestMapping("/list") public String list(HttpServletRequest request){ List<Student> students = this.studentService.findAll(); request.setAttribute("students", students); return "/list"; } }8.显示层
1).save.jsp 使用表单提交数据 2).list.jsp <table> <tr> <td>学号</td> <td>姓名</td> <td>年龄</td> </tr> <c:forEach items="${students}" var="s" varStatus="vs"> <tr> <td>${s.studentNo }</td> <td>${s.studentName }</td> <td>${s.age }</td> </tr> </c:forEach> </table>3).savesuccess.jsp
添加成功的提示信息