具体步骤:
展示一下我的项目列表(其中常用jar包主要是为了导入连接数据库的dbutils,我为了方便一起导入)
1、新建Project(此处以创建web项目为例)
勾选Web Application + Hibernate
同时勾选 ”Create default hibernate configuration and main class” (当然也可以不勾选,在项目创建好后再手动创建也是一样的)
2.点击configure选择如下所示,并完成创建:
3.配置数据库(我的是mysql,在连接数据库之前,需创建好数据库)
找到Database,点击如图所示 ,点击“Test Connection”测试连接成功,点击“Apply”,点击OK 连接成功
4.配置hibernate.cfg.xml(具体功能有注释):
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置数据库信息-->
<property name="connection.url">jdbc:mysql:///test_hibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- 第二部分,配置hibernate信息 -->
<!--输出底层sql语句-->
<property name="hibernate.show_sql">true</property>
<!--输出底层语句格式-->
<property name="hibernate.format_sql">true</property>
<!--hibernate帮忙创建表,配置之后update帮我们更新(有表更新,没表创建)-->
<property name="hbm2ddl.auto">update</property>
<!--配置数据库方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--第三部分:把映射文件放到核心配置文件中-->
<mapping class="customer.Customer"/>
</session-factory>
</hibernate-configuration>
5.hibernate支持直接使用注解的形式,在实体类进行添加相应的注解,可以不用创建映射文件,我这里使用注解,但是也展示映射文件(需要注意的是如果使用注解需要改动一些东西)
1.hibernate核心配置文件变化
<mapping resource="customer/customer.hbm.xml"/>
改成
<mapping class="customer.Customer"/>
customer.hbm.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--配置表和实体类的对应
class标签
name属性:实体类全路径
table属性:数据库表名称
-->
<class name="customer.Customer" table="customer">
<!--配置实体类id和表id对应
hibernate要求实体类有一个属性唯一值
-->
<!--id标签
name属性:实体类里面的id属性名称
column属性:生成表字段名称
-->
<id name="id" column="id">
<!--设置数据库自动增长的唯一主键-->
<generator class="native"></generator>
</id>
<property name="username" column="username"/>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
</class>
</hibernate-mapping>
Customer实体类配置:
@Entity
@Table(name = "customer")
public class Customer {
@Id // 表示主键
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator") // 自增长
@Column(name = "id") // 类属性对应着表字段
private int id;
private String username;
private String password;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
测试类:
public class test {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Customer customer = new Customer();
customer.setUsername("AT&T");
customer.setPassword("123456");
customer.setAddress("美国");
session.save(customer);
tx.commit();
session.close();
}
}
测试结果:
数据库自动创建的表:
这里结束之后自己依然是有一个疑问,希望大家会的给出解答:
hibernate自动生成表格内容的类型尺寸显然不合理,可能是有什么属性没有设置还是其他问题?