springmvc+spring+hibernate simple additions and deletions to verify examples
HIbernate configures the MySQL database in the same way as structs+spring+hibernate.
Can be understood as SPRINGMVC to replace the Structs2, SPRINGMVC in the Controller corresponding to the Action in the Structs2, relatively more simple, After all, there is a configuration file struts.xml, this instance even xx.hbm.xml in the way of annotations instead of the
corresponding to the page echo data, Struts2, with the value stack, session, request, etc., SPRINGMVC can also use the servlet api,session can be used, you can use MAP, model and so forth to display data
You can use the Jstl,struts S label on the page.
<s:iterator value= "List" var= "D" >
<c:foreach var= "P" items= "${requestscope.personlist}" >
Both delete and modify are parameters of the incoming ID in the URL
http://localhost:8080/../doupdate?id=402881e958de29980158de2aa5440000
Project Structure
In addition to the spring and hibernate basic packages, you need to add a jar package for the JSTL tag library
Jstl.jar Standard.jar
HIbernate c3p0 jar Package,
C3p0-0.9.2.1.jar Hibernate-c3p0-4.3.11.final.jar Mchange-commons-java-0.2.3.4.jar 3 x
Corresponds to the HIBERNATE-RELEASE-4.3.11.FINAL\LIB\OPTIONAL\C3P0 directory
MySQL Database connection jar Package
Mysql-connector-java-5.1.40-bin.jar
the jar package is in the source code.
No interfaces are used for each layer
Version Information
Eclipse version neon.1a release (4.6.1)
Spring 4.3.4
Hibernate 4.3.11
Tomcat 7.0
JDK 1.8
Database MySQL5.7.12
SOURCE Download: http://download.csdn.net/detail/peng_hong_fu/9706477 partial source code
The business layer and the DAO layer do not use interfaces (decoupling, etc.) Web. Xml
Xml
<?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" 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>springmvc_spring _hibernate</display-name> <!--prevent Chinese parameter scrambling in front--<filter> <filter-name>set Characterencoding</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilt
Er</filter-class> <init-param> <param-name>encoding</param-name>
<param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-valu E> <!--forced transcoding-</init-param> </filter> <filter-mapping> <FILTER-NAME>SETCHARACTERENCODING&L T;/filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--SPR ing configuration listener--> <!--needed for Contextloaderlistener-<context-param> <param-name >contextConfigLocation</param-name> <param-value>classpath:/resources/beans.xml</param-value&
Gt </context-param> <!--bootstraps the root Web application context before servlet initialization---& Lt;listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class&
Gt </listener> <!--SPRINGMVC configuration Dispatcherservlet--<!--the front controller of this Spring WEB AP Plication, responsible for handling all application requests-<servlet> <servlet-name>spring DispatcherservLet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</
Servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/resources/springmvc.xml</param-value> </init-param> <lo Ad-on-startup>1</load-on-startup> </servlet> <!--Map All requests to the Dispatcherservlet for
Handling-<servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
entity class
Person.java
Package com.jxust.svsh.entity;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import javax.persistence.Table;
Import Org.hibernate.annotations.GenericGenerator; /** * Personal Information entity class * Annotation mode configuration Data sheet * @author Peng * @Date2016 December 8 pm 2:54:52 */@Entity @Table (name = "Person") public class person {private String id;//primary key ID private string name;//name private string idcard;//ID number Private Strin
G phone;//Mobile Number private String address;//address public person () {super ();
The public person (string name, string Idcard, String phone, string address) {super ();
THIS.name = name;
This.idcard = Idcard;
This.phone = phone;
this.address = address; @Id @Column (name = "Id", nullable = false, unique = True) @GenericGenerator (name = "Generator", strategy = "UUID") @GeneratedValue (generator = "generator") public StringGetId () {return id;
} public void SetId (String id) {this.id = ID;
} @Column (name = "Name", nullable = false, length = +) public String GetName () {return name;
} public void SetName (String name) {this.name = name;
} @Column (name = "Idcard", nullable = false, length = +) public String Getidcard () {return idcard;
} public void Setidcard (String idcard) {this.idcard = Idcard;
} @Column (name = "Phone", nullable = false, length = +) public String Getphone () {return phone;
} public void Setphone (String phone) {this.phone = phone;
} @Column (name = "Address", Nullable = false, length = +) public String getaddress () {return address;
The public void setaddress (String address) {this.address = address; } @Override Public String toString () {return ' person [id= + ID + ', name= ' + name + ', IdCArd= "+ Idcard +", phone= "+ Phone +", address= "+ address +"] "; }
}
Service Business Layer
Personservice.java
Package com.jxust.svsh.service;
Import java.util.List;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Transactional;
Import Com.jxust.svsh.dao.PersonDAO;
Import Com.jxust.svsh.entity.Person;
@Transactional @Service public class Personservice {@Autowired public Persondao Persondao;
/** * Add * @param person * */public void Addperson (person person) {Persondao.addperson (person); /** * ID query * @param ID * @return */public person Getpersonbyid (String id) {R
Eturn Persondao.getpersonbyid (ID); }/** * UPDATE * @param person */public void Updateperson (person person) {Persondao.updateper
Son (person); }/** * Delete * @param ID */public void Deletepersonbyid (String id) {Persondao.deleteperson
Byid (ID); }/** * Query all * @return */Public list<person> getpersons () {return persondao.getpersons ();
}
}
DAO Layer
Persondao.java
Package Com.jxust.svsh.dao;
Import java.util.List;
Import Javax.annotation.Resource;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import Org.springframework.stereotype.Repository;
Import Com.jxust.svsh.entity.Person;
@Repository public class Persondao {@Resource private sessionfactory sessionfactory;
Private Session getsession () {return sessionfactory.getcurrentsession (); /** * ID query * @param ID * @return */public person Getpersonbyid (String id) {retur N (person) this.getsession (). CreateQuery ("From person where id=?").
Setparameter (0, id). Uniqueresult (); }/** * Add * @param person */public void Addperson (person person) {this.getsession (). Save
(person); }/** * UPDATE * @param person */public void Updateperson (person person) {this.getsession (). u
Pdate (person); }/** * Delete * @param ID */public void DelEtepersonbyid (String ID) {this.getsession (). CreateQuery ("Delete person where id=?").
Setparameter (0, id). executeupdate (); }/** * Query all * @return * */@SuppressWarnings ("unchecked") public list<person> getpersons ()
{return this.getsession (). Createcriteria (Person.class). List (); }
}
Controller
Personcontroller.java
Package Com.jxust.svsh.controller;
Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import org.springframework.web.bind.annotation.SessionAttributes;
Import Com.jxust.svsh.entity.Person;
Import Com.jxust.svsh.service.PersonService; /** * Controller * @author Peng * @Date2016 year December 9 Morning 11:25:40 */@SessionAttributes (value = "username") @Controller @Req
Uestmapping (value = "/person") public class PersonController {@Autowired public personservice personservice; /** * Login request, failed to return error.jsp * * @param username * @param password * @return * * * @Request Mapping ("/login") Public String Dologin (string Username, string password, map<string, object> Map) {if (Username.equals ("admin") && Password.equals ("admin")) {Map.put ("username", username);//stored in Request domain/** * Class plus @sessionattributes ({"username"}) will also be stored in the session field * @SessionAttributes In addition to the property name can be specified to be stored in the session of the attribute (using Val
UE attribute Value) * You can also specify which model properties need to be placed in the session by the object type of the model property (in effect, the types property value), */return "frame";
} return "error";
/** * Save the added data * * @param person * @return */@RequestMapping (value = "/saveperson")
Public String Saveperson (person person) {Personservice.addperson (person);
return "Redirect:main"; }/** * Jump to add page * savepage.jsp * @return */@RequestMapping (value = "/addperson") public
String Saveperson () {return ' savepage ';
}/** * Delete a data * * @param ID * @return */@RequestMapping (value = "/deletepersonbyid") Public String 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 * @param ID * @param model used to save echo data * @return */@RequestMapping (value = "/doupdate") public string DoUpdate (@RequestParam (value = "id") string ID, model model
{Model.addattribute ("person", Personservice.getpersonbyid (ID));
return "Editpage";
/** * Update Data * * @param person * @return */@RequestMapping (value = "/updateperson")
Public String Updateperson (person person) {System.out.println (person.tostring ());
Personservice.updateperson (person);
return "Redirect:main"; /** * Query All personnel information * * @param map uses map to save the echo data * @return */@RequestMapping (value = "/ma In ") Public String mian (map<string, object> Map) {map.put (" personlist ", PersonservicE.getpersons ());
/* Iterate through the collection to see the data being 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";
}
}
configuration files such as XML
Springmvc.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:aop= "Http://www.springframework.org/schema/aop" xmlns:
context= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.3.xsd "> <!--configure automatically scanned packages--<context:component-scan base-package=" Com.jxust "> & Lt;context:include-filter type= "Annotation" expression= "Org.springframewOrk.stereotype.Controller "/> <context:exclude-filter type=" annotation "expression=" Org.springframework.ster Eotype. Service "/>