1. Project structure
2. Configuration file
Spring.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" XM lns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" Xsi:sche malocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/conte Xt/spring-context.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sp Ring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-ao P.xsd "> <!--spring annotation search scope--> <context:component-scan base-package=" action "/> <context:component-s Can base-package= "service"/> <conteXt:component-scan base-package= "dao"/> <!--referencing the database configuration file--> <context:property-placeholder location= " Classpath:jdbc.properties "/> <!--Configure Data source-use C3P0 connection pool--> <bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "> <property name=" driverclass "value=" ${ Jdbc.driver} "/> <property name=" Jdbcurl "value=" ${jdbc.url} "/> <property name=" user "value=" ${jdbc.usern AME} "/> <property name= password" value= "${jdbc.password}"/> <!--c3p0 Connection pool parameter configuration--> <prop Erty name= "initialpoolsize" value= "${c3p0.initialpoolsize}"/><!--initialize the number of connections--> <property name= "MinPo Olsize "value=" ${c3p0.minpoolsize} "/><!--minimum number of connections--> <property name=" maxpoolsize "value=" ${c3p0.max Poolsize} "/><!--the maximum number of connections reserved in the connection pool default:15--> <property name=" MaxIdleTime "value=" ${c3p0.maxidletime} "/&G
t;<!--Connection expiration (seconds) (<mysql 28800) default:0--> <property name= "Idleconnectiontestperiod" value= "${c3p0.idleconnectiontestperiod}"/><!--check for idle connections in the connection pool (seconds) ( <mysql 28800) default:0--> </bean> <!--configuration hibernate-sessionfactory--> <bean id= "Sessionfac Tory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "> <property name=" DataSource "ref=" DataSource "/> <!--the package in which the entity class is located contains annotation scans for each class in the package, eliminating one configuration--> <property name=" Packagestoscan "> <list>< Value>model</value></list> </property> <!--Configure hibernate properties--> <property name= "Hibernat Eproperties "> <props> <prop key=" hibernate.dialect ">${hibernate.dialect}</prop><!--Database dialect- -> <prop key= "Hibernate.show_sql" >${hibernate.show_sql}</prop><!--Print SQL statements--> <prop key= " Hibernate.format_sql >${hibernate.format_sql}</prop><!--Format SQL--> </props> </property&
Gt </bean> <!--configuration HibernateService Manager--> <bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <!--using annotations-registration Transaction Management class or method--&G
T <tx:annotation-driven transaction-manager= "TransactionManager"/> </beans>
Jdbc.properties (database connection pool configuration file)
######################################## ######## Database Configuration ####### #################################
###### ##\u6570\u636e\u5e93\u9a71\u52a8 Jdbc.driver=com.mysql.jdbc.driver ##\u8fde\u63a5\u5b57\u7b26\u4e32 Jdbc.url=jdbc:mysql://localhost:3306/book ##\u6570\u636e\u5e93\u7528\u6237\u540d Jdbc.username=root ##\u6570\ u636e\u5e93\u6570\u636e\u5e93\u5bc6\u7801 jdbc.password=123456 ######################################## ########
C3P0 configuration ####### ####################################### ##\u521d\u59cb\u5316\u8fde\u63a5\u6570 default:3 c3p0.initialpoolsize=3 ##\u6700\u5c0f\u8fde\u63a5\u6570 c3p0.minpoolsize=2 ##\u8fde\u63a5\u6c60\u4e2d\u4fdd\u7559 \u7684\u6700\u5927\u8fde\u63a5\u6570 default:15 c3p0.maxpoolsize=10 ##\u8fde\u63a5\u7684\u8fc7\u671f\u65f6\u95f4 ( \U79D2), (<mysql 28800) default:0-\u6c38\u4e0d\u4e22\u5f03 c3p0.maxidletime=28000 ##\u9694\u591A\u5C11\u79D2\ U68C0\U67E5\U6240\U6709\U8FDE\U63A5\U6C60\U4E2D\U7684\U7A7A\U95F2\U8FDE\U63A5, (<mysql 28800) default:0-\u4e0d\u68c0\u67e5 c3p0.idleconnectiontestperiod=3600 ################################# ####### ######## Hibernate configuration ####### ####################################### B9\u8a00 hibernate.dialect=org.hibernate.dialect.mysqldialect #hibernate. dialect= Org.hibernate.dialect.SQLServerDialect #hibernate. Dialect=org.hibernate.dialect.oracledialect ##\u662f\u5426\ U6253\u5370sql\u8bed\u53e5 hibernate.show_sql=true ##\u662f\u5426\u683c\u5f0f\u5316sql\u8bed\u53e5 (\u5206\u884C\ u663e\u793a) Hibernate.format_sql=false # #session \u6bcf\u6b21\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\ U636E\U6570\U91CF #hibernate. jdbc.fetch_size=10 ##\u6bcf\u6b21\u6279\u91cf\u5904\u7406\u63d2\u5165\u6216\u66f4\ U65B0\U7684\U6570\U91CF #hibernate. jdbc.batch_size=10 ##\u662f\u5426\u4f7f\u7528jdbc\u53ef\u6eda\u52a8\u7684\ U7ED3\U679C\U96C6, \u5bf9\u5206\u9875\u6709\u5e2e\u52a9 #hibernate. Jdbc.use_scrollable_resultset=true # #create \ Uff1a\u8868\U793a\u542f\u52a8\u7684\u65f6\u5019\u5148drop\uff0c\u518dcreate # #create-drop: \u4e5f\u8868\u793a\u521b\u5efa\ Uff0c\u53ea\u4e0d\u8fc7\u518d\u7cfb\u7edf\u5173\u95ed\u524d\u6267\u884c\u4e00\u4e0bdrop # #update: \u8FD9\u4E2A\ U64cd\u4f5c\u542f\u52a8\u7684\u65f6\u5019\u4f1a\u53bb\u68c0\u67e5schema\u662f\u5426\u4e00\u81f4\uff0c\u5982\ U679C\U4E0D\U4E00\U81F4\U4F1A\U505ASCHEME\U66F4\U65B0 # #validate: \u542f\u52a8\u65f6\u9a8c\u8bc1\u73b0\ U6709schema\u4e0e\u4f60\u914d\u7f6e\u7684hibernate\u662f\u5426\u4e00\u81f4\uff0c\u5982\u679c\u4e0d\u4e00\u81f4 \u5c31\u629b\u51fa\u5f02\u5e38\uff0c\u5e76\u4e0d\u505a\u66f4\u65b0 #hibernate. Hbm2ddl.auto=validate ##\u8FDE\ u63a5\u6570\u636e\u5e93\u65f6\u662f\u5426\u4f7f\u7528unicode\u7f16\u7801 #hibernate. Connection.useunicode=true # #\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u6570\u636e\u7684\u4f20\u8f93\u5b57\u7b26\u96c6\u7f16\u7801\u65b9\u5f0f # Hibernate.connection.characterencoding=utf8
Struts.xml (Note: Many of the action here are configured in annotations so that the contents of the XML file are greatly simplified)
<?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> <!--the encoding mode of request parameters--> <constant name=" struts.i18n.encoding "value=" UTF-8 "/&G
T <!--Specifies the type of request suffix that is struts2 processed. Multiple comma-separated--> <constant name= "struts.action.extension" value= "Action,do"/> <!--reload if struts.xml changes. The default value is false, the development phase is best to open--> <constant name= "Struts.configuration.xml.reload" value= "true"/> <!-- Whether to use the development mode of struts. Development mode will have more debugging information--> <constant name= "Struts.devmode" value= "false"/> <!--set whether the browser caches static content. The default value is true, and the development phase is best to turn off--> <constant name= "Struts.serve.static.browserCache" value= "false"/> <!--whether dynamic method calls are turned on- Allowed to use! Match request--> <constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/> <!-- Specifies that the creation of the action object by spring is <constant name= "struts.objectfactory" value= "Spring"/>--> </stRuts>
3. Part of the code (the project code is relatively large, you can send a part of the user operation as an example)
Useraction.java
Package action;
Import Javax.annotation.Resource; Import model.
User;
Import org.apache.struts2.convention.annotation.Action;
Import Org.apache.struts2.convention.annotation.Namespace;
Import Org.apache.struts2.convention.annotation.Result;
Import Org.apache.struts2.convention.annotation.Results; Import service.
UserService; @Action ("User") @Namespace ("/book") @Results ({@Result (name= "Login", location= "/book/login.jsp"), @Result (name= "
Register ", location="/book/register.jsp "), @Result (name=" index ", location=" Index.action ", type=" redirect "),})
public class Useraction extends baseaction{private static final long serialversionuid = 1L;
private user user;
@Resource private UserService UserService; /** * Registered User * @return/Public String Register () {if (User.getusername (). IsEmpty ()) {addactionmessage ("User name cannot be
Empty! ");
return "register";
}else if (Userservice.isexist (User.getusername ())) {Addactionmessage ("Username already exists!");
return "register"; }else {UserserviCe.add (user);
Addactionmessage ("Registration is successful, please login!");
return "Login"; }/** * User login * @return * @throws Exception/Public String login () {if Userservice.checkuser (user.getus
Ername (), User.getpassword ())) {getsession (). Put ("username", user.getusername ());
Return "index";
else {addactionmessage ("username or password is wrong!");
return "Login";
}/** * Logout login * @return/Public String logout () {getsession (). Remove ("username");
GetSession (). Remove ("indent");
return "Login";
Public user GetUser () {return user;
public void SetUser (user user) {this.user = user;
}
}
Userdao.java
Package DAO;
Import java.util.List; Import model.
User;
Import Org.springframework.stereotype.Repository;
@Repository//Register DAO layer bean equivalent to @component public class Userdao extends basedao{/** * Find user by user name * @return no record returns null */Public User Getbyusername (String username) {return (User) getsession (). CreateQuery ("from User where Username=:usern Ame "). SetString (" username ", username). Uniqueresult (); HQL Statement/** * Find * @param username * @param password * @return no record returns null/public user Getbyu via username and password Sernameandpassword (string Username, string password) {return (User) getsession (). Createsqlquery ("select * from User wher E username=? and password=? "). Addentity (User.class). setstring (0, username). setstring (1, password). Uniqueresult (); SQL statement Mode}/** * Get list * @param page * @param rows * @return No records return empty set/@SuppressWarnings ("unchecked") p Ublic list<user> getlist (int page, int rows) {return getsession (). CreateQuery (' from User '). SetfiRstresult (rows* (page-1)). Setmaxresults (rows). List (); /** * Total * @return/public long gettotal () {return (long) getsession (). CreateQuery ("SELECT count (*) from U"
Ser "). Uniqueresult ();
}
}
User.java
Package model;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
@Entity//annotation for hibernate entity corresponding database user table public
class User {
@Id //Annotation primary key
@GeneratedValue//id generation policy The default auto is equivalent to the hibernate native-self-increasing field
private int id;
Private String username;
private String password;
Private String address;
Private String phone;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
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 String getaddress () {return address
;
}
public void setaddress (String address) {
this.address = address;
}
Public String Getphone () {return
phone;
}
public void Setphone (String phone) {
this.phone = phone;
}
}
Userservice.java
Package service;
Import java.util.List;
Import Javax.annotation.Resource; Import model.
User;
Import Org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Transactional; Import util.
Safeutil; Import DAO.
Userdao; @Service//annotation for service layer Spring Management bean @Transactional//annotation All of these methods join the spring transaction, setting the default public class UserService {@Resource/
/spring Injection Class object private Userdao Userdao; /** * Authentication User Password * @param username * @param password * @return/public boolean CheckUser (string username, string
Password) {return Userdao.getbyusernameandpassword (username, safeutil.encode (password))!= null; /** * User exists * @param username * @return * * Public boolean isexist (String username) {return userdao.getbyu
Sername (username)!= null; /** * Add * @param user * @return/public boolean Add (user user) {User.setpassword (Safeutil.encode) (USER.G
Etpassword ()));
return Userdao.save (user) > 0; /** * gets * @param useri via IDD * @return */public User get (int userid) {return userdao.get (User.class, UserID); /** * @param username * via username * @return */public User gets (String username) {return Userdao.getbyus
Ername (username);
/** * List * @param page * @param rows * @return/public list<user> getlist (int page, int rows) {
return userdao.getlist (page, rows);
/** * Total * @return/public long gettotal () {return userdao.gettotal ();
/** * UPDATE * @param user/public boolean update (user user) {return userdao.update (user);
/** * Delete * @param user */public boolean delete (int id) {User user = new user ();
User.setid (ID);
return Userdao.delete (user);
}
}
From the code can be seen, with the SSH framework of the project hierarchy is more distinct, logical clear, code more concise, good readability
4. Screenshot:
(1) Client user interface
Figure 4.1.1 Bookstore Home
Figure 4.1.2 Bookstore Introduction
Figure 4.1.3 Boutique recommended
Figure 4.1.4 Latest published
Figure 4.1.5 Promotions
Figure 4.1.6 User Registration
Figure 4.1.7 User Login
Figure 4.1.8 User Login Post page
(2) Background management interface
Figure 4.2.1 Background Management Home
Figure 4.2.2 Admin Login Page
Figure 4.2.3 Order Management
Figure 4.2.4 Customer Management
Figure 4.2.5 Book Management
Figure 4.2.6 Class Management
Figure 4.2.7 User Management
Summary: Refer to someone else's code, step-by-step to write their own this project, the middle of a lot of bugs, changed two days, fortunately, the great God to guide, just so smooth. Later have to do more projects, a lot of accumulated experience. SSH completes one, and the next is the SSM framework. The road of the code agriculture is arduous and long, full of fun.