81,092
社区成员
发帖
与我相关
我的任务
分享
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.autocommit">true</property >
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="MyUser.hbm.xml"/>
<mapping class="com.example.hibernate.MyUser" />
</session-factory>
</hibernate-configuration>
<?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.example.hibernate.MyUser" table="myuser">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" length="20" column="username" type="string" not-null="true"></property>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
package com.example.hibernate;
//import org.hibernate.cfg.Configuration;
import java.util.EnumSet;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
public class ExportDB {
public static void main(String[] args){
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
}
}
D:\WWW>java -cp .\WEB-INF\classes;c:\apache-tomcat-9.0.0.M4\lib\hibernate-core-5.1.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\jboss-logging-3.3.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\geronimo-jta_1.1_spec-1.1.1.jar;c:\apache-tomcat-9.0.0.M4\lib\dom4j-1.6.1.jar;c:\apache-tomcat-9.0.0.M4\lib\hibernate-commons-annotations-5.0.1.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\hibernate-jpa-2.1-api-1.0.0.Final.jar;c:\apache-tomcat-9.0.0.M4\lib\mysql-connector-java-6.0.2-bin.jar;c:\apache-tomcat-9.0.0.M4\lib\classmate-1.3.0.jar com.example.hibernate.ExportDB
四月 27, 2016 11:27:13 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
四月 27, 2016 11:27:13 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
四月 27, 2016 11:27:13 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
四月 27, 2016 11:27:13 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces m
ay be removed at any time.
四月 27, 2016 11:27:13 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
四月 27, 2016 11:27:13 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be remove
d at any time.
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/test]
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****, autocommit=true}
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
四月 27, 2016 11:27:14 上午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Wed Apr 27 11:27:14 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if expl
icit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide tr
uststore for server certificate verification.
四月 27, 2016 11:27:14 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
四月 27, 2016 11:27:14 上午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Hibernate: drop table if exists myuser
Hibernate: create table myuser (id integer not null auto_increment, username varchar(20) not null, password varchar(255), createTime datetime, expireTime datetime, primary key (id))
四月 27, 2016 11:27:14 上午 org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2f16c6b3'
package com.example.hibernate;
import java.util.Date;
public class MyUser {
private int Id;
private String username;
private String password;
private Date createtime;
private Date expiretime;
public int getId() {
return this.Id;
}
public void setId(int id) {
this.Id = id;
}
public String getusername() {
return this.username;
}
public void setusername(String username) {
this.username = username;
}
public String getpassword() {
return this.password;
}
public void setpassword(String password) {
this.password = password;
}
public Date getcreateTime() {
return createtime;
}
public void setcreateTime(Date createtime) {
this.createtime = createtime;
}
public Date getexpireTime() {
return expiretime;
}
public void setexpireTime(Date expiretime) {
this.expiretime = expiretime;
}
}
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page import="org.hibernate.Session" %>
<%@ page import="org.hibernate.SessionFactory" %>
<%@ page import="org.hibernate.service.ServiceRegistry" %>
<%@ page import="org.hibernate.boot.registry.StandardServiceRegistryBuilder" %>
<%@ page import="org.hibernate.boot.MetadataSources" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.example.hibernate.MyUser" %>
<%
ServiceRegistry sr = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();
Session ss = null;
try {
ss = sf.openSession();
ss.beginTransaction();
MyUser u = new MyUser();
u.setId(1);
u.setusername("user01");
u.setpassword("passwd01");
u.setcreateTime(new Date());
u.setexpireTime(new Date());
ss.save(u);
ss.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
ss.getTransaction().rollback();
}finally {
if (ss != null) {
if (ss.isOpen()) {
ss.close();
}
}
if (sf != null) {
sf.close();
}
}
%>