SpringMVC+Spring+Hibernate的小例子

Strusts2+Spring+Hibernate虽然是主流的WEB开发框架,但是SpringMVC有越来越多的人使用了,确实也非常好用,用得爽!

这里实现了一个SpringMVC+Spring+Hibernate的小例子。注释都在代码里面。

项目各包的结构如下图:

SpringMVC+Spring+Hibernate的小例子_第1张图片


1, 首先是pom.xml


	4.0.0
	com.miquan
	springmvc_spring_hibernate
	war
	0.0.1-SNAPSHOT
	springmvc_spring_hibernate Maven Webapp
	http://maven.apache.org

	
		
		
			org.springframework
			spring-aspects
			4.0.4.RELEASE
		
		
		
			junit
			junit
			4.11
			test
		
		
		
			org.springframework
			spring-context
			4.1.0.RELEASE
		
		
			org.springframework.ws
			spring-ws-core
			2.2.0.RELEASE
		
		
			org.springframework
			spring-orm
			4.1.1.RELEASE
		
		
		
			org.hibernate
			hibernate-core
			4.3.6.Final
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			mysql
			mysql-connector-java
			5.1.18
		
	

	
		springmvc_spring_hibernate
	


2, web.xml配置好SpringMVC和Spring




	  
      
        org.springframework.web.context.ContextLoaderListener  
    

	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		1
	

	
		springmvc
		/
	


3, SpringMVC的配置——springmvc-servlet.xml(注意命名,对应servlet中的springmvc,命名规则: []-servlet.xml)



        
    
    
	
	
	
	
	
	
	
		
		
	
	

4, spring的配置——applicationContext.xml默认是这个名字,并且系统会到WEB-INF目录下找这个文件。需要自定义名称和目录的自己百度一下。




	
	
	

	
	
		
		
		
		
	

	
	
		
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				
				update
				true
			
		
	

	
	
		
	

	
	
		
	

	
	
	


5, 实体类。

package com.miquan.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;import java.io.Serializable;

@Entity
@Table
public class User impements Serializable {
	private int id;
	private String username;
	private String password;
	
	public User() {
		super();
	}
	public User(int id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + "]";
	}
}

6, controller层。

package com.miquan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.miquan.model.User;
import com.miquan.service.IUserService;

@Controller
@RequestMapping(value="/user")
public class UserController {
	/**
	 * 注入userService。
	 * 如果userService继承了某个接口,
	 * 这里类型必须是接口IUserService,
	 * 不能是类UserService,不知道为什么。
	 */
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/registe", method=RequestMethod.GET)
	public String registe() {
		User user = new User(0, "小马云", "999");
		userService.registe(user);
		return "index";
	}
}

7, service层。

package com.miquan.service;

import com.miquan.model.User;

public interface IUserService {

	public boolean registe(User user);

}

package com.miquan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.miquan.dao.IGeneralDao;
import com.miquan.model.User;

@Service
public class UserService implements IUserService {
	@Autowired
	private IGeneralDao generalDao;
	
	/* 
	 * 这里要有事务注解,默认readOnly=true,不设置的话会报错。
	 * insert和update操作都要。
	 */
	@Transactional(readOnly=false)
	public boolean registe(User user) {
		generalDao.save(user);
		return false;
	}
}

8, dao层。

package com.miquan.dao;

import java.io.Serializable;
import java.util.List;

public interface IGeneralDao {
	public  T findById(Class type, Serializable id);  
	  
    public  List findAll(Class type);  
  
    public void save(Object... entities);  
  
    public void update(Object... entities);  
  
    public void saveOrUpdate(Object entity);  
  
    public void delete(Object... entities);  
  
    public void deleteById(Class type, Serializable id);  
  
    public void refresh(Object... entities);  
  
    public void flush();
}

package com.miquan.dao;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
 * 这个类将dao成封装成了一个操作类,从网上复制过来的。
 */
@Repository
public class GeneralDao implements IGeneralDao {
	/**
	 * 这个bean里面需要注入sessionFactory,所以把这个bean写在了配置中。
	 */
	@Autowired
	private HibernateTemplate hibernateTemplate;

	public  T findById(Class type, Serializable id) {
		return hibernateTemplate.get(type, id);
	}

	public  List findAll(Class type) {
		return hibernateTemplate.loadAll(type);
	}

	public void save(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.save(entity);
		}
	}

	public void saveOrUpdate(Object entity) {
		hibernateTemplate.saveOrUpdate(entity);
	}

	public void update(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.update(entity);
		}
	}

	public void delete(Object... entities) {
		for (Object entity : entities) {
			if (entity != null) {
				hibernateTemplate.delete(entity);
			}
		}
	}

	public void deleteById(Class type, Serializable id) {
		if (id == null) {
			return;
		}
		Object entity = findById(type, id);
		if (entity == null) {
			return;
		}
		delete(entity);
	}

	public void refresh(Object... entities) {
		for (Object entity : entities) {
			hibernateTemplate.refresh(entity);
		}
	}

	public void flush() {
		hibernateTemplate.flush();
	}
}

9, 测试。

package com.miquan.service;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.miquan.controller.UserController;

public class UserServiceTest {
	@Test
	public void test() {
		ApplicationContext ctx = new FileSystemXmlApplicationContext(
				"src/main/webapp/WEB-INF/applicationContext.xml");
		UserController controller = ctx.getBean(UserController.class);
		controller.registe();
	}
}

或者部署在tomcat上,在浏览器访问http://localhost:8080/xx项目/user/registe。

你可能感兴趣的:(WEB开发,springmvc,spring,hibernate,注解,事务)