spring mvc + jpa

最近看到公司一同事在玩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.Driver
jdbc.url=jdbc:mysql://localhost:3306/mvc?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
jdbc.user=root
jdbc.password=123
jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
#jdbc.dialect=org.hibernate.dialect.MySQLInnoDBDialect
jdbc.initialPoolSize=2
jdbc.maxPoolSize=200
jdbc.batch_size=20
jdbc.hbm2ddl.auto=update
jdbc.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.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<!-- 配置占位符 -->
	<bean
		class="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 {

	@Autowired
	private UserService userService;
	
	@Test
	public 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);
	}
	
	@Test
	public 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;
	}
	
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学英语的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值