[color=red][b]个人努力,请不要转载,收藏就好[/b][/color]
本工程为Maven工程,可以下载maven-eclipse插件进行项目导入
如果在eclipse中用WTP插件配置Tomcat运行,需要把maven项目转成Eclipse工程
请下载附件,解压后在工程文件夹运行以下maven命令:
以下是工程的pom.xml文件,用来管理所有依赖的jar包:
以下是web.xml,已经配置了Struts和Spring:
Spring主配置文件applicationContext.xml:
Struts用默认配置,因此不放置struts.xml
Hibernate的Spring配置hibernate.cfg.xml文件如下:
以下这句用来指明,数据事务用Spring的声明式事务,即使用@Transactional注解
数据库的属性配置文件hibernate.properties如下:
logback日志配置文件logback.xml如下:
以上是所有的配置文件。
JSP代码不再贴出,请下载附件。
以下是java程序代码
用于接收请求的Struts的Action类:UserAction.java
处理逻辑的Service:UserService.java
因为Service只是处理增删改查,没有验证数据,所以比较简单,值得注意的是,@Transactional事务注解只能写在Service层,不能写在Action层,不然会出错,估计是对Spring3对Struts2支持的不太好
Hibernate的PO对象:User.java
不再需要用XML进行表和实体类的映射了
数据存储层通用DAO接口定义:GeneralDao.java
DAO的实现类:GeneralDAOImpl.java
这就是整个搭建框架,是一个内核。在应用时,可以配置freemarker之类的其余框架进来,用于实际开发
纯注解形式的代码有助于代码开发和管理,很代码更整洁,测试代码也可以用注解形式,也方便测试。因为注解形式把配置和代码放在一起,维护比较方便,减少了配置和代码的同步,也减少了代码量,使BUG数降低。个人推荐使用注解形式的配置
注解形式的代码,可以算一程契约形式的编程,只关心我想做什么,不关心怎么做,代码的耦合比较小,模块化比较强。JDK7也在主推模块化,这两个方向是以后的发展趋势
本工程为Maven工程,可以下载maven-eclipse插件进行项目导入
如果在eclipse中用WTP插件配置Tomcat运行,需要把maven项目转成Eclipse工程
请下载附件,解压后在工程文件夹运行以下maven命令:
call mvn eclipse:eclipse -Dwtpversion=2.0
以下是工程的pom.xml文件,用来管理所有依赖的jar包:
<?xml version="1.0" encoding="GBK"?>
<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>com.tianlihu.projects</groupId>
<artifactId>S2S3H3_demo</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<springframe.version>3.1.4.RELEASE</springframe.version>
<unitils.version>3.1</unitils.version>
</properties>
<dependencies>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.3.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-struts</artifactId>
<version>${springframe.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.23</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframe.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-core</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-dbunit</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-easymock</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-spring</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-mock</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-inject</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerVersion>1.6</compilerVersion>
<encoding>GBK</encoding>
<fork>true</fork>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>GBK</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
以下是web.xml,已经配置了Struts和Spring:
<?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">
<!-- 加载struts2核心 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 加载spring配置文件applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Spring主配置文件applicationContext.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"
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">
<context:annotation-config />
<context:component-scan base-package="com.tianlihu.projects.ssh" />
<context:property-placeholder location="classpath:hibernate.properties"/>
<import resource="classpath:hibernate.cfg.xml"/>
</beans>
Struts用默认配置,因此不放置struts.xml
Hibernate的Spring配置hibernate.cfg.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="initialPoolSize" value="${hibernate.connection.initialPoolSize}" />
<property name="minPoolSize" value="${hibernate.connection.minPoolSize}" />
<property name="maxPoolSize" value="${hibernate.connection.maxPoolSize}" />
<property name="preferredTestQuery" value="select 1 from dual " />
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.tianlihu.projects.ssh.*.po" />
<property name="useTransactionAwareDataSource" value="true" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">${hibernate.temp.use_jdbc_metadata_defaults}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:aspectj-autoproxy />
<tx:annotation-driven />
</beans>
以下这句用来指明,数据事务用Spring的声明式事务,即使用@Transactional注解
<tx:annotation-driven />
数据库的属性配置文件hibernate.properties如下:
## hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create-drop
hibernate.temp.use_jdbc_metadata_defaults=false
## hibernate cache
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=true
## C3P0 configuration
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.initialPoolSize=1
hibernate.connection.minPoolSize=1
hibernate.connection.maxPoolSize=3
logback日志配置文件logback.xml如下:
<?xml version="1.0" encoding="GBK"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{36} - %msg %n </pattern>
</encoder>
</appender>
<appender name="logfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
logs/ssh.%d{yyyy-MM-dd-HH}.log
</FileNamePattern>
<MaxHistory>10</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
<![CDATA[
%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{36} - %msg %n
]]>
</Pattern>
</layout>
</appender>
<logger name="root" level="ERROR">
<appender-ref ref="stdout" />
<appender-ref ref="logfile" />
</logger>
<logger name="java.sql" level="DEBUG">
<appender-ref ref="stdout" />
<appender-ref ref="logfile" />
</logger>
<logger name="com.tianlihu" level="DEBUG">
<appender-ref ref="stdout" />
<appender-ref ref="logfile" />
</logger>
</configuration>
以上是所有的配置文件。
JSP代码不再贴出,请下载附件。
以下是java程序代码
用于接收请求的Struts的Action类:UserAction.java
package com.tianlihu.projects.ssh.account.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.tianlihu.projects.ssh.account.po.User;
import com.tianlihu.projects.ssh.account.service.UserService;
@ParentPackage("struts-default")
@Namespace("/account")
@Component
public class UserAction {
@Autowired
private UserService userService;
private List<User> users;
private String userId;
private String name;
private String password;
private String age;
private String message;
@Action(value = "createUser", results = { @Result(name = "success", location = "/WEB-INF/userForm.jsp") })
public String createUser() {
message = "创建用户";
return "success";
}
@Action(value = "create", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
public String create() {
User user = getUser();
userService.createUser(user);
message = "创建用户'" + userId + "'成功";
return "success";
}
@Action(value = "updateUser", results = { @Result(name = "success", location = "/WEB-INF/userForm.jsp") })
public String updateUser() {
User user = userService.findByUserId(userId);
name = user.getName();
password = user.getPassword();
age = String.valueOf(user.getAge());
message = "修改用户'" + userId + "'";
return "success";
}
@Action(value = "update", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
public String update() {
User user = getUser();
userService.updateUser(user);
message = "修改用户'" + userId + "'成功";
return "success";
}
@Action(value = "delete", results = { @Result(name = "success", type = "redirect", params = { "encode", "true" }, location = "fetchUsers?message=${message}") })
public String deleteUser() {
userService.deleteUser(userId);
message = "删除用户'" + userId + "'成功";
return "success";
}
@Action(value = "fetchUsers", results = { @Result(name = "success", location = "/WEB-INF/users.jsp") })
public String fetchUsers() {
users = userService.findAll();
return "success";
}
private User getUser() {
User user = new User();
user.setUserId(userId);
user.setName(name);
user.setPassword(password);
user.setAge(Integer.parseInt(age));
return user;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
处理逻辑的Service:UserService.java
因为Service只是处理增删改查,没有验证数据,所以比较简单,值得注意的是,@Transactional事务注解只能写在Service层,不能写在Action层,不然会出错,估计是对Spring3对Struts2支持的不太好
package com.tianlihu.projects.ssh.account.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tianlihu.projects.ssh.account.dao.GeneralDao;
import com.tianlihu.projects.ssh.account.po.User;
@Service
public class UserService {
@Autowired
private GeneralDao generalDao;
public User findByUserId(String userId) {
return generalDao.findById(User.class, userId);
}
public List<User> findAll() {
return generalDao.findAll(User.class);
}
@Transactional
public void createUser(User user) {
generalDao.save(user);
}
@Transactional
public void updateUser(User user) {
generalDao.update(user);
}
@Transactional
public void deleteUser(String userId) {
generalDao.deleteById(User.class, userId);
}
@Transactional
public void deleteUser(User user) {
generalDao.delete(user);
}
}
Hibernate的PO对象:User.java
不再需要用XML进行表和实体类的映射了
package com.tianlihu.projects.ssh.account.po;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "AC_USER")
public class User implements Serializable {
private static final long serialVersionUID = -1376216905288707683L;
@Id
@Column(name = "USER_ID", length =50, nullable = false)
private String userId;
@Column(name = "NAME", length = 50, nullable = false)
private String name;
@Column(name = "PASSWORD", length = 50, nullable = false)
private String password;
@Column(name = "AGE", length = 20, nullable = false)
private int age;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
数据存储层通用DAO接口定义:GeneralDao.java
package com.tianlihu.projects.ssh.account.dao;
import java.io.Serializable;
import java.util.List;
public interface GeneralDao {
public <T> T findById(Class<T> type, Serializable id);
public <T> List<T> findAll(Class<T> type);
public void save(Object... entities);
public void update(Object... entities);
public void saveOrUpdate(Object entity);
public void delete(Object... entities);
public void deleteById(Class<?> type, Serializable id);
public void refresh(Object... entities);
public void flush();
}
DAO的实现类:GeneralDAOImpl.java
package com.tianlihu.projects.ssh.account.dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class GeneralDAOImpl implements GeneralDao {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public <T> T findById(Class<T> type, Serializable id) {
return hibernateTemplate.get(type, id);
}
@Override
public <T> List<T> findAll(Class<T> type) {
return hibernateTemplate.loadAll(type);
}
@Override
public void save(Object... entities) {
for (Object entity : entities) {
hibernateTemplate.save(entity);
}
}
@Override
public void saveOrUpdate(Object entity) {
hibernateTemplate.saveOrUpdate(entity);
}
@Override
public void update(Object... entities) {
for (Object entity : entities) {
hibernateTemplate.update(entity);
}
}
@Override
public void delete(Object... entities) {
for (Object entity : entities) {
if (entity != null) {
hibernateTemplate.delete(entity);
}
}
}
@Override
public void deleteById(Class<?> type, Serializable id) {
if (id == null) {
return;
}
Object entity = findById(type, id);
if (entity == null) {
return;
}
delete(entity);
}
@Override
public void refresh(Object... entities) {
for (Object entity : entities) {
hibernateTemplate.refresh(entity);
}
}
@Override
public void flush() {
hibernateTemplate.flush();
}
}
这就是整个搭建框架,是一个内核。在应用时,可以配置freemarker之类的其余框架进来,用于实际开发
纯注解形式的代码有助于代码开发和管理,很代码更整洁,测试代码也可以用注解形式,也方便测试。因为注解形式把配置和代码放在一起,维护比较方便,减少了配置和代码的同步,也减少了代码量,使BUG数降低。个人推荐使用注解形式的配置
注解形式的代码,可以算一程契约形式的编程,只关心我想做什么,不关心怎么做,代码的耦合比较小,模块化比较强。JDK7也在主推模块化,这两个方向是以后的发展趋势