1 2 3 4 5 6 7 8 | CREATE TABLE contact ( ID varchar (8) NOT NULL , LOGIN_ID varchar (18) NOT NULL , PASSWORD varchar (12) NOT NULL , NAME varchar (40) NOT NULL , EMAIL varchar (40) NOT NULL , PRIMARY KEY (ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | public class ContactEntity { private String _id; private String _name; private String _email; private String _loginId; private String _password; <br> public ContactEntity(){} public ContactEntity(String id, String loginId, String password ,String name, String email){ this ._id = id; this ._loginId = loginId; this ._password = password; this ._name = name; this ._email = email; } public String getId(){ return this ._id; } public void setId(String id){ this ._id=id; } public String getEMail(){ return this ._email; } public void setEMail(String email){ this ._email=email; } public String getName(){ return this ._name; } public void setName(String name){ this ._name=name; } public String getLoginId(){ return this ._loginId; } public void setLoginId(String loginId){ this ._loginId=loginId; } public String getPassword(){ return this ._password; } public void setPassword(String password){ this ._password=password; } public String toString(){ return "{ Id=\"" + this ._id + "\"" + ", LoginId=\"" + this ._loginId + "\"" + ", Name=\"" + this ._name + "\"" + ", EMail=\"" + this ._email + "\" }" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version = "1.0" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" < hibernate-mapping > < class name = "ContactEntity" table = "CONTACT" > < id name = "Id" column = "ID" > < generator class = "assigned" /> </ id > < property name = "EMail" column = "EMAIL" /> < property name = "Name" column = "NAME" /> < property name = "LoginId" column = "LOGIN_ID" /> < property name = "Password" column = "PASSWORD" /> </ class > </ hibernate-mapping > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; <br> public class Main { public static void main(String[] args) { String loginId = "RicCC@cnblogs.com" ; String password = "123" ; if (args!= null && args.length== 2 ){ loginId = args[ 0 ]; password = args[ 1 ]; } HibernateTest(loginId, password); } private static void HibernateTest(String loginId, String password){ SessionFactory factory = null ; Session session = null ; Transaction transaction = null ; List contacts = null ; Iterator it = null ; try { factory = new Configuration().configure().buildSessionFactory(); session = factory.openSession(); transaction = session.beginTransaction(); <br> System.out.println( "===Create Contacts===" ); ContactEntity c = new ContactEntity( "01111111" , "RicCC@cnblogs.com" , "123" , "Richie" , "RicCC@cnblogs.com" ); session.save(c); c = new ContactEntity( "91111111" , "a@cnblogs.com" , "123" , "AAA" , "a@cnblogs.com" ); session.save(c); c = new ContactEntity( "81111111" , "b@cnblogs.com" , "123" , "BBB" , "b@cnblogs.com" ); session.save(c); c = new ContactEntity( "31111111" , "c@cnblogs.com" , "123" , "CCC" , "c@cnblogs.com" ); session.save(c); session.flush(); <br> System.out.println( "\n===Login Test===" ); contacts = session.createQuery( "from ContactEntity where LoginId=:loginId" ) .setString( "loginId" , loginId) .list(); if (contacts.isEmpty()) System.out.println( "contact " + loginId + " not found!" ); else { c = (ContactEntity)contacts.get( 0 ); if (c.getPassword()==password) System.out.println( "user " + loginId + " login successful" ); else System.out.println( "password is incorrect, login failed!" ); } <br> System.out.println( "\n===Delete Contacts===" ); contacts = session.createQuery( "from ContactEntity" ).list(); it = contacts.iterator(); while (it.hasNext()){ session.delete(it.next()); } transaction.commit(); } catch (Exception e){ if (transaction!= null ) transaction.rollback(); System.out.println(e.getMessage()); } finally { if (session!= null ) session.close(); if (factory!= null ) factory.close(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <? xml version = '1.0' encoding = 'utf-8' ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" < hibernate-configuration > < session-factory > < property name = "connection.driver_class" >com.mysql.jdbc.Driver</ property > < property name = "connection.username" >root</ property > < property name = "connection.password" >dev</ property > < property name = "connection.pool_size" >10</ property > < property name = "show_sql" >true</ property > < property name = "dialect" >org.hibernate.dialect.MySQLDialect</ property > < property name = "hbm2ddl.auto" >validate</ property > < mapping resource = "ContactEntity.hbm.xml" /> </ session-factory > </ hibernate-configuration > |
联系客服