今天用了一下java的数据库持久化-业务的hibernate框架。下面给出hibernate 连接mysql数据库示例
建表结构如下
mysql> desc test; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(100) | NO | | NULL | | +----------+--------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
mysql>
hibernate配置文件 hibernate.cfg.xml
- <?xmlversion='1.0'encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <propertyname="show_sql">true</property>
- <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
- <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
- <propertyname="connection.url">jdbc:mysql://192.168.25.152/test</property>
- <propertyname="connection.username">这里写用户名</property>
- <propertyname="connection.password">密码</property>
- <mappingresource="user.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.25.152/test</property> <property name="connection.username">这里写用户名</property> <property name="connection.password">密码</property> <mapping resource="user.hbm.xml"/> </session-factory> </hibernate-configuration>
表映射 user.hbm.xml
- <?xmlversion='1.0'encoding='UTF-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <classname="com.TestDb"table="test">
- <idname="id"column="id">
- <generatorclass="increment"/>
- </id>
- <propertyname="username"column="username"/>
- </class>
- </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!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.TestDb" table="test"> <id name="id" column="id"> <generator class="increment"/> </id> <property name="username" column="username" /> </class> </hibernate-mapping>
表映射类
- package com;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2006-2-6
- * Time: 22:10:05
- * To change this template use File | Settings | File Templates.
- */
- publicclass TestDb {
- private String username;
- private Long id;
- public Long getId() {
- return id;
- }
- publicvoid setId(Long id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- publicvoid setUsername(String myname) {
- this.username = myname;
- }
- }
package com; /** * Created by IntelliJ IDEA. * User: Administrator * Date: 2006-2-6 * Time: 22:10:05 * To change this template use File | Settings | File Templates. */ public class TestDb { private String username; private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String myname) { this.username = myname; } }
测试类
- package com;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- publicclass test {
- //遍历
- publicstaticvoid all()
- {
- Query q = session.createQuery("select c.id,c.username from TestDb as c");
- List l = q.list();
- for(int i=0;i<l.size();i++)
- {
- //TestDb user = (TestDb)l.get(i);
- //System.out.println(user.getUsername());
- Object[] row = (Object[])l.get(i);;
- Long id = (Long)row[0];
- String name = (String)row[1];
- System.out.println(id+" "+name);
- }
- }
- //读取
- publicstaticvoid load()
- {
- TestDb obj = (TestDb) session.load(TestDb.class, new Long(2));
- System.out.println(obj.getUsername());
- }
- //更新
- publicstaticvoid update()
- {
- TestDb obj = (TestDb) session.load(TestDb.class, new Long(2));
- obj.setUsername("cg");
- }
- //插入
- publicstaticvoid insert()
- {
- TestDb user = new TestDb();
- user.setUsername("sb");
- session.save(user);
- }
- static SessionFactory sessionFactory;
- static Session session ;
- static Transaction tx ;
- privatestaticvoid init()
- {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- session = sessionFactory.openSession();
- tx = session.beginTransaction();
- }
- privatestaticvoid close()
- {
- tx.commit();
- session.close();
- sessionFactory.close();
- }
- publicstaticvoid main(String[] args)
- {
- init();
- update();
- close();
- }
- }
package com; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class test { //遍历 public static void all() { Query q = session.createQuery("select c.id,c.username from TestDb as c"); List l = q.list(); for(int i=0;i<l.size();i++) { //TestDb user = (TestDb)l.get(i); //System.out.println(user.getUsername()); Object[] row = (Object[])l.get(i);; Long id = (Long)row[0]; String name = (String)row[1]; System.out.println(id+" "+name); } } //读取 public static void load() { TestDb obj = (TestDb) session.load(TestDb.class, new Long(2)); System.out.println(obj.getUsername()); } //更新 public static void update() { TestDb obj = (TestDb) session.load(TestDb.class, new Long(2)); obj.setUsername("cg"); } //插入 public static void insert() { TestDb user = new TestDb(); user.setUsername("sb"); session.save(user); } static SessionFactory sessionFactory; static Session session ; static Transaction tx ; private static void init() { sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); } private static void close() { tx.commit(); session.close(); sessionFactory.close(); } public static void main(String[] args) { init(); update(); close(); } }
文件结构