这个整合的文件下载地址在底部,需要的朋友可以拿去使用
第一步:
加入jar包
第二步,编写配置文件
web.xml
<?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">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring-servlet.xml,/WEB-INF/classes/spring-transation.xml,/WEB-INF/classes/spring-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 中文乱码 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring-servlet.xml
<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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.shoe.controller" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
spring-transation.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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
"
>
<!-- 装载属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.test.orm.Manage</value>
</list>
</property>
<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.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource"></property>
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* com.test.daoimpl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
</beans>
spring-beans.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean name="manageDAOIMPL" class="com.test.daoimpl.ManageDAOIMPL">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="manageController" class="com.test.controller.ManageController">
<property name="manageDAOIMPL" ref="manageDAOIMPL"></property>
</bean>
</beans>
jdbc.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/test1
database.user=root
database.password=123
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
还有一个log4j.properties的配置文件,应该会自己找资料导进去吧
这是src目录下的包
ManageController.java
package com.test.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.dao.ManageDAO;
import com.test.orm.Manage;
@Controller
public class ManageController {
@Resource(name="manageDAOIMPL")
private ManageDAO manageDAOIMPL;
/*****两种接收方式**********/
/*@RequestMapping("/addManage") //在浏览器里输入addManage.action
public String addManage(HttpServletRequest request){
Manage manage = new Manage();
System.out.println(request.getParameter("username"));
System.out.println(request.getParameter("password"));
manage.setUsername(request.getParameter("username"));
manage.setPassword(request.getParameter("password"));
boolean b = manageDAOIMPL.addManage(manage);
System.out.println(b);
return "addSuc";
}*/
@RequestMapping("/addManage") //在浏览器里输入addManage.action
public String addManage(Manage manage){
System.out.println(manage.getUsername());
System.out.println(manage.getPassword());
boolean b = manageDAOIMPL.addManage(manage);
System.out.println(b);
return "addSuc";
}
public ManageDAO getManageDAOIMPL() {
return manageDAOIMPL;
}
public void setManageDAOIMPL(ManageDAO manageDAOIMPL) {
this.manageDAOIMPL = manageDAOIMPL;
}
}
ManageDAO.java
package com.test.dao;
import com.test.orm.Manage;
public interface ManageDAO {
public boolean addManage(Manage manage);
}
ManageDAOIMPL.java
package com.test.daoimpl;
import org.hibernate.SessionFactory;
import com.test.dao.ManageDAO;
import com.test.orm.Manage;
public class ManageDAOIMPL implements ManageDAO{
private SessionFactory sessionFactory;
public boolean addManage(Manage manage) {
boolean flag = false;
int i = (Integer)sessionFactory.getCurrentSession().save(manage);
if(i > 0){
flag = true;
}
return flag;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Manage.java
package com.test.orm;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_admin")
public class Manage {
private int id;//管理员id
private String username;//管理员账号
private String password;//管理员密码
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=32)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(length=32)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
index.jsp
<body>
<form action="addManage.action" method="post">
账号:<input type="text" name="username">
密码:<input type="password" name="password"/>
<input type="submit" value="添加">
</form>
</body>
addSuc.jsp //表单提交成功后,跳转到这个页面
//这是我所有的文件目录
这是下载地址,可以拿去参考http://yunpan.cn/QGqsCWIHILfb9