1.建一个web项目。
2.建一个自己的包
将你所有有关hibernate的包放到里面。切记入包的时候一定不要忘记这个。数据库有关的包。
3.在src中 编写一个类Customer
里面包含基本信息和set(),get()方法。
4.数据库里建一张对应的表
5.在Customer类同一个包里面建一个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="cn.itcast.domain.Customer" table="customer" >
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="sex" column="sex" type="string"/>
<property name="age" column="age" type="int"/>
<property name="city" column="city" type="string"/>
</class>
</hibernate-mapping>
6.在src下面建一个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是连接配置文件的根元素 -->
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost/hibernate_1</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">123456</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->
<mapping resource="cn/itcast/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7.下面我们建一个测试类来向数据库里插入信息。
package cn.itcast.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.itcast.domain.Customer;
public class CustomerTest {
public static void insertTest () {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Customer c = new Customer();
c.getName();
c.setName("冯宝宝");
c.setAge(20);
c.setCity("成都");
c.setSex("女");
session.save(c);
t.commit();
session.close();
sessionFactory.close();
}
public static void main(String[] args){
insertTest();
}
}
8.执行一下,到数据库查看结果。
当当当当,大功告成。
9.问题与总结。
(1).没有导入数据库的包,不过问题不太严重。根据错误信息买上改过来就好了。
(2).int&integer?????请参考https://www.cnblogs.com/guodongdidi/p/6953217.html