spring mvc + jpa
来源:互联网 发布:java filelist 编辑:程序博客网 时间:2024/06/06 05:22
最近看到公司一同事在玩spring mvc,知道spring mvc已经出了很久了,空闲之余本人就整合了一下springmvc + jpa,其中jpa的实现还是Hibernate
现在注解是大行其道。
让我们回顾一下:
1、以前的servlet一定要在web.xml里配置每一个servlet,现在只要在servlet类上面配置下注解就可以。
2、struts的每一个Action请求要在struts.xml文件配置,不过也可以通过注解来配置每一个action请求@Repository("userDao")
3、在j2ee中每一个dao 或 service的bean要在spring.xml,其实也可能用注解来配置 @Repository("userDao") || @Service("userService")
4、spring mvc 在处理请求的时候是用Controller来实现,它可以在spring配置文件里去配置请求,用注解也不在话下了@Controller@RequestMapping("/user.do")
现在就来讲解下整合步骤:
step 1:配置下web.xml文件
在web.xml配置下将请求转交给spring去处理,
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
step 2:在WEB-INF里配置个dispatcher-servlet.xml
//下面是声明你可以用注解来配置controller,这样我们以后用注解来配置每一个请求就好了,不用在spring配置文件去配置每一个请求
<mvc:annotation-driven />
<context:component-scan base-package="com.kwok.springmvc.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
step3:请求这一步骤讲解了,接下来就配置jpa用hibernate实现怎么整合spring, 然后再junit测试下
配置Hibernate的数据库连接时,一般的工作流程都是先配置dataSource,再配置sessionFactory,然后配置事件管理,这个就不做详细的解释
下面是db.properties文件
jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mvc?useUnicode=true&characterEncoding=utf-8&autoReconnect=truejdbc.user=rootjdbc.password=123jdbc.dialect=org.hibernate.dialect.MySQL5Dialect#jdbc.dialect=org.hibernate.dialect.MySQLInnoDBDialectjdbc.initialPoolSize=2jdbc.maxPoolSize=200jdbc.batch_size=20jdbc.hbm2ddl.auto=updatejdbc.query.substitutions=true 1, false 0, yes ''Y'', no ''N''
Hibernate 在spring中的配置
<?xml version="1.0" encoding="UTF-8"?> <!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> --><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" 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"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- 配置占位符 --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:db.properties" /></bean><!-- 数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" abstract="false" lazy-init="default" autowire="default"><property name="driverClassName" value="${jdbc.driverClass}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean><!-- 配置一个sessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="hibernateProperties"><props><prop key="hibernate.dialect">${jdbc.dialect}</prop><prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop><prop key="hibernate.jdbc.batch_size">${jdbc.batch_size}</prop><prop key="hibernate.cache.use_second_level_cache">false</prop><prop key="hibernate.query.substitutions">${jdbc.query.substitutions}</prop><prop key="hibernate.show_sql">false</prop><prop key="hibernate.format_sql">true</prop></props></property><property name="packagesToScan"><list><value>com.kwok.springmvc.module.entity</value></list></property></bean><context:annotation-config/><context:component-scan base-package="com.kwok.springmvc" /><bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <property name="dataSource" ref="dataSource"/> </bean><tx:annotation-driven transaction-manager="transactionManager" /><!-- hibernateTemplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean></beans>
step4:接下来都是Entity,Controller,Service,Dao之间的注解来完成业务逻辑,下面是每一个层的注解啦
@Entity
@Table(name="mvc_user")
@Controller
@RequestMapping("/user.do")
@Service("userService")
@Repository("userDao")
step4:Junit4的单元测试
package com.kwok.springmvc.junit;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.transaction.TransactionConfiguration;import com.kwok.springmvc.module.entity.User;import com.kwok.springmvc.service.UserService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"/applicationContext.xml"})@TransactionConfiguration(transactionManager="myTxManager",defaultRollback=false)public class ServiceJunit {@Autowiredprivate UserService userService;@Testpublic void testInsert(){User user = new User();user.setName("wfung_kwok2");user.setPassword("wfung_kwok");user.setEmail("wfung_kwok@123.com");user.setStatus(true);userService.insert(user);}@Testpublic void testGet(){/*User user = userService.get((long)2);System.out.println(user.getName());List<User> users = getUserService().findAllByCondition("1=1", null, 0, 0);for(User u : users){System.out.println(u.getId());}user.setPassword("!@#$%");userService.update(user);*/Object o = userService.queryObjcet("select count(*) from User where 1=1", null);System.out.println((Long)o);}public void setUserService(UserService userService) {this.userService = userService;}}
- spring mvc + jpa
- spring mvc + jpa 学习
- spring mvc jpa实现
- GWT + Spring MVC + JPA整合
- spring mvc + jpa 实现 ssh
- spring-MVC+hibernate+mybatis+jpa
- spring-mvc-jpa环境搭建
- spring mvc 集成 spring data jpa
- spring mvc之jpa的配置
- spring mvc + jpa 事务无法提交
- Spring mvc + maven +mybatis+jpa 架构解析
- spring boot jsp mvc jpa mysql
- Spring+mvc+jpa+hibernate一步一步实现
- idea 搭建maven+spring mvc+jpa框架
- 基于Spring MVC+Spring JPA技术使用的视频下载
- 玩转spring MVC(九)---Spring Data JPA
- Spring mvc中使用Spring Data Jpa 连接Mysql数据库
- Spring Data Jpa 实现分页(Spring MVC+easyui)
- 关于SqmUpload任务
- webkitgtk简单示例
- Dropbox首位员工辞职 将与另一个同事创业?
- beginthread和beginthreadex的使用方法
- linux下svn服务器端安装与使用
- spring mvc + jpa
- Power Management
- vim命令(持续更新)
- Astyle使用说明
- 对大学 IT 专业教育的反馈
- Sensors
- Radio Layer Interfac
- AIR 程序多开
- 技能的反面 - 魔方和模仿