可以使用jpa生成数据库表
persistence.xml
注意<property name="hibernate.hbm2ddl.auto" value="update" />
这个是自动生成表的
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Test {
public void createTable() {
// 可以验证生成表是否正确
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("wechat");
factory.close();
}
public static void main(String[] args) {
Test t =new Test();
t.createTable();
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="wechat" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.wc.bean.OfUser</class>
<class>com.wc.bean.WcUser</class>
<class>com.wc.bean.WcLoginInfo</class>
<class>com.wc.bean.WcMessages</class>
<class>com.wc.bean.WcMusic</class>
<class>com.wc.bean.WcFile</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://182.254.155.153:3306/hcapi?useUnicode=true&characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password"
value="密码" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
注意<property name="hibernate.hbm2ddl.auto" value="update" />
这个是自动生成表的