Original link: http://www.cnblogs.com/DiYuShe/admin/EditPosts.aspx? Opt = 1
Before building the environment, we need to prepare the following items (which is used below ):
Structs2.2.3 download
Hibernate3.3.2 (originally used 4.0, but spring does not seem to support 4.0 yet, which will be changed later) download
Spring 3.0 download
Above is the official website
I need other packages later. I have packed the package and downloaded it.
The development tool uses eclipse + jdk6 + tomcat + mysql5.1
Next we will start to build the environment
Create a Dynamic web project and enter any name
First, load the spring environment.
Org. springframework. asm-3.1.0.M2.jar org. springframework. beans-3.1.0.M2.jar org. springframework. context-3.1.0.M2.jar
Org. springframework. core-3.1.0.M2.jar org. springframework. expression-3.1.0.M2.jar org. springframework. jdbc-3.1.0.M2.jar
Org. springframework. web-3.1.0.M2.jar
We recommend that you do not add the most core running code at one time to see what is missing, so that no extra package will be added,Spring3 has separated packages by function
Unlike the previous package, it is so flexible that we don't need to add the required functions as long as we run the required functions and we don't have to add them in a hard way. I will be here to discuss the features of each package.
I'm not talking about Baidu too much!
After adding the package, we start to create a spring application and create an XML file ApplicationContext. xml (put in the src root directory). You can also write other names,
Below is the basic structure
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
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>
/* Add beans here */
<Beans>
Let's test whether spring has been set up.
Create a Person class
Public class Person {
Private String name;
Private int age;
/* You can also write the get method. If you do not write the get method, spring has no requirement */
Public void setName (String name) {this. name = name}
Public void setAge (int age) {this. age = age}
Public String toString (){
Return "name:" + name + "age:" + age; // rewrite the toString method to output information.
}
}
Then
Add bean-related configuration in the configuration file AppliccationContext. xml (this is a value injection, interface and construction injection)
<Bean id = "nike" class = "Person" scope = "prototype">
<Property name = "name" value = "XXX"> </property>
<Property name = "age" value = "18"> </property>
</Bean>
Test code
Public class testPerson {
Public static void main (String [] args ){
XmlBeanFactory beansfactory = new XmlBeanFactory (new FileSystemResource ("src/ApplicationContext. xml "));
System. out. println (beansfactory );
Person nike = (Person) beansfactory. getBean ("nike ");
System. out. println (nike );
}
The running result should be: name: nike age: 18
This proves that the beans have been created by ourselves, and we will integrate the packages required by hibernate as follows:
Antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.jar ejb3-persistence.jar
Hibernate3.jar hibernate-annotations.jar hibernate-commons-annotations.jar
Javassist-3.9.0.GA.jar jta-1.1.jar
Add and configure hibernate in spring (we need to create another database called spring)
<! -- Integrate the hibernate configuration section -->
<Bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource"
Destroy-method = "close"> <! -- Define a data source -->
<Property name = "driverClassName" value = "com. mysql. jdbc. Driver"/> <! -- Define a database driver -->
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/spring? CharacterEncoding = UTF-8 "/> <! -- Connection string -->
<Property name = "username" value = "root"/> <! -- Account -->
<Property name = "password" value = "198902"/> <! -- Password -->
</Bean>
<! -- Configure sessionFactory -->
<Bean id = "sessionFactory"
Class = "org. springframework. orm. hibernate3.annotation. AnnotationSessionFactoryBean"
>
<Property name = "dataSource" ref = "dataSource"/>
<Property name = "annotatedClasses">
<List>
<Value> com. spring. beans. Person </value> <! -- Define the element association. You can configure the annoration configuration in the xml file. -->
</List>
</Property>
<Property name = "hibernateProperties"> <! -- Hibernate parameter -->
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
<Prop key = "hiberante. format_ SQL"> true </prop>
<Prop key = "hibernate. hbm2ddl. auto"> update </prop>
</Props>
</Property>
</Bean>
<! -- Define DAO -->
<Bean id = "me" class = "com. hibernate. dao. PersonDao">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
We can use the Person just now.
@ Entity
@ Table (name = "person ")
Public class Person {
@ Id
@ Column (unique = true, name = "person_id", nullable = false)
Private int id;
@ Column (name = "name", length = 20)
Private String name;
@ Column (name = "age ")
Private int age;
/* You can also write the get method. If you do not write the get method, spring has no requirement */
Public void setName (String name) {this. name = name}
Public void setAge (int age) {this. age = age}
Public void setId (int id) {this. id = id}
... The get method at the end saves the need for self-writing.
Public String toString (){
Return "name:" + name + "age:" + age; // rewrite the toString method to output information.
}
}
Below is the DAO Interface
Public interface IPersonDao {
Public void createPerson (Person person );
Public List <Serializable> getAll ();
Public int count ();
Public void drop (Person person );
}
DAO implementation:
Public class PersonDao implements IPersonDao {
Private SessionFactory sessionFactory;
@ Override
Public void createPerson (Person person ){
Session session = sessionFactory. openSession ();
Session. beginTransaction ();
Session. save (person );
Session. beginTransaction (). commit ();
}
@ Override
Public List <Serializable> getAll (){
Session session = sessionFactory. openSession ();
String SQL = "from Person ";
Session. beginTransaction ();
Query query = session. createQuery (SQL );
Return query. list ();
}
@ Override
Public int count (){
Session session = sessionFactory. openSession ();
String SQL = "from person ";
Session. begintransaction ();
Query query = session. createquery (SQL );
Return query. List (). Size ();
}
@ Override
Public void drop (person ){
Session session = sessionfactory. opensession ();
Session. begintransaction ();
Session. Delete (person );
Session. begintransaction (). Commit ();
}
Public void setsessionfactory (sessionfactory ){
This. sessionfactory = sessionfactory;
}
}
Test method:
Public class testperson {
Public static void main (string [] ARGs ){
Xmlbeanfactory beansfactory = new xmlbeanfactory (New filesystemresource ("src/applicationcontext. xml "));
PersonDao persondao = (PersonDao) beansfactory. getBean ("me ");
Person person = new Person ();
Person. setId (5 );
Person. setFirstname ("12223 ");
Person. setLastname ("qwe2 ");
Persondao. createPerson (person );
System. out. println (persondao. getAll (). size ());
}
}
Since the data table is set to automatic update, if there is no table, it will automatically create the relationship between the table and them.
Open the mysql client and enter:
Use spring;
Select 8 from person;
If we see our data, it will prove OK!
Here we have basically integrated hibernate!
Start integrating struts
; Old:
Struts2-core-2.2.3.jar struts2-spring-plugin-2.2.3.jar xwork-core-2.2.3.jar
Ognl-3.0.1.jar freemarker-2.3.16.jar commons-logging-api-1.1.jar commons-fileupload-1.2.2.jar
Antlr-2.7.2.jar
Create the configuration file struts. xml under SRC
I will write a little bit from the Web. xml configuration, and I think it is better to understand it (simply write it in the form of comments without explanation)
<? 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_3_0.xsd" id = "webapp_id" version = "3.0">
<Display-Name> spring_structs_ajax </display-Name>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> <! -- Specify the location of the spring configuration file -->
/WEB-INF/classes/ApplicationContext. xml
</Param-value>
</Context-param>
<Listener>
<Listener-class>
Org. springframework. web. context. ContextLoaderListener
</Listener-class>
</Listener> <! -- Configure interception -->
<Filter>
<Filter-name> struts2 </filter-name>
<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> struts2 </filter-name> <! -- Struts configuration -->
<Url-pattern>/* </url-pattern>
</Filter-mapping>
</Web-app>
Struts. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Constant name = "struts. devMode" value = "true"/>
<! -- It is important to specify the factory -->
<Constant name = "struts. objectFactory" value = "spring"> </constant>
<Package name = "B" extends = "struts-default">
<Action name = "index">
<Result>/index. jsp </result>
</Action>
<! -- The first test -->
<Action name = "h" class = "pca" method = "getPe">
<Result name = "SUCCESS">/hello. jsp </result>
</Action>
</Package>
</Struts>
Create an ACtion
Public class personAction extends ActionSupport {
Private static final long serialVersionUID = 1L;
Private IPersonDao dao;
/*
* Test Method
* The spring value injection sequence is based on the configuration file values.
*/
Public String getPe (){
System. out. println (dao );
HttpServletRequest request = ServletActionContext. getRequest ();
Request. getSession (). setAttribute ("personlist", dao. getAll ());
Return "SUCCESS ";
}
Public IPersonDao getDao (){
Return dao;
}
Public void setDao (IPersonDao dao ){
This. dao = dao;
}
}
When running, how can we see the results? Don't worry, just create a page:
Create a hello. jsp page under WebContent
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> test OK </title>
</Head>
<Body>
Test OK <br>
<A> last name: </a> <label >$ {personlist} </label> <br/>
</Body>
</Html>
Run open browser: Enter http: // localhost: 8080/project name/h
I can package the source code and download it myself.
We can see the data in our database! This is the end of the article. If the first article is not red, please point out more!