[Eclipse]Hibernate环境搭建

    xiaoxiao2022-07-02  119

    我的环境:JDK1.7+MySQL5.7 工具:Eclipse

    第一步:下载包

    https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/

    这个网址已经更新了最好在csdn里找一找5.0.7版本的保持版本一致

    第二步:创建数据库

    sql语句↓

    /* SQLyog Ultimate v8.32 MySQL - 5.5.27 : Database - hbtest ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`hbtest` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `hbtest`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `uid` bigint(100) NOT NULL AUTO_INCREMENT, `username` varchar(25) DEFAULT NULL, `password` varchar(25) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `user` */ /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

    第三步:创建项目导入jar包

    创建项目

    右键创建Dynamic Web project,版本选择2.5

    找到第一步下载的压缩包解压进入路径\hibernate-release-5.0.7.Final\lib\required

    复制里面全部包导入到项目lib目录下

    同时也需要导入

    log4j-1.2.16.jar

    mysql-connector-java-5.1.7-bin.jar

    slf4j-api-1.6.1.jar

    slf4j-log4j12-1.7.2.jar

    这4个包

    结果如图

    第四步:创建实体类JAVABeanPOJO

    路径如上

    user的代码↓

    public int uid; public String username; public String password; @Override public String toString() { return "user [uid=" + uid + ", username=" + username + ", password=" + password + "]"; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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; }

    第四步:创建对应user的映射文件

    路径文件名如下

    文件配置信息

    按着Ctrl键不放鼠标悬浮到“cn.len.test.user”检查地址是否被编译到了

    <?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.len.test.user" table="user"> <id name="uid" column="uid"> <generator class="native"/> </id> <property name="username" column="username"/> <property name="password" column="password"/> </class> </hibernate-mapping>

    第五步:创建hibernate核心文件

    注意是在src文件夹下创建核心文件

    核心文件内容

    <?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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hbtest</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 配置Hibernate的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置===== --> <!-- 打印sql到控制台上 --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL --> <property name="hibernate.format_sql">true</property> <!-- 先判断是否存在表再自动创建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="cn/len/test/user.hbm.xml" /> </session-factory> </hibernate-configuration>

    按着Ctrl键不放鼠标悬浮到"cn/len/test/user.hbm.xml"检查地址是否被编译到了

    第六步:工具类

    代码

    package cn.len.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { public static final Configuration cfg; public static final SessionFactory sf; static{ cfg = new Configuration().configure(); sf = cfg.buildSessionFactory(); } public static Session openSession() { return sf.openSession(); } }

    第七步:测试类

    package cn.len.test; import org.hibernate.Session; import org.hibernate.Transaction; public class UserTest { /** * @param args */ public static void main(String[] args) { Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); user u = new user(); u.setUsername("len"); u.setPassword("123"); session.save(u); tx.commit(); session.close(); } }

    看到这个恭喜你成功了!

    删除:

    session.delete(u);

    查询:

    session.get("user.class",u.getUid()); System.out.println(u);

    更改

    user u = new user(); u.setUsername("len3"); u.setPassword("123"); session.update(u);

     

    最新回复(0)