鲁豫有约马云第几期:spring mvc注解例子
来源:百度文库 编辑:九乡新闻网 时间:2024/05/13 13:40:16
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。
s3h3
contextConfigLocation
classpath:applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.do
index.jsp
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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/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">
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
${dataSource.dialect}
${dataSource.hbm2ddl.auto}
update
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
dataSource.password=123
dataSource.username=root
dataSource.databaseName=test
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.dialect=org.hibernate.dialect.MySQL5Dialect
dataSource.serverName=localhost:3306
dataSource.url=jdbc:mysql://localhost:3306/test
dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}
dataSource.hbm2ddl.auto=update
CREATE TABLE `test`.`student` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`psw` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
)
package com.mvc.entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "name")
private String user;
@Column(name = "psw")
private String psw;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
}
Dao层实现
package com.mvc.dao;
import java.util.List;
public interface EntityDao {
public List
文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。
web.xml配置:
spring-servlet,主要配置controller的信息
applicationContext.xml代码
hibernate.properties数据库连接配置
配置已经完成,下面开始例子
先在数据库建表,例子用的是mysql数据库
建好表后,生成实体类
Dao层实现
spring mvc注解例子
Spring MVC 原理
spring mvc入门教程
Spring的AOP例子
Spring MVC快速上手教程
spring MVC 基本操作方法2
Spring注解详解2
一步一步开发 Spring Framework MVC 应用程序
Spring 系列,第 3 部分: 进入 Spring MVC
spring aop pointcut expression 例子说明
一个spring的简单例子2
Struts VS Spring两种MVC框架比较
Spring 2.5:Spring MVC中的新特性(3) - 编程入门网
MVC
Spring3 MVC (三)----注解基本配置及@Controller和@Reques...
Spring3.0MVC和Hibernate基于annotation注解的整合
MVC详解
中医例子
发布MVC网站注意事项
MVC中Return Json()
spring festival
Spring colors
【引用】Spring
spring 定时器