springmvc注解配置例子的实例代码下载

 弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的 spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。

原创整理不易,转载请注明出处:springmvc注解配置例子的实例代码下载

代码下载地址: http://www.zuidaima.com/share/1751860180044800.htm


文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。

web.xml配置:

 
  
  s3h3  
  
  contextConfigLocation  
  classpath:applicationContext * .xml  
  
  
  org.springframework.web.context.ContextLoaderListener  
  
 
  
  spring  
  org.springframework.web.servlet.DispatcherServlet  
   
  
  
  spring  
 
 * . do  
  
  
  index.jsp  
  
  

 

spring-servlet,主要配置controller的信息

 
  
 
  
  
  
  
  
 
  
  
 
  
  

 

applicationContext.xml代码

 
  
 
  
   
 
  
 
  
  
  
  
 ${dataSource.dialect}  
 ${dataSource.hbm2ddl.auto}  
 update  
  
  
  
  
  com.mvc.entity   
  
  
  
 
  
  
  
  
 
  
  
  
  
  
  
  
  
  
  
  
  
 
  
  

 

hibernate.properties数据库连接配置

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 

 

配置已经完成,下面开始例子
先在数据库建表,例子用的是mysql数据库

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`) 
) 

 

建好表后,生成实体类

packagecom.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 createQuery(final String queryString); 
 public Object save(final Object model); 
 public void update(final Object model); 
 public void delete(final Object model); 
} 

 
  

 

package com.mvc.dao; 
 
 import java.util.List; 
 
 import org.hibernate.Query; 
 import org.springframework.orm.hibernate3.HibernateCallback; 
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
 
 public class EntityDaoImpl extends HibernateDaoSupport implements EntityDao { 
 public List createQuery(final String queryString) { 
 return (List) getHibernateTemplate().execute( 
 new HibernateCallback() { 
 public Object doInHibernate(org.hibernate.Session session) 
 throws org.hibernate.HibernateException { 
 Query query=session.createQuery(queryString); 
 List rows=query.list(); 
 return rows; 
 } 
 }); 
 } 
 public Object save(final Object model) { 
 return getHibernateTemplate().execute( 
 new HibernateCallback() { 
 public Object doInHibernate(org.hibernate.Session session) 
 throws org.hibernate.HibernateException { 
 session.save(model); 
 return null; 
 } 
 }); 
 } 
 public void update(final Object model) { 
 getHibernateTemplate().execute(new HibernateCallback() { 
 public Object doInHibernate(org.hibernate.Session session) 
 throws org.hibernate.HibernateException { 
 session.update(model); 
 return null; 
 } 
 }); 
 } 
 public void delete(final Object model) { 
 getHibernateTemplate().execute(new HibernateCallback() { 
 public Object doInHibernate(org.hibernate.Session session) 
 throws org.hibernate.HibernateException { 
 session.delete(model); 
 return null; 
 } 
 }); 
 } 
} 

 
  


Dao在applicationContext.xml注入

 
  
  


 

Dao只有一个类的实现,直接供其它service层调用,如果你想更换为其它的Dao实现,也只需修改这里的配置就行了。
开始写view页面,WEB-INF/view下新建页面student.jsp,WEB-INF/view这路径是在spring-servlet.xml文件配置的,你可以配置成其它,也可以多个路径。student.jsp代码

<% @ page language="java"contentType="text/html;charset=UTF-8" 
 pageEncoding="UTF-8"%> 
 <% @ include file="/include/head.jsp"%> 
  
  
  
  
  添加