第一步导入依赖:
mysql的数据库驱动
hibernate连接是要的事务包
hibernate核心包:
测试类:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { SysUser user = new SysUser(); user.setId(7); user.setUsername("小明"); //使用Hibernate的API来完成将Customer信息保存到mysql数据库中的操作 Configuration conf = new Configuration().configure().addClass(SysUser.class); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); SessionFactory sessionFactory = conf.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession(); //相当于得到一个Connection //开启事务 session.beginTransaction(); //操作 session.save(user); //事务提交 session.getTransaction().commit(); session.close(); sessionFactory.close(); } }
对应的配置文件:SysUser.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" "hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.roadjava.studentroom.demo.hibernate_mysql"> <!-- name:即实体类的全名 table:映射到数据库里面的那个表的名称 catalog:数据库的名称 --> <class name="SysUser" table="hibernate" catalog="demo"> // 表示对应的类名 数据库对应的表名 数据库的名字 <!-- class下必须要有一个id的子元素 --> <!-- ID是用于描述主键的 --> <id name="id" column="id"> <!-- 主键生成策略 --> <!-- <generator class="native"></generator>--> </id> <!-- 使用property来描述属性与字段的对应关系 如果length忽略不写,且你的表是自动创建这种方案,namelength的默认长度是255 --> <property name="username" column="username" length="20"></property> </class> </hibernate-mapping>
实体类:一定在javax下面,
import javax.persistence.Table; @Entity//@Entity表示该类能被hibernate持久化 @Table(name="hibernate") public class SysUser { @Column(name="id") private Integer id; @Column(name="username") private String username; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "sysUser{" + "id=" + id + ", username='" + username + '\'' + '}'; } } hibernate核心配置文件: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> <!-- 配置关于数据库连接的四个项:driverClass url username password --> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 可以将向数据库发送的SQL语句显示出来 --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL语句 --> <property name="hibernate.format_sql">true</property> <!-- hibernate的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置hibernate的映射文件的位置 --> <mapping resource="com/roadjava/studentroom/demo/hibernate_mysql/SysUser.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
对应的数据库语句:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for hibernate
-- ----------------------------
DROP TABLE IF EXISTS `hibernate`;
CREATE TABLE `hibernate` (
`username` varchar(22) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '',
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of hibernate
-- ----------------------------
INSERT INTO `hibernate` VALUES ('小明', '2');
INSERT INTO `hibernate` VALUES ('小明', '7');