标签:









<?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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 从类路径下加载Spring配置文件,classpath关键字特指从类路径下加载 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/config/spring-config/applicationContext-*.xml</param-value></context-param><!-- 负责启动Spring容器的监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置Spring MVC --><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/config/spring-config/springmvc-servlet.xml</param-value></init-param><load-on-startup>3</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping></web-app>

#数据库相关配置#hibernate.connection.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:dbnamejdbc.connection.url=jdbc:mysql://localhost:3306/sampledb#jdbc.connection.driver_class=oracle.jdbc.driver.OracleDriverjdbc.connection.driver_class=com.mysql.jdbc.Driverjdbc.connection.username=rootjdbc.connection.password=qaz#Hibernate相关配置#hibernate.dialect=org.hibernate.dialect.OracleDialect#hibernate.dialect=org.hibernate.dialect.SQLServerDialecthibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.hbm2dll.auto=updatehibernate.show_sql=true




<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!--方式1:指定配置文件存放位置 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:/config/database.properties</value></list></property></bean><!--方式2:指定配置文件存放位置 --><!-- <context:property-placeholder location="classpath:/config/database.properties" />--><!-- 方式1:配置数据源,采用c3p0数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="${jdbc.connection.driver_class}" /><property name="jdbcUrl" value="${jdbc.connection.url}" /><property name="user" value="${jdbc.connection.username}" /><property name="password" value="${jdbc.connection.password}" /><property name="minPoolSize" value="2" /><property name="maxPoolSize" value="20" /><property name="maxIdleTime" value="1800" /><property name="acquireIncrement" value="2" /><property name="maxStatements" value="0" /><property name="initialPoolSize" value="3" /><property name="idleConnectionTestPeriod" value="1800" /><property name="acquireRetryAttempts" value="4" /><property name="breakAfterAcquireFailure" value="false" /><property name="testConnectionOnCheckout" value="false" /></bean><!-- 方式2:配置数据源,采用BasicDataSource数据源 --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close" p:driverClassName="${hibernate.connection.driver_class}"p:url="${hibernate.connection.url}" p:username="${hibernate.connection.username}"p:password="${hibernate.connection.password}" /> --><!-- jdbc事务管理器 --><bean id="txManagerJDBC"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref local="dataSource" /></property></bean><!--事务模板 --><bean id="transactionTemplate"class="org.springframework.transaction.support.TransactionTemplate"><property name="transactionManager"><ref local="txManagerJDBC" /></property></bean></beans>




<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.hbm2dll.auto">${hibernate.hbm2dll.auto}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop></props></property><!-- 配置实体类 即相当于配置了 映射文件 --><!-- <property name="annotatedClasses"> <list> <value>com.ll.spring.orm.Cat</value></list> </property> --><!-- ORM映射文件存储位置 --><property name="mappingDirectoryLocations"><list><value>classpath:/config/hibernate-config</value></list></property></bean><!-- 配置Hibernate事务管理器 --><bean id="txManager_Hibernate"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:annotation-driven transaction-manager="txManager_Hibernate"/><!-- 配置事务异常封装 --><bean id="persistenceExceptionTranslationPostProcessor"class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /><!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager --><tx:advice id="txAdvice" transaction-manager="txManager_Hibernate"><tx:attributes><tx:method name="save*" propagation="REQUIRED" /><tx:method name="read*" read-only="true" /><tx:method name="list*" read-only="true" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="*" propagation="REQUIRED" /></tx:attributes></tx:advice><!-- aop配置 --><aop:config ><aop:pointcut id="txPointcut" expression="execution(* com.ll.service..*.*(..))" /><aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" /></aop:config></beans>

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 扫描web包,应用Spring的注解 --><context:component-scan base-package="com.ll.web" /><!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/jsp/"p:suffix=".jsp" /></beans>

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --><context:component-scan base-package="com.ll.dao" /><context:component-scan base-package="com.ll.service" /><bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"p:sessionFactory-ref="sessionFactory" /><!-- 配置Jdbc模板 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"p:dataSource-ref="dataSource" /></beans>


