看价的后台基本也就那样了,水平有限,一开始没做功能划分,想到什么写什么,好在注释还是有的,虽然没有模块化,起码没一个类写几百行,要修修补补还是可以的。写了几段sql实在没耐心了,还是用框架吧,下面先来背背书。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
配置Hibernate
导入jar包(是的,我不会用maven),在项目目录下建一个hibernate.cfg.xml文件,编辑如下
1 2 3 4 5 6 7 8 9 10 11 12
| <hibernate-configuration> <session-factory > <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbname</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
|
注意其中的数据库名字、用户名、密码,mapping标签就是标记User这个model类,User.hbm.xml这个映射文件也要自己编辑。
编辑映射文件
比如现在有个User类,有以下属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| private int id;
private String clientID;
private String username;
private String password;
private String phoneNumber;
private String deviceToken;
private String itemID_AddDetails;
private Date createTime; private String loginTime;
|
新建一个User.hbm.xml文件,编辑如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.sephilex.model.User"> <id name="id"> <generator class="identity"/> </id> <property name="username"/> <property name="password"/> <property name="deviceToken"/> <property name="clientID"/> <property name="createTime"/> <property name="itemID_AddDetails"/> <property name="phoneNumber"/> <property name="loginTime"/> </class> </hibernate-mapping>
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.sephilex.hibernateTest; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); export.create(true, true); } }
|
不过这一步一般都不用做,因为项目的数据库表一般都设置好了。
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
| package com.sephilex.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = null; try { session = factory.openSession(); session.beginTransaction(); User user = new User(); user.setName("张三"); user.setPassword("123"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { if (session != null) { if (session.isOpen()) { session.close(); } } } } }
|
增删改查
先封装一些对session的操作
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
| import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils {
private static SessionFactory factory; static { try { Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); }catch(Exception e) { e.printStackTrace(); } }
public static Session getSession() { return factory.openSession(); }
public static void closeSession(Session session) { if (session != null) { if (session.isOpen()) { session.close(); } } } public static SessionFactory getSessionFactory() { return factory; } }
|
增删改查
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import java.util.Date; import org.hibernate.Session; import com.sephilex.hibernate.HibernateUtils; import com.sephlex.hibernate.User; import junit.framework.TestCase; public class TestHibernate {
public void testSave() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user = new User(); user.setName("李四"); user.setPassword("123"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); session.save(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
public void testDel(){ Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user=(User)session.load(User.class, "4028989545a244640145a24466300001"); session.delete(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
public void testUpdate(){ Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001"); user.setName("王五"); session.update(user); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } }
public void testLoad(){ Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user=(User)session.load(User.class, "4028989545a243ca0145a243cbf80001"); System.out.println(user.getName()); System.out.println(user.getId()); System.out.println(user.getPassword()); System.out.println(user.getClass()); System.out.println(user.getCreateTime()); System.out.println(user.getExpireTime()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }
|
配置c3p0连接池
假装有用户,假装有并发
配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.timeout">300</property> <property name="automaticTestTable">Test</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.acquire_increment">1</property> <property name="c3p0.testConnectionOnCheckout">true</property> <property name="hibernate.c3p0.validate">true</property> <property name="c3p0.idleConnectionTestPeriod">18000</property> <property name="c3p0.maxIdleTime">25000</property> <property name="c3p0.idle_test_period">120</property>
|