Simple implementation of adding user functions, only for beginners reference, you can expand the program function (increase and deletion).
Paste the code here, you need to download it (because it is lazy).
Applicationcontext.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:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:p= "http://www.springframework.org/schema/p" xmlns:util= "http:/ /www.springframework.org/schema/util "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "Xmlns:cache= "Http://www.springframework.org/schema/cache"xsi:schemalocation= "http://Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx.xsdhttp//Www.springframework.org/schema/jdbchttp//www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsdhttp//Www.springframework.org/schema/cachehttp//www.springframework.org/schema/cache/spring-cache-3.1.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/utilhttp//www.springframework.org/schema/util/spring-util.xsd "><!--introduce external configuration files--<context:property-placeholder location= "classpath:jdbc.properties"/> <!--Configure connection pooling--&G T <bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= "${jdbc.driverclass}"/> ; <property name= "Jdbcurl" value= "${jdbc.url}"/> <property name= "user" value= "${jdbc.username}"/> <property name= "Password" value= "${jdbc.password}"/> </bean> <!--Configure Hibernate-related properties-< ; Bean id= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <!--injection Connection pool--<property NA Me= "DataSource" ref= "DataSource"/> <!--Configure hibernate Properties--<property name= "Hibernateproper Ties "> <props> <prop key=" Hibernate.dialect ">org.hibernate.dialect.mysqldia lect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >true</prop> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> </props> </property> <!--load a mapping file in Hibernate--<property name= "Mappingreso Urces "> <list> <value>cn/bj/ssh/entity/User.hbm.xml</value> </list> </property> </bean> <!--Configuring the Action class--<bean id= "Userac tion "class= "Cn.bj.ssh.action.UserAction" scope= "prototype" > <!--Manual Injection service--<property name= "use Rservice "ref=" UserService "/> </bean> <!--Configuring the Business Class--<bean id=" UserService "class= "Cn.bj.ssh.service.UserService" > <property name= "Userdao" ref= "Userdao"/> </bean> <!-- Configure DAO's Class-<bean id= "Userdao"class= "Cn.bj.ssh.dao.UserDao" > <property name= "sessionfactory" ref= "Sessionfactory"/> </bean> & lt;! --Configuration transaction Manager--<bean id= "TransactionManager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name= "sessionfactory" ref= "se Ssionfactory "/> </bean> <!--opening annotated things--<tx:annotation-driven transaction-manager=" transactionm Anager "/> </beans>
View Code
Connection Database configuration: jdbc.properties
# JDBC Configuration jdbc.driverclass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:// 127.0.0.1:3306/sshjdbc.username=rootjdbc.password=root
View Code
Struts.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public " -//apache software foundation//dtd Struts Configuration 2.3//en " http://struts.apache.org/dtds/struts-2.3.dtd" ><struts> <!--by spring manage Direct Write ID-- <packageextends= " Struts-default "namespace="/"> class=" Useraction "method=" {1} "> <result name=" Loginsuccess ">/index.jsp</result> <!--<result name=" Success_save ">/index.jsp</result >--> </action> </Package ></struts>
View Code
Useraction.java
Packagecn.bj.ssh.action;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.opensymphony.xwork2.ModelDriven;ImportCn.bj.ssh.entity.User;ImportCn.bj.ssh.service.UserService; Public classUseractionextendsActionsupportImplementsModeldriven<user>{ Private Static Final LongSerialversionuid = 1L; //classes used by model drivers PrivateUser User =NewUser (); //Automatic injection PrivateUserService UserService; Public voidSetuserservice (UserService userservice) { This. UserService =UserService; } @Override PublicUser Getmodel () {returnuser; } //Save User PublicString Save () {userservice.save (user); return"Loginsuccess"; } }
View Code
Userservice.java (because it's simpler and looks more intuitive, service and DAO don't have a write interface)
PackageCn.bj.ssh.service;Importorg.springframework.transaction.annotation.Transactional;ImportCn.bj.ssh.dao.UserDao;ImportCn.bj.ssh.entity.User; @Transactional Public classuserservice{PrivateUserdao Userdao; Public voidSetuserdao (Userdao Userdao) { This. Userdao =Userdao; } Public voidSave (User user) {userdao.save (user); } }
View Code
Userdao.java
PackageCn.bj.ssh.dao;Importorg.hibernate.SessionFactory;ImportCn.bj.ssh.entity.User; Public classUserdao {Privatesessionfactory sessionfactory; Public voidsetsessionfactory (sessionfactory sessionfactory) { This. Sessionfactory =sessionfactory; } PublicString Save (User user) { This. Sessionfactory.getcurrentsession (). Save (user); return"Success_save"; } }
View Code
Entity class user.jsp
Packagecn.bj.ssh.entity; Public classUser {PrivateInteger pid; PrivateString name; PrivateString password; PrivateDouble height; PublicUser () {} PublicUser (Integer pid, string name, string password,double height) {Super(); This. PID =pid; This. Name =name; This. Password =password; This. Height =height; } PublicInteger getpid () {returnpid; } Public voidsetpid (Integer pid) { This. PID =pid; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicDouble getheight () {returnheight; } Public voidsetheight (Double height) { This. Height =height; } }
View Code
Mapping files: User.hbm.xml
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><%@ taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >View CodeAdd user page: adduser.jsp
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate- mappingPublic "-//hibernate/hibernate mapping DTD 3.0//en" "/http Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">class name=" Cn.bj.ssh.entity.User "table=" User "> <id name=" pid "column=" pid "> class=" Native "/> </id> <property name= "name" column= "name" length= "></property> <property" Name= "password" column= "password" length= "/> <property" name= "height" column= "height"/> </ class>
View CodeIt's a bit late to get back to this project link first.
Struts2+spring+hibernte Consolidation Example