Spring SPRINGMVC Hibernate consolidated Java Web project __web

Source: Internet
Author: User
Tags aop connection pooling java web log4j

The main is to configure ApplicationContext and Springmvc.xml

Applicationcontext.xml includes the introduction of database properties files, component scans excluding the Controller layer's service layer and the DAO layer

Creates a DataSource bean object and its properties, properties, and the properties of the database connection.

Create sqlsession bean objects, inject data sources, configure Hibernate's attribute SQL formatting, create tables, and dialects;

Creates a transaction-managed bean object, injecting the Sqlsession object,

Use the annotation method of transaction management: <tx:annotation-driven transationmanager= "Transationmanager"/>

Springmvc.xml


Component scans include controller layer exclude service layer and DAO layer

The configuration Content Negotiation Management Bean object contains the media type properties and the default content type;

"Mvc:anotation-driven"

Mvc:default-servlet-handler

and view parser prefixes and suffixes


entity classes, @Entity @Table

"Person")
Controller class @controller Service class @Service DAO class using annotation way @repository;

Class is used in

@Autowired
  Personservice;
@Autowired
Persondao;
@Resource
sessionfactory;

Automatically injects the class to be invoked;

With such annotations, the corresponding Bean objects are not configured in the configuration file;



1, the introduction of the corresponding jar package, Spring package: Spring-aop spring-aspect spring-context Spring-beans spring-core spring-orm Spring-tx spring-test Spring-web Spring-jdbc about 11

Hibernate package: Hibernate3, required under Lib: Jta javaassist slf4j commons-collections dom4j Antrl and HIBERNATE-JPA about 8

and Mysql-connect-java.

Sping-dependent packages spingsource...mchange...c3p0 springsource....commons-logging Spingsource. Aop.. Springsource...aspect. springsource.log4j.slf4j-log4j about 7


2

Establish package and class Com.mz.wfy.domain Person.java

ID//primary Key ID
 name,//name
idcard;//ID number phone
 number
 address 
All of the above are string type    encapsulation and toString methods       
@Id
"Id"falsetrue) "
generator""UUID")
"Generator" )
"Name" false, length = 32)

Establish the corresponding hibernate mapping file User.hbm.xml the table and entity mapping of the database; there are ID (including generator node) property section under the Class node (name table attribute) in hibernate-mapping Point (Name Colomn property)



Com.mz.wfy.controller Personcontroller.java

@SessionAttributes (value ="username"@Controller//Using the annotation flag it is a controller @RequestMapping (value ="/person")Public classPersonController {@Autowired PublicPersonservicePersonservice; /** * Login request, Failure return error.jsp * *@paramusername *@paramPassword *@return * * @RequestMapping ("/login") PublicString Dologin (string username, string password, map<string, object> Map) {if(Username.equals ("Admin") && Password.equals ("Admin")) {Map.put ("username", username)//stored in the request requesting domain/** * class plus @sessionattributes ({"username"}) is also stored in the session domain *@SessionAttributesYou can specify which model attributes need to be placed in the session (actually using the types property value) through the object type of the Model property, in addition to the attributes that need to be stored in the session (using Value property values) through the property name. return"Frame"; } return"Error"; /** * Save added data *@param Person *@return * * @RequestMapping (value ="/saveperson") PublicString Saveperson (person person) {Personservice. Addperson (person); return"Redirect:main"; /** * Jump to add page * savepage.jsp *@return * * @RequestMapping (value ="/addperson") PublicString Saveperson () { return"Savepage"; /** * Delete a piece of data * *@paramID *@return * * @RequestMapping (value ="/deletepersonbyid") PublicString Deletepersonbyid (@RequestParam (value ="id"String ID) {System. out. println ("Delete single");Personservice. Deletepersonbyid (ID); return"Redirect:main"; /** * Jump to update page, echo data * editpage.jsp *@paramID *@paramModelUse model to save ECHO data *@return * * @RequestMapping (value ="/doupdate") PublicString doupdate (@RequestParam (value ="id"String ID, model model) {Model.addattribute ("Person",Personservice. Getpersonbyid (ID)); return"Editpage"; /** * Update Data * *@param Person *@return * * @RequestMapping (value ="/updateperson") PublicString Updateperson (person person) {System. out. println (Person.tostring ());Personservice. Updateperson (person); return"Redirect:main"; /** * Query All personnel information * *@paramMapUse map to save echo data *@return * * @RequestMapping (value ="/main") PublicString Mian (map<string, object> Map) {map.put ("Personlist",Personservice. Getpersons ()); 
       /* Traverse the collection to view the data that is queried * list<person> lists = personservice.getpersons (); 
       * Iterator<person> it = lists.iterator ();
       * while (It.hasnext ()) {* Person P =it.next ();
       * SYSTEM.OUT.PRINTLN (P.tostring ()); *  }
       */ return"Main"; }
}


Com.mz.wfy.service Personservice


Com.mz.wfy.dao Persondao


PrivateSession getsession () { returnsessionfactory. Getcurrentsession (); /** * Query by ID *@paramID *@return */ PublicPerson Getpersonbyid (String ID) { return(person) This. GetSession (). CreateQuery ("from person where id=?"). Setparameter (0, id). Uniqueresult (); }/** * Add *@param Person */Public voidAddperson (person person) { This. GetSession (). Save (person); }/** * Update *@param Person */Public voidUpdateperson (person person) { This. getsession (). Update (person); }/** * Delete *@paramID */Public voidDeletepersonbyid (String ID) { This. GetSession (). CreateQuery ("Delete person where id=?"). Setparameter (0, id). executeupdate (); /** * Query All *@return * * @SuppressWarnings ("Unchecked") PublicList<person> getpersons () { return this. GetSession (). Createcriteria (person.class). List (); }




3. Create various properties files and profiles, A jdbc.properties

Jdbc.driverclass=com.mysql.jdbc.driver; JDBC. url=jdbc:mysql://localhost:3306/sprsh?useunicode=yes&characterencoding=utf-8

Jdbc.uername=root jdbc.password=123456


B log4j.properties ..... Baidu Search can be, this later detailed study;

C Applicationcontext.xml



< contextBase-package= "Com.mz.wfy"> < context
     type = "Annotation" 
 expression ="Org.springframework.stereotype.Controller"/> </     
 Context : Component-scan>

A introduce an external property file Jdbc.properties use

<context:property-placeholder location= "Classpath:jdbc.properties"/>

b Configure connection pooling, DataSource data source bean objects

<bean id= "DataSource" class= "com.springframework.org.mchange.c3p0....>

<property name= "Driverclass" value= "${jdbc. driverclass}"/>

<property name= "Jdbcurl" value= "${jdbc. URL}"/>

<property name= "Jdbc.user" value= "${jdbc. Username}"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.