<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.ll.domain.User" table="t_user"><id name="userId" type="java.lang.Integer"><column name="user_id" /><generator class="increment" /></id><property name="userName" type="java.lang.String"><column name="user_name" length="60"/></property><property name="password" type="java.lang.String"><column name="password" length="60"/></property><property name="credits" type="java.lang.Integer"><column name="credits"/></property><property name="lastIp" type="java.lang.String"><column name="last_ip" length="32" /></property><property name="lastVisit" type="java.util.Date"><column name="last_visit" length="23" /></property></class></hibernate-mapping>

package com.ll.domain;import java.io.Serializable;import java.util.Date;@SuppressWarnings("serial")public class User implements Serializable {private int userId;private String userName;private String password;private int credits;private String lastIp;private Date lastVisit;public String getLastIp() {return lastIp;}public void setLastIp(String lastIp) {this.lastIp = lastIp;}public Date getLastVisit() {return lastVisit;}public void setLastVisit(Date lastVisit) {this.lastVisit = lastVisit;}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getCredits() {return credits;}public void setCredits(int credits) {this.credits = credits;}}




<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html><head><title>论坛登录</title></head><body><c:if test="${!empty error}"><font color="red"><c:out value="${error}" /></font></c:if><form action="<c:url value="test.action"/>" method="post">用户名:<input type="text" name="userName"><br>密 码:<input type="password" name="password"><br><input type="submit" value="登录" /><input type="reset" value="重置" /></form></body></html>



<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>宝宝淘论坛</title></head><body>${user.userName},欢迎您进入宝宝淘论坛,您当前积分为${user.credits};</body></html>






package com.ll.web;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.ll.domain.User;import com.ll.service.IUserService;import com.ll.service.UserService;@Controllerpublic class LoginController {@Autowiredprivate IUserService userService;@RequestMapping(value = "/index.action")public String loginPage() {return "login";}@RequestMapping(value = "/test.action")public String test(HttpServletRequest request, LoginCommand loginCommand) {System.out.println("用户名:" + loginCommand.getUserName() + "--密码:"+ loginCommand.getPassword());User user = new User();user.setUserName(loginCommand.getUserName());user.setPassword(loginCommand.getPassword());userService.save(user);request.getSession().setAttribute("user", user);return "main";}}
package com.ll.web;public class LoginCommand {private String userName;private String password;public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}}
package com.ll.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.ll.dao.IUserDao;import com.ll.dao.UserDao;import com.ll.domain.User;@Servicepublic class UserService implements IUserService{@Autowiredprivate IUserDao userDao;public void save(User user){userDao.save(user);}}
package com.ll.service;import com.ll.domain.User;public interface IUserService {public void save(User user);}
package com.ll.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.orm.hibernate4.HibernateTemplate;import org.springframework.stereotype.Repository;import com.ll.domain.User;@Repositorypublic class UserDao implements IUserDao{@Autowiredprivate HibernateTemplate hibernateTemplate;@Autowiredprivate JdbcTemplate jdbcTemplate;public void save(User user) {// 测试jdbcTemplate操作String sqlStr = " SELECT count(*) FROM t_user ";int count = jdbcTemplate.queryForInt(sqlStr);System.out.println("jdbcTemplate-用户个数:" + count);//测试hibernateTemplate读写操作try {System.out.println("hibernateTemplate-用户个数: " + hibernateTemplate.find("from User").size());hibernateTemplate.save(user);} catch (Exception e) {System.out.println("==》Hibernate保存数据错误...");System.out.println(e.getMessage());}}}
package com.ll.dao;import com.ll.domain.User;public interface IUserDao {public void save(User user);}
package com.ll.domain;import java.io.Serializable;import java.util.Date;@SuppressWarnings("serial")public class User implements Serializable {private int userId;private String userName;private String password;private int credits;private String lastIp;private Date lastVisit;public String getLastIp() {return lastIp;}public void setLastIp(String lastIp) {this.lastIp = lastIp;}public Date getLastVisit() {return lastVisit;}public void setLastVisit(Date lastVisit) {this.lastVisit = lastVisit;}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getCredits() {return credits;}public void setCredits(int credits) {this.credits = credits;}}
【Spring实战-2】Spring4.0.4整合Hibernate4.3.6
标签:
原文地址:http://www.cnblogs.com/ssslinppp/p/4538035.html