开发环境 idea12 + spring3.2.2 + hibernate4.2.2.Final+ junit4.9
项目结构;
数据库结构:
</pre><pre name="code" class="html">CREATE TABLE `ip` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`openId` varchar(30) DEFAULT NULL,
`ip` varchar(30) DEFAULT NULL,
`addtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
1.创建实体类:
package entity;
import javax.persistence.*;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-7-30
* Time: 下午3:02
* To change this template use File | Settings | File Templates.
*/
@Table(name="ip", schema ="", catalog ="ippost")
@Entity
public class IPEntity {
private Integer id;
private String openId;
private String ip;
private Date addTime;
@Column(name = "id", nullable = false, insertable = true, updatable = true, length = 11, precision = 0 )
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name ="openId",nullable = true, insertable = true, updatable = true, length = 30, precision = 0)
@Basic
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
@Column(name = "ip",nullable = true,insertable = true, updatable = true,length = 30,precision = 0)
@Basic
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
@Column(name="addTime", nullable= true,insertable = true, updatable = true,length =20, precision =0)
@Basic
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
}
2.dao层
package dao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-7-30
* Time: 下午3:00
* To change this template use File | Settings | File Templates.
*/
public class BaseDao<T> {
@Resource
private SessionFactory sessionFactory;
private Class<T> entityClass;
@SuppressWarnings({"unchecked", "rawtypes"})
public BaseDao(){
//getGenericSuperclass() 通过反射获取当前类表示的实体(类,接口,基本类型或void)的直接父类的Type,getActualTypeArguments()返回参数数组。
Type genTpye = getClass().getGenericSuperclass();
Type[] param = ((ParameterizedType)genTpye).getActualTypeArguments();
entityClass = (Class) param[0];
}
private Session getSession() {
return sessionFactory.getCurrentSession() == null ? sessionFactory
.openSession() : sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T Load(Serializable id){
return (T) getSession().load(entityClass,id);
}
@SuppressWarnings("unchecked")
public T get(Serializable id){
return (T) getSession().get(entityClass,id);
}
public void save(T entity){
getSession().save(entity);
}
public void update(T entity){
getSession().saveOrUpdate(entity);
}
public void delete(T entity){
getSession().delete(entity);
}
@SuppressWarnings("rawtypes")
public List getBySQLQuery(String sql){
Query query = getSession().createSQLQuery(sql);
return query.list();
}
public Query createQuery(String hql,Object... values){
Assert.hasText(hql);
Query query = getSession().createQuery(hql);
for(int i=0; i<values.length;i++){
query.setParameter(i,values[i]);
}
return query;
}
}
package dao;
import entity.IPEntity;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-1
* Time: 上午10:44
* To change this template use File | Settings | File Templates.
*/
public interface IPDao {
public void saveIP(IPEntity entity );
public void deleteIP(IPEntity entity);
public void updateIP(IPEntity entity);
public IPEntity findById(int id);
}
daoImpl 层
package dao.impl;
import dao.BaseDao;
import dao.IPDao;
import entity.IPEntity;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-1
* Time: 上午11:08
* To change this template use File | Settings | File Templates.
*/
@Repository
public class IPDaoImpl extends BaseDao<IPEntity> implements IPDao {
@Override
public void saveIP(IPEntity entity) {
super.save(entity);
}
@Override
public void deleteIP(IPEntity entity) {
//To change body of implemented methods use File | Settings | File Templates.
super.delete(entity);
}
@Override
public IPEntity findById(int id) {
String hql = "from IPEntity ie where id="+ id;
Query query = createQuery(hql);
return (IPEntity) query.uniqueResult();
}
@Override
public void updateIP(IPEntity entity) {
//To change body of implemented methods use File | Settings | File Templates.
super.update(entity);
}
}
3.service层
package service;
import entity.IPEntity;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-1
* Time: 上午11:13
* To change this template use File | Settings | File Templates.
*/
public interface IPService {
void saveIP(IPEntity entity);
void deleteIP(IPEntity entity);
void updateIP(IPEntity entity);
IPEntity findById(int id);
}
serviceImpl层
package service.impl;
import dao.IPDao;
import entity.IPEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import service.IPService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.annotation.Resource;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-1
* Time: 上午11:19
* To change this template use File | Settings | File Templates.
*/
@Service
@Transactional(propagation = Propagation.REQUIRED)
@TransactionConfiguration(transactionManager = "txManager", defaultRollback=true)
public class IPServiceImpl implements IPService {
@Resource
private IPDao ipDaoImpl;
@Override
public void saveIP(IPEntity entity) {
//To change body of implemented methods use File | Settings | File Templates.
ipDaoImpl.saveIP(entity);
}
@Override
public void deleteIP(IPEntity entity) {
//To change body of implemented methods use File | Settings | File Templates.
ipDaoImpl.deleteIP(entity);
}
@Override
public void updateIP(IPEntity entity) {
//To change body of implemented methods use File | Settings | File Templates.
ipDaoImpl.updateIP(entity);
}
@Override
public IPEntity findById(int id) {
return ipDaoImpl.findById(id);
}
}
4.Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>IPPost</groupId>
<artifactId>IPPost</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>3.2.2.RELEASE</spring.version>
<spring.mvc.version>3.2.2.RELEASE</spring.mvc.version>
<hibernate.version>4.2.2.Final</hibernate.version>
<junit.version>4.9</junit.version>
<!-- spring 3.X 以上请使用junit4.8 以上 -->
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- spring 3.2.2 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!--对Tomcat的连接池的集成 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 对服务器的代理接口 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 为简化JMS API的使用而作的简单封装 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 整合第三方的ORM框架,如hibernate,ibatis,jdo,以及 spring的JPA实现 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring 对Object/XMl的映射支持,可以让Java与XML之间来回切换 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- start spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.mvc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.mvc.version}</version>
</dependency>
<!-- org.aspectj Aop需要-->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.6.11</version >
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.10</version>
</dependency>
<!-- mysql 链接需要-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
<!-- START SERVLET -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- start hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<!-- start XML -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>20040902.021138</version>
</dependency>
</dependencies>
</project>
5.配置文件 使用到了Spring +SpringMVC + hibernate 的配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<!-- 配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 编码过滤 -->
<filter>
<filter-name>Set Character Encoding</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>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>traveldispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/IPPost-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>traveldispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<import resource="IPPost-mvc.xml" />
</beans>
3.IPPost-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 自动扫描controller下所有控制器 -->
<context:component-scan base-package="controller" />
<!-- 启动spring mvc注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven/>
<!-- 对静态文件进行映射 -->
<mvc:resources mapping ="/resources/**" location = "/resources/" cache-period ="3153600"/>
<!-- jsp页面解析 -->
<bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<import resource="IPPost-service.xml"/>
</beans>
4.IPPost-service.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
<!-- service.impl包下所有标注@Service的服务组件 -->
<context:component-scan base-package="service.impl"/>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<!--使用强大的切点表达式语言轻松定义目标方法-->
<aop:config proxy-target-class="true">
<!--通过aop定义事务增强切面-->
<aop:pointcut id="serviceMethod"
expression="execution(* service.impl.*Service.*(..))" />
<!--引用事务增强-->
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<!--事务增强-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!--事务属性定义-->
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 数据访问层配置 -->
<import resource="IPPost-dao.xml" />
</beans>
IPPost-dao.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:content="http://www.springframework.org/schema/context"
xmlns:comtent="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 扫描dao.impl包下所有标注@Repository的DAO组件 -->
<content:component-scan base-package="dao.impl" />
<!-- 引入数据库配置文件 -->
<comtent:property-placeholder location="jdbc.properties" />
<bean id = "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="password" value="${jdbc.password}" />
<property name="username" value="${jdbc.username}" />
</bean>
<bean name="sessionFactory" id = "sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<!-- 扫描com.hdu.cash.entity包下所有标注@Etity,@Table的实体类 -->
<!-- 基于JSR 220的JPA注解 -->
<property name="packagesToScan" value="entity"/>
<!-- 数据库方言 -->
<property name="hibernateProperties">
<props>
<prop key="connection.autocommit">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
</props>
</property>
</bean>
</beans>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/IPPost?characterEncoding=UTF-8
jdbc.username=
jdbc.password=
开始单元测试:
1.spring3 以上建议使用junit4.8 以上测试;
2使用maven管理项目时 请将各个配置文件放置于Resources 文件夹下。
package dao.impl;
import dao.IPDao;
import entity.IPEntity;
import org.hibernate.SessionFactory;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import service.IPService;
import javax.annotation.Resource;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-1
* Time: 上午11:31
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
@Transactional
@TransactionConfiguration(transactionManager = "txManager")
public class IPDaoImplTest {
@Resource
private IPService ipServiceImpl;
@Resource
private IPDao ipDaoImpl;
@org.junit.Test
@Rollback(true)
public void testSaveIP() throws Exception {
IPEntity entity = new IPEntity();
entity.setAddTime(new Date());
ipServiceImpl.saveIP(entity);
}
@org.junit.Test
@Rollback(true)
public void testDeleteIP() throws Exception {
IPEntity entity = new IPEntity();
entity.setAddTime(new Date());
entity.setIp("192.168.1.1");
ipDaoImpl.saveIP(entity);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
使用以上注释就可以进行spring 的单元测试 需要提供spring-test.jar(pom.xml 内已经包含)
@Rollback(true)
如果不想污染数据,请使用<span style="font-family: Arial, Helvetica, sans-serif;">@Rollback(true)回滚事务。</span>
在上面的配置上已经能提供Spring mvc 功能,只需要再完成controller层。各个系统的功能不同这部分代码就不展示了。