Maven version JPA best practices and mavenjpa Best Practices

Source: Internet
Author: User
Tags getzip

Maven version JPA best practices and mavenjpa Best Practices
Project Structure

/* Navicat MySQL Data TransferSource Server: localhostSource Server Version: 50525 Source Host: localhost: 3306 Source Database: jpa-demoTarget Server Type: MYSQLTarget Server Version: 50525 File Encoding: 65001 Date: 20:09:27 */SET FOREIGN_KEY_CHECKS = 0; -- Modify Table structure for 'address' -- ---------------------------- drop table if exists 'address'; create table 'address' ('ssid SSID 'int (11) not null, 'city' varchar (55) not null, 'street' varchar (55) not null, 'zip' varchar (8) not null, primary key ('address') ENGINE = InnoDB default charset = utf8; -- ------------------------------ Records of address -- -------------------------- insert into 'address' VALUES ('1', 'shenzhen ', 'takada market ', '123'); insert into 'address 'values ('2', 'shenzhen', 'takada intersection', '123 '); insert into 'address 'values ('3', 'shenzhen ', 'four seasons flower city', '123 '); -- ---------------------------- Table structure for 'userinfo' -- ---------------------------- drop table if exists 'userinfo'; create table 'userinfo' ('userid' int (11) not null, 'username' varchar (20) not null, 'birthday' datetime default null, 'sex' varchar (8) not null, 'ssid SSID 'int (11) not null, primary key ('userid') ENGINE = InnoDB default charset = utf8; -- Restore Records of userinfo -- -------------------------- insert into 'userinfo' VALUES ('1', 'zhang jinxiong ', null, 'male', '1'); insert into 'userinfo' VALUES ('2', 'Lee moumou ', null, 'male', '2 '); insert into 'userinfo' VALUES ('3', 'wang mou', '2017-08-10 00:00:00 ', 'female', '3 '); insert into 'userinfo' VALUES ('4', 'chen moumou ', '2017-08-12 00:00:00', 'male', '3 ');Source code list 2: pom. xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.coderdream</groupId><artifactId>jpa-demo</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>jpa-demo Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><junit.version>4.11</junit.version><mysql.version>5.1.17</mysql.version><slf.version>1.7.5</slf.version><toplink.essentials.version>2.1-60f</toplink.essentials.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>toplink.essentials</groupId><artifactId>toplink-essentials</artifactId><version>${toplink.essentials.version}</version></dependency><dependency><groupId>toplink.essentials</groupId><artifactId>toplink-essentials-agent</artifactId><version>${toplink.essentials.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency></dependencies><build><finalName>jpa-demo</finalName></build></project>
Code List 3: persistence. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Persistence xmlns = "http://java.sun.com/xml/ns/persistence" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version = "1.0"> <persistence-unit name = "piscesPU" transaction-type = "RESOURCE_LOCAL"> <provider> oracle. toplink. essentials. persistenceProvider </provider> <class> com. coderdream. m Odel. UserInfo </class> <class> com. coderdream. model. Address </class> <properties> <! -- Database connection configuration, JDBC driver --> <property name = "toplink. jdbc. Driver" value = "com. mysql. jdbc. driver"/> <! -- Database connection configuration, URL --> <property name = "toplink. jdbc. url" value = "jdbc: mysql: // localhost: 3306/jpa-demo"/> <! -- Database connection configuration, username --> <property name = "toplink. jdbc. user" value = "root"/> <! -- Database connection configuration and password --> <property name = "toplink. jdbc. password" value = "1234"/> </properties> </persistence-unit> </persistence>
Code list 4: Address. java
Package com. coderdream. model; import java. io. serializable; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. id; @ Entitypublic class Address implements Serializable {// Address id, cannot be empty, must be unique @ Id @ Column (name = "addressid", unique = true, nullable = false) private int addressid; // city, cannot be blank @ Column (name = "city", nullable = false) private String city; // Street, cannot be blank @ Column (name = "Street", nullable = false) private String street; // zip code, cannot be empty @ Column (name = "zip", nullable = false) private String zip; public Address () {} public Address (int addressid) {this. setAddressid (addressid);} public int getAddressid () {return this. addressid;} public void setAddressid (int addressid) {this. addressid = addressid;} public String getCity () {return this. city;} public void setCity (String city) {th Is. city = city;} public String getStreet () {return street;} public void setStreet (String street) {this. street = street;} public String getZip () {return this.zip;} public void setZip (String zip) Export this.zip = zip;} @ Overridepublic int hashCode () {return this. addressid;} @ Overridepublic boolean equals (Object object) {if (! (Object instanceof Address) return false; final Address other = (Address) object; return this. addressid = other. addressid ;}@ Overridepublic String toString () {return "Address [addressid =" + getAddressid () + ", city = '" + getCity () + "', street = '"+ getStreet () +"', zip = '"+ getZip () + "";}}
Code List 5: UserInfo. java
Package com. coderdream. model; import java. io. serializable; import java. SQL. timestamp; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. id; import javax. persistence. joinColumn; import javax. persistence. oneToOne; import static javax. persistence. cascadeType. *; @ Entitypublic class UserInfo implements Serializable {// user Id. It cannot be blank and must be unique @ Id @ Column (name = "userid", unique = True, nullable = false) private int userid; // user name, cannot be empty @ Column (name = "userName", nullable = false) private String userName; // gender, cannot be blank @ Column (name = "sex", nullable = false) private String sex; // Date of birth, can be blank @ Column (name = "birthday ") private Timestamp birthday; // address, cannot be blank // PERSIST indicates that the updated UserInfo data will be updated at the same time when new UserInfo data is added; // REMOVE indicates that the UserInfo deleted from the database will also delete the corresponding data in the Address Table @ OneToOne (cascade = {PERSIST, REMOVE }) @ JoinColumn (name = "addressID", nullable = false) private Address address Address; public UserInfo () {} public UserInfo (int userid) {this. setUserid (userid) ;}@ Overridepublic int hashCode () {return this. getUserid () ;}@ Overridepublic boolean equals (Object object Object) {if (! (Object instanceof UserInfo) return false; final UserInfo other = (UserInfo) object; return this. userid = other. userid;} @ Overridepublic String toString () {return "UserInfo [userid =" + this. userid + ", userName = '" + userName + "', sex = '" + sex + "', birthday =" + birthday + ", address = "+ address +" ";} public int getUserid () {return userid;} public void setUserid (int userid) {this. userid = userid;} public String getUserName () {return userName;} public void setUserName (String userName) {this. userName = userName;} public Timestamp getBirthday () {return birthday;} public void setBirthday (Timestamp birthday) {this. birthday = birthday;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public Address getAddress () {return address;} public void setAddress (Address address Address) {this. address = address ;}}
Code List 6: SimpleService. java
Package com. coderdream. service; import java. util. list; import javax. persistence. entityManager; import javax. persistence. entityManagerFactory; import javax. persistence. persistence; import com. coderdream. model. address; import com. coderdream. model. userInfo; public class SimpleService {/*** delete data with user id = 6 */public static void delete () {final EntityManagerFactory emf = Persistence. createEntityManagerFactory ("piscesPU"); final EntityManager em = emf. createEntityManager (); // if no data is found, an exception UserInfo info = em is thrown here. find (UserInfo. class, 6); try {em. getTransaction (). begin (); em. remove (info); em. getTransaction (). commit ();} finally {em. close () ;}/ *** modify user id = 6 data */public static void update () {final EntityManagerFactory emf = Persistence. createEntityManagerFactory ("piscesPU"); final EntityManager em = emf. createEntityManager (); // if no data is found, an exception UserInfo info = em is thrown here. find (UserInfo. class, 6); info. setUserName ("Haha"); info. getAddress (). setStreet (" 2"); try {em. getTransaction (). begin (); // automatically updates info to the database em. getTransaction (). commit ();} finally {em. close () ;}/ *** query all user data */public static void query () {final EntityManagerFactory emf = Persistence. createEntityManagerFactory ("piscesPU"); long s = System. currentTimeMillis (); // if the database connection fails, the final EntityManager em = emf will be thrown. createEntityManager (); long e = System. currentTimeMillis (); System. out. println ("database connection time:" + (e-s) + "millisecond"); // get data @ SuppressWarnings ("unchecked") List <UserInfo> list = em. createQuery ("SELECT a FROM UserInfo "). getResultList (); int I = 0; for (UserInfo info: list) {System. out. println ("nth" + (++ I) + "value:" + info);} em. close ();}/*** create a piece of data with the user id = 6, address id = 6 */public static void create () {final EntityManagerFactory emf = Persistence. createEntityManagerFactory ("piscesPU"); final EntityManager em = emf. createEntityManager (); UserInfo info = new UserInfo (6); info. setSex ("male"); info. setUserName ("Zhang xx"); info. setBirthday (new java. SQL. timestamp (System. currentTimeMillis (); Address naddr = new Address (6); naddr. setCity ("Shenzhen"); naddr. setStreet ("sakada"); naddr. setZip ("518000"); info. setAddress (naddr); try {em. getTransaction (). begin (); em. persist (info); em. getTransaction (). commit ();} finally {em. close () ;}/ *** main function */public static void main (String [] args) throws Throwable {SimpleService. query (); SimpleService. create (); System. out. println ("query after adding a piece of data"); SimpleService. query (); SimpleService. update (); System. out. println ("querying after modifying a piece of data"); SimpleService. query (); SimpleService. delete (); System. out. println ("query after deleting a piece of data"); SimpleService. query ();}}
Running result
[TopLink Info]: 08:24:08. 134 -- ServerSession (1112823384) -- TopLink, version: Oracle TopLink essen- 2.1 (Build 60f (01/07/2009) [TopLink Info]: 08:24:08. 822 -- ServerSession (1112823384) -- file:/E:/E_441_64/workspace/jpa-demo/target/classes/-piscesPU login successful connection time: 1264 milliseconds, 1st values: userInfo [userid = 1, userName = 'zhang jinxiong ', sex = 'male', birthday = null, address = Address [addressid = 1, city = 'shenzhen ', street = 'qingtian market ', zip = '1997, 518001 values: UserInfo [userid = 2, userName = 'Lee mous', sex = 'male', birthday = null, address = Address [addressid = 2, city = 'shenzhen', street = 'sakada intersections ', zip = '2017 518002 3rd values: UserInfo [userid = 3, userName = 'wang moumou ', sex = 'female', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 4, userName = 'chen moumou ', sex = 'male', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen', street = 'four seasons flower city', zip = '2017 Add a new piece of data and query the time used to connect to the database: 1st values in 0 Ms: UserInfo [userid = 1, userName = 'zhang jinxiong ', sex = 'male', birthday = null, address = Address [addressid = 1, city = 'shenzhen ', street = 'putian market', zip = '2017 518001 2nd values: UserInfo [userid = 2, userName = 'Lee moumou ', sex = 'male', birthday = null, address = Address [addressid = 2, city = 'shenzhen', street = 'sakada intersections ', zip = '2017 518002 3rd values: userInfo [userid = 3, userName = 'wang moumou ', sex = 'female', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 4, userName = 'chen moumou ', sex = 'male', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 6, userName = 'zhang moumou ', sex = 'male', birthday = 20:24:09. 102, address = Address [addressid = 6, city = 'shenzhen', street = 'qingtian ', zip = '2017 it takes time to query and connect to the database after modifying a piece of data: 1st values in 0 Ms: UserInfo [userid = 1, userName = 'zhang jinxiong ', sex = 'male', birthday = null, address = Address [addressid = 1, city = 'shenzhen ', street = 'putian market', zip = '2017 518001 2nd values: UserInfo [userid = 2, userName = 'Lee moumou ', sex = 'male', birthday = null, address = Address [addressid = 2, city = 'shenzhen', street = 'sakada intersections ', zip = '2017 518002 3rd values: userInfo [userid = 3, userName = 'wang moumou ', sex = 'female', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 4, userName = 'chen moumou ', sex = 'male', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 6, userName = 'haha', sex = 'male', birthday = 20:24:09. 102, address = Address [addressid = 6, city = 'shenzhen', street = 'putian 2', zip = '000000' delete a piece of data and query the time used to connect to the database: 1st values in 0 Ms: UserInfo [userid = 1, userName = 'zhang jinxiong ', sex = 'male', birthday = null, address = Address [addressid = 1, city = 'shenzhen ', street = 'putian market', zip = '2017 518001 2nd values: UserInfo [userid = 2, userName = 'Lee moumou ', sex = 'male', birthday = null, address = Address [addressid = 2, city = 'shenzhen', street = 'sakada intersections ', zip = '2017 518002 3rd values: userInfo [userid = 3, userName = 'wang moumou ', sex = 'female', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017 518003 values: UserInfo [userid = 4, userName = 'chen moumou ', sex = 'male', birthday = 00:00:00. 0, address = Address [addressid = 3, city = 'shenzhen ', street = 'four seasons flower city', zip = '2017
Complete Project source code

: Http://download.csdn.net/detail/xuxiheng/8181849

References

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.