代码框架:
本工程的代码了。。jar包就不在这里展示了。 从官网上下载都可以!
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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springMVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SSH-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
UserController: public class UserController implements Controller{ @Resource(name="Userservice") private IUserservice userservice; public IUserservice getUserservice() { return userservice; } public void setUserservice(IUserservice userservice) { this.userservice = userservice; } @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.println("begin"); User user = new User(); user.setUsername("tdd"); user.setPassword("123"); this.getUserservice().addUser(user); User u = this.getUserservice().getUser(user.getUserid()); return new ModelAndView("/welcome","result",u.getUsername()); } }
IUserDAO: public interface IUserDAO { public void saveUser(User user); public User getUser(int id); }
UserDAO: public class UserDAO implements IUserDAO{ private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void saveUser(User user) { // TODO Auto-generated method stub this.getSessionFactory().getCurrentSession().save(user); } @Override public User getUser(int id) { // TODO Auto-generated method stub String hql = "from User u where u.userid=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql);//此时使用的是hql语句 query.setInteger(0, id); return (User) query.uniqueResult(); }
@Entity @Table(name = "T_USER") public class User { private int userid; private String username; private String password; @GenericGenerator(name = "generator", strategy = "increment") @Id @GeneratedValue(generator = "generator") @Column(name = "user_id", unique = true, nullable = false) public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } @Column(name = "user_name", length = 10) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(name = "user_password", length = 30) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
IUserservice : public interface IUserservice { public void addUser(User user); public User getUser(int id); }
Userservice : public class Userservice implements IUserservice{ private IUserDAO userDAO; public IUserDAO getUserDAO() { return userDAO; } public void setUserDAO(IUserDAO userDAO) { this.userDAO = userDAO; } @Override public void addUser(User user) { // TODO Auto-generated method stub this.getUserDAO().saveUser(user); } @Override public User getUser(int id) { // TODO Auto-generated method stub return this.getUserDAO().getUser(id); } }
hibernate.cfg.xml: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- (通常是 hibernate.cfg.xml)中声明持久性类: --> <hibernate-configuration> <session-factory> <mapping class="com.tdd.ssh.model.User"/> </session-factory> </hibernate-configuration>
jdbc: #MYSQL jdbc.driverClass=com.mysql.jdbc.Driver jdbc.dialect=org.hibernate.dialect.MySQLDialect jdbc.jdbcUrl=jdbc\:mysql\://localhost\:3306/test jdbc.username=root jdbc.password=111111
SSH-bean: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <bean id="userDAO" class="com.tdd.ssh.dao.UserDAO"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="UserserviceBase" class="com.tdd.ssh.service.Userservice"> <property name="userDAO" ref="userDAO"></property> </bean> <bean id="Userservice" parent="transactionBese"> <property name="target" ref="UserserviceBase"></property> </bean> </beans>
SSH-common: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <!-- 配置hibernate相关数据库的操作 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="50" /> <property name="maxIdleTime" value="1800" /> <property name="acquireIncrement" value="2" /> <property name="maxStatements" value="0" /> <property name="initialPoolSize" value="10" /> <property name="idleConnectionTestPeriod" value="30" /> <property name="acquireRetryAttempts" value="30" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocations"> <list> <value>classpath:hibernate.cfg.xml</value> <!-- 看这里 看这里 看这里 --> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${jdbc.dialect}</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.use_sql_comments">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> </props> </property> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager"></property> <!-- 配置事务管理器 --> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="get*">PROPAGATION_NEVER</prop> </props> </property> </bean> </beans>
springMVC-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.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"> <!-- 注解扫描包 --> <context:component-scan base-package="com.tdd.ssh" /> <!-- 开启注解 --> <mvc:annotation-driven /> <!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean name="/testSSH" class="com.tdd.ssh.controller.UserController" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
<html> </head> <body> 田躲躲 <br/> <h>传递数据</h> ${result} </body> </html>
至此所有的工作都完成了,开始运行.. 【第一次接触springMVC 挺好用~~~】
http://localhost:8080/SSH/testSSH