hibernate4.3.5.Final入门1
使用maven管理项目
1.依赖包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>4.3.5.Final</version> </dependency> </dependencies>
2.配置hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/cl?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.max_size">3</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="sin.domain.User" /> </session-factory> </hibernate-configuration>
3.hibernate 工厂类
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { Configuration cfg = new Configuration().configure(); StandardServiceRegistry ssr =new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build(); SessionFactory factory = cfg.buildSessionFactory(ssr); return factory; } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void schemaCreate() { Configuration cf = new Configuration().configure(); SchemaExport se = new SchemaExport(cf); se.create(true, true); } public static void schemaUpdate() { Configuration cf = new Configuration().configure(); SchemaUpdate su = new SchemaUpdate(cf); su.execute(false, true); } public static void schemaValidate() { Configuration cf = new Configuration().configure(); SchemaValidator su = new SchemaValidator(cf); su.validate(); } public static void main(String[] args) { schemaCreate(); } }
4.实体类,此处省略set/get方法
@Entity @Table(name = "t_h_user") public class User { @Id @GeneratedValue private int id; private String name; private Date birthday;
5.测试
public class ch01 { private static SessionFactory sf; @BeforeClass public static void befoe() { sf = HibernateUtil.getSessionFactory(); } @Test public void test() { User u = new User(); u.setName("chenlongccc"); u.setBirthday(new Date()); Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(u); session.getTransaction().commit(); //session.close(); } }
本文出自 “天空海阔” 博客,谢绝转载!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。