spring mvc + hibernate
来源:互联网 发布:澳洲硕士 知乎 编辑:程序博客网 时间:2024/06/06 15:40
1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>totalDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>totalDispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2. totalDispatcher-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="test" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
<!-- 配置数据源 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 使用注解方式的sessionfacroty -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>test</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--注解方式事务管理-->
<tx:annotation-driven transaction-manager="txManager"/>
<mvc:annotation-driven/>
</beans>
3. BaseDaoImpl
package test.dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {
@Resource
private SessionFactory sessionFactory;
protected Class<T> entityClass;
public BaseDaoImpl(){
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(T entity) {
getSession().merge(entity);
}
public T findById(Integer id) {
return (T) getSession().get(getEntityClass(), id);
}
public List<T> getList(){
return getSession().createQuery("from "+getEntityClass().getName()+" order by id").list();
}
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
public Class<T> getEntityClass(){
return (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
}
4.BookService
package test.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import test.bean.Book;
import test.dao.BookDao;
@Service
@Transactional
public class BookService {
@Resource
private BookDao bookDao;
5.
package test.controller;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;
import test.bean.Book;
import test.service.BookService;
@Controller
public class TestController {
@Resource
private BookService bookService;
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcome(HttpServletRequest request,HttpServletResponse response) {
Book book = (Book) bookService.findById(1);
ModelAndView mav = new ModelAndView();
mav.addObject("book",book.getName());
mav.addObject("name",request.getParameter("name"));
return mav;
}
@RequestMapping(value = "/book/addBook", method = RequestMethod.GET)
public void addBook(ModelMap model) {
List bookList = bookService.getList();
model.addAttribute("bookList",bookList);
}
@RequestMapping(value = "/book/editBook", method = RequestMethod.GET)
public void editBook(int id,ModelMap map) {
Book book = bookService.findById(id);
map.addAttribute("book",book);
}
@RequestMapping(value = "/book/saveBook", method = RequestMethod.POST)
public String saveBook(BookForm book) {
//Book bookBean = new Book();
bookService.save(book);
return "redirect:/book/listBook";
}
@RequestMapping(value = "/book/listBook", method = RequestMethod.GET)
public void listBook(ModelMap model) {
List bookList = bookService.getList();
model.addAttribute("bookList",bookList);
}
}
public void save(Book book) {
bookDao.save(book);
}
public Book findById(Integer id) {
return bookDao.findById(id);
}
public List<Book> getList(){
return bookDao.getList();
}
}
- spring mvc +hibernate 登录
- spring mvc + hibernate
- spring-mvc hibernate配置
- Spring MVC+Hibernate配置
- spring mvc + hibernate 整合
- spring mvc+hibernate乱码
- spring mvc hibernate 随记
- spring mvc + hibernate 分页
- spring mvc集成hibernate
- Spring、Spring MVC、Hibernate相关内容
- Spring mvc+Spring+hibernate整合
- Spring mvc+Spring+hibernate整合
- Spring MVC + Spring + Hibernate集成
- spring+hibernate+mysql mvc 配置
- Spring mvc+hibernate工程搭建
- spring mvc + hibernate(简单登陆)
- spring mvc+hibernate简易配置
- Spring mvc+hibernate+freemarker(实战)
- nginx平滑升级到0.8.53
- 25条有用的SSH命令(ZZ)
- 第一章:快速入门
- REMOTE_ADDR,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR
- 也来发个C#处理JSON的简单代码
- spring mvc + hibernate
- heredoc
- sql语句中的union和union all
- [Linux网络编程]ARP简单实例
- No compatible targets were found....
- PHP 简单修改,删除,(笔记)
- Wince 6.0 中Error: failed PB timebomb checK和PB builder 过期的解决方法!
- 得到手机信息
- centos 设置ssh 私钥访问