需要的jar包,包含了spring mvc的jar
<!-- 统一管理 jar 包版本 --> <properties> <version.junit>4.0</version.junit> <version.spring>3.0.6.RELEASE</version.spring> <version.slf4j>1.6.4</version.slf4j> <version.jstl>1.2</version.jstl> <version.jackson>1.8.4</version.jackson> <version.servlet-api>2.5</version.servlet-api> <version.c3p0>0.9.1.2</version.c3p0> <version.mysql>5.1.21</version.mysql> <version.javassist>3.12.1.GA</version.javassist> <version.commons-dbcp>1.4</version.commons-dbcp> <!-- 轻易不要升级这两个jar --> <version.hibernate>3.5.1-Final</version.hibernate> <version.hibernate-annotations>3.5.1-Final</version.hibernate-annotations> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${version.junit}</version> <scope>test</scope> </dependency> <!-- org.slf4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${version.slf4j}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${version.jstl}</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${version.servlet-api}</version> <scope>provided</scope> </dependency> <!-- spring return json --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>${version.jackson}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>${version.jackson}</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${version.hibernate}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>${version.hibernate-annotations}</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>${version.c3p0}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${version.mysql}</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>${version.javassist}</version> </dependency> <!-- **************************** spring *************************************** --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${version.commons-dbcp}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${version.spring}</version> <scope>test</scope> </dependency> </dependencies>
hibernate.dialect=org.hibernate.dialect.MySQLDialect jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/springmvc jdbc.username=root jdbc.password=root
<?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:p="http://www.springframework.org/schema/p" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <context:property-placeholder location="classpath*:config/jdbc.properties" /> <bean id="springmvcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="properties"> <props> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> <prop key="c3p0.initialPoolSize">10</prop> <prop key="c3p0.minPoolSize">10</prop> <prop key="c3p0.autoCommitOnClose">false</prop> <prop key="hc3p0.maxPoolSize">30</prop> <prop key="hc3p0.timeout">120</prop> <prop key="c3p0.maxIdleTime">18000</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="c3p0.testConnectionOnCheckout">true</prop> <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop> </props> </property> </bean> <!-- org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <bean id="springmvcSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="springmvcDataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagesToScan" value="com.longxia.**.model" /> </bean> <bean id="springmvcHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="springmvcSessionFactory" /> </bean> <bean id="jdbcSpringmvcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="springmvcDataSource" /> </beans>
将applicationDataSource.xml 加入的web.xm中
<!-- 设定配置文件列表 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> <!-- classpath*: 指定编译后的class目录 --> classpath*:config/application*.xml </param-value> </context-param>
package com.longxia.springmvc.model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Version; import org.hibernate.annotations.OptimisticLockType; /** * @author xialong<[email protected]> * */ @Entity @Table(name="t_user") @org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.VERSION) public class User extends BaseModel { private static final long serialVersionUID = 2358420388745275538L; private Integer id; private String userName; private String password; private Date version; @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.AUTO) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="user_name",length=50) public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Column(name="password",length=50) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Version @Column(name="version") public Date getVersion() { return version; } public void setVersion(Date version) { this.version = version; } }
/** * */ package com.longxia.springmvc.dao.userdao; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.stereotype.Component; import com.longxia.springmvc.dao.HibernateBaseDao; import com.longxia.springmvc.model.User; /** * @author xialong * */ @Component public class UserDaoImpl extends HibernateBaseDao<User> implements IUserDao { @Resource private SessionFactory springmvcSessionFactory; @PostConstruct public void initSessionFactory() { super.setSessionFactory(springmvcSessionFactory); } /* (non-Javadoc) * @see com.longxia.springmvc.dao.userdao.IUserDao#findAllUsers() */ @Override public List<User> findAllUsers() { String hql = "from User"; return this.getHibernateTemplate().find(hql); } }