hibernate的第一次使用
作者:互联网
- 先资源下载hibernate-release-5.5.5.Final
- 新建一个maven项目quickstart
- 在pom.xml下添加依赖
<!-- hibernate依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.5.5.Final</version> </dependency> <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
- 在main和test文件夹下面建立resources文件,写两个一样的hibernate.cfg.xml文件,还需要在mysql中创建一个数据库
<?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 name="foo"> <!-- 初始化JDBC连接 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate</property> //数据库名 ///三个斜杠中间//(这个是默认端口号)(localhost:3306)/ <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 关联对象配置文件 --> <mapping resource="com/uestc/User.hbm.xml"/> </session-factory> </hibernate-configuration>
- 在src最下面一级目录下面新建一个实体类User
public class User implements Serializable { private Integer id; private String name; private String pwd; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
- 在src最下面一级目录下面新建一个User.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="com.uestc.User" table="user"> //如果table="user",column="name",column="pwd"等字样变红,需要加入database,在点击Tool Windows下面的Persistence,在再爆红的字符串加上alt+enter <!-- 主键自增策略 --> <id name="id"> <generator class="native"/> </id> <!-- 属性映射 --> <property name="name" column="name"/> <property name="pwd" column="pwd"/> </class> </hibernate-mapping>
- 在test最下面的一级目录下面建立一个HibernateTest类
public class HibernateTest { @Test public void testInit(){ StandardServiceRegistry registry = null; SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try{ //5.X版本的写法 //初始化注册服务对象 registry = new StandardServiceRegistryBuilder() .configure() // 默认加载hibernate.cfg.xml配置文件,如果配置文件名称被修改,.configure("修改的名称") .build(); //从元信息获取Session工厂 sessionFactory = new MetadataSources(registry) .buildMetadata() .buildSessionFactory(); //从工厂创建Session连接 session = sessionFactory.openSession(); //开启事务 tx = session.beginTransaction(); //创建实例 User user = new User(); user.setName("zhangsan"); user.setPwd("123"); session.save(user); //提交事务 tx.commit(); }catch (Exception e){ //回滚事务 tx.rollback(); }finally { if(session != null && session.isOpen()) session.close(); } } }
- 点击run运行报错,最后在再pom.xml文件中添加一个配置
<build> <!-- 读取配置文件 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
- 最后执行完成后,没有报错,就可以去数据库中查看到增加的信息了。
标签:hibernate,name,第一次,pwd,session,使用,public,String 来源: https://www.cnblogs.com/mhy123/p/16217447.html