- 浏览: 312575 次
- 性别:
- 来自: 宁波
文章分类
最新评论
-
bqlin1987:
请问是不是ITeye的代码莫名的换行了?
学习SSO-1 Tomcat启用ssl -
a6821122:
在data-config.xml 里的javascript 不 ...
solr导入Oracle日期时间数据的处理 -
jie_kong:
是flexigrid不是flexgrid
FlexGrid自己用的经验 -
zjnbshifox:
呵呵,我也在摸索阶段,谢谢提醒ligywin 写道还需要补充以 ...
CAS自定义Credentials登录 -
ligywin:
还需要补充以下方法import org.apache.comm ...
CAS自定义Credentials登录
web.xml
app-config.xml[Spring的数据访问配置文件]
mvc-config.xml [spring mvc 配置文件]
DAO接口[]
dao实现
实际的DAO以及其接口
Service层
spring的controller写法
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>helpu</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/app-config.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/mvc-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
app-config.xml[Spring的数据访问配置文件]
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property> <property name="url"><value>jdbc:mysql://localhost/helpu?useUnicode=true&characterEncoding=utf-8</value></property> <property name="username"><value>root</value></property> <property name="password"><value></value></property> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"><ref local="dataSource"/></property> <property name="packagesToScan" value="com.helpu" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
mvc-config.xml [spring mvc 配置文件]
<?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:mvc="http://www.springframework.org/schema/mvc" 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/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"> <!-- Scans the classpath of this application for @Components to deploy as beans --> <context:component-scan base-package="com.helpu" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- Configures the @Controller programming model --> <mvc:annotation-driven /> <!-- misc --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="suffix" value=".jsp"/> </bean> <!-- Configures Hibernate - Database Config <import resource="db-config.xml" />--> </beans>
DAO接口[]
package com.helpu.dao; import java.util.List; /** * * @author apple * @description * @param <T> */ public interface IDAO<T> { T get(int id); T add(T obj); boolean delete(T obj); T update(T obj) throws Exception; List<T> query(String hql,Object... params); int count(); int count(String hql,Object...params); List<T> query(String hql,int page,int pagesize,Object... params); }
dao实现
package com.helpu.dao.impl; import java.lang.reflect.ParameterizedType; import java.sql.SQLException; import java.util.List; import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.annotations.FlushModeType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.HibernateAccessor; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.HibernateTemplate; import com.helpu.dao.IDAO; public abstract class DAOImpl<T> implements IDAO<T> { protected HibernateTemplate hibernateTemplate; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { hibernateTemplate = new HibernateTemplate(sessionFactory); //hibernateTemplate.setFlushMode(HibernateAccessor.FLUSH_AUTO); } public T add(T obj) { hibernateTemplate.save(obj); return obj; } public boolean delete(T obj) { try{ hibernateTemplate.delete(obj); return true; }catch (Exception e) { return false; } } @SuppressWarnings("unchecked") public T get(int id) { Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; return hibernateTemplate.get(clazz, id); } public T update(T obj) throws Exception { hibernateTemplate.update(obj); return obj; } @SuppressWarnings("unchecked") @Override public List<T> query(String hql, Object... params) { return (List<T>) hibernateTemplate.find(hql, params); } @SuppressWarnings("unchecked") @Override public int count() { Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; String hql = "select count(*) from " + clazz.getName(); return count(hql); } @Override @SuppressWarnings("rawtypes") public int count(String hql,Object...params) { List result = hibernateTemplate.find(hql,params); return ((Long) result.get(0)).intValue(); } @Override public List<T> query(final String hql, final int page, final int pagesize, final Object... params) { @SuppressWarnings("unchecked") List<T> result = hibernateTemplate.executeFind(new HibernateCallback<List<T>>() { @Override public List<T> doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.createQuery(hql); q.setFirstResult(page*pagesize-pagesize); q.setMaxResults(pagesize); int i=0; for (Object o : params) { q.setParameter(i, o); i++; } return q.list(); } }); return result; } }
实际的DAO以及其接口
package com.helpu.dao; import com.helpu.model.Category; public interface ICategoryDAO extends IDAO<Category> { } package com.helpu.dao.impl; import org.springframework.stereotype.Repository; import com.helpu.dao.ICategoryDAO; import com.helpu.model.Category; @Repository public class CategoryDAO extends DAOImpl<Category> implements ICategoryDAO { }
Service层
package com.helpu.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.helpu.dao.ICategoryDAO; import com.helpu.helper.JsonDataWrapper; import com.helpu.model.Category; @Service public class CategoryService { @Autowired() @Qualifier("categoryDAO") private ICategoryDAO categoryDAO; public void setCategoryDAO(ICategoryDAO categoryDAO) { this.categoryDAO = categoryDAO; } @Transactional(readOnly=true) public JsonDataWrapper<Category> pages(int page,int pagesize){ int total = categoryDAO.count(); List<Category> rows = categoryDAO.query("from Category", page, pagesize); JsonDataWrapper<Category> categories = new JsonDataWrapper<Category>(page, total, rows); return categories; } @Transactional public void update(Category c) throws Exception { this.categoryDAO.update(c); } @Transactional public void save(Category c){ this.categoryDAO.add(c); } @Transactional public void delete(int id) { this.categoryDAO.delete(this.categoryDAO.get(id)); } @Transactional(readOnly=true) public Category getCategory(int id) { return this.categoryDAO.get(id); } @Transactional(readOnly=true) public List<Category> all() { return this.categoryDAO.query("from Category"); } }
spring的controller写法
package com.helpu.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.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.helpu.model.Category; import com.helpu.service.CategoryService; @Controller @RequestMapping("/category") public class SoftwareCategoryController { @RequestMapping("/show.do") public @ResponseBody Category show(@RequestParam int id){ return this.categoryService.getCategory(id); } public void setCategoryService(CategoryService categoryService) { this.categoryService = categoryService; } @Autowired private CategoryService categoryService; }
发表评论
-
jasper report与Spring mvc整合
2015-10-19 16:02 1598配置jasper view resolver <be ... -
solr导入Oracle日期时间数据的处理
2013-02-08 10:01 5100参考文章: http://stackoverflow.com/ ... -
solr服务端加亮设置
2013-02-06 23:25 1274在${solr.home}/conf/solrconfig.x ... -
Solr客户端自定义开发
2013-02-06 16:52 1615@Service @Qualifier(" ... -
CAS 客户端获取Credentials额外信息
2013-02-06 09:40 3936服务端的配置 1、在deployerContext.xml中加 ... -
CAS自定义Credentials登录
2013-02-02 00:03 7941先看了这篇文章http://www.blogjava.net/ ... -
Spring data MongoDB DSL
2013-01-29 13:38 2561这两天自己配置了一个通过Spring Data来连接Mongo ... -
Spring MVC和freemarker配置
2013-01-21 13:45 2435参考文章:http://www.cnblogs.com/hoo ... -
我自己的maven环境
2013-01-17 14:37 1066今天把自己的maven环境记录下来,准备重装系统能找到 mav ... -
Spring-data jpa学习
2013-01-17 14:25 1771一个早上加一个中午就耗在这里了啊,就一个小问题,让我吐血不已, ... -
今天学会的DetachedCriteria用法
2013-01-12 22:01 10139以前用hibernate,一般都是使用hql语句的,因为和sq ... -
学习SSO-1 Tomcat启用ssl
2012-11-29 10:41 6945根据网上的http://www.kafeitu.me/sso/ ... -
Solr连接数据库
2012-04-10 09:40 6182要建立自己的全文检索 ... -
Solr试用小记
2012-04-10 09:15 15371、下载tomcat7、solr 3.5.0 以及IKAnal ... -
java生成二维码,qrcode
2012-03-22 15:41 6987通过barcode4j生成 @Controller @R ... -
使用HttpClient 4.1.2调用webservice
2012-01-19 14:22 19137下载httpclient,把压缩包里lib目录的所有jar放到 ... -
Spring MVC3 Hibernate3 Annotation 补充
2011-05-30 11:59 1369通过配置文件进行事务声明 app-config.xml & ... -
Tomcat Mod_jk负载均衡Session复制的几点补充
2010-02-24 16:49 1311tomcat和mod_jk进行负载均衡的时候,如果要实现Ses ... -
第一次实际应用Spring MVC
2009-11-17 08:37 1561要做一个简单的投票的东西,看spring mvc的文章已经很久 ... -
我的ant脚本
2009-07-27 15:40 1288<?xml version="1.0&qu ...
相关推荐
Spring MVC 整合Hibernate的一个小小的用例,适合初学Spring MVC 的学者。
Spring4 MVC Hibernate4集成,使用Annotation,封装dao层和service层。 环境: spring 4.0.3.RELEASE hibernate 4.3.5.Final mysql 5.1.29
SpringMVC+Hibernate+annotation+Easyui权限管理系统,Session过期控制(包括Ajax请求的过期控制),访问权限控制(权限控制到按钮),hibernate数据库用户名与密码加密,其它的不说了,绝对物有所值
springmvc + hibernate annotation 配置
下载后请修改数据库用户名和密码,即springAnnotation-hibernate.xml文件 案例系统支持功能如下: 注册功能,身份证,邮箱,手机号,姓名等校验,ajax异步获取资源 分页显示,编辑更新,多行删除等更功能,使用了MVC...
一、 XML,使用tx标签配置拦截器实现事务 一、 Annotation方式 以下所使用环境为Spring4.0.3、Hibernate4.3.5
18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 19. <property name="dataSource" ref="dataSource" /> 20. <property name="hibernateProperties"> 21. <props> 22...
SSH开发注解大全,涉及到的注解内容涵盖AspectJ,Batch,Spring,Cache,Data-Jpa,Integration,JMX,Roo,Security,Test,MVC,WS,JSR(168,222,224,250,299,303,314,317,330,220),Hibernate 3
此项目是struts2 + hibernate3(annotation) + spring(annotation)整合后的项目雏形,基于MVC的设计思想,很适合初学ssh框架的童鞋参考
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
Chapter 6- Annotation-driven development with Spring Chapter 7 - Database interaction using Spring Chapter 8 - Messaging, emailing, asynchronous method execution, and caching using Spring Chapter 9 - ...
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <value>com.org.entity</value> <prop key="hibernate....
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot ...
Spring MVC 3.x 之中也大力支持 JSR-303,可以在控制器中对表单提交的数据方便地验证。 该压缩包包含了hibernate-validator-5.2.4.Final.jar、hibernate-validator-annotation-processor-5.2.4.Final.jar、...
2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...
SpringMVC JSR 数据校验完整jar包下载,一共六个jar包,classmate-1.0.0.jar,hibernate-validator-5.0.0.final.jar,hibernate-validator-annotation-processor-5.0.1.final.jar,hibernate-validator-cdi-5.0.0....
Building on familiar technologies such as JDBC, Hibernate, JPA, Spring Security, Spring MVC, and Swing, you'll apply AOP to common problems encountered in enterprise applications. This book requires...
2.5.1. Spring MVC合理的默认值 2.5.2. Portlet 框架 2.5.3. 基于Annotation的控制器 2.5.4. Spring MVC的表单标签库 2.5.5. 对Tiles 2 支持 2.5.6. 对JSF 1.2支持 2.5.7. JAX-WS支持 2.6. 其他 2.6.1. 动态...