美文网首页
重拾Hibernate(1) 快速搭建

重拾Hibernate(1) 快速搭建

作者: 心扬 | 来源:发表于2019-02-03 14:48 被阅读0次

引入依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.14</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.1.Final</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
</dependency>

创建持久化类

@Data
@Entity
@Table(name = "t_news")
public class News {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String title;

    private String content;
}
  • @Entity 注解生命该类是一个持久化类
  • @Table 指定该类映射的表,name属性指定表名
  • @Id 指定类的标识属性,即可以唯一标识该对象的属性,通常映射到表中的主键列
  • @GeneratedValue 指定主键的生成策略,strategy属性指定主键的生成策略为IDENTITY

配置hibernate.cfg.xml

<!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="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>

        <!-- hibernate基本信息 -->
        <!-- hibernate数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>

        <!-- 执行操作时控制台是否要打印SQL -->
        <property name="show_sql">true</property>
        <!-- 是否对SQL进行格式化 -->
        <property name="format_sql">true</property>
        <!-- 自动生成表的策略 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 所有的持久化类  -->
        <mapping class="io.q.model.News"/>
        
    </session-factory>
</hibernate-configuration>

测试程序

public class NewsTest {

    private static StandardServiceRegistry registry;

    private static SessionFactory sessionFactory;

    private Session session;

    private Transaction transaction;

    @BeforeClass
    public static void init(){
        registry = new StandardServiceRegistryBuilder().configure().build();
        sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
    }

    @Before
    public void setUp(){
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }


    @After
    public void tearDown(){
        transaction.commit();
        session.close();
    }


    @AfterClass
    public static void destroy(){
        sessionFactory.close();
        StandardServiceRegistryBuilder.destroy(registry);
    }

    @Test
    public void saveNews(){
        News news = new News();
        news.setTitle("重拾hibernate");
        news.setContent("重拾hibernate快速入门程序");
        session.save(news);
    }
}

执行日志

image.png

执行结果

image.png

相关文章

网友评论

      本文标题:重拾Hibernate(1) 快速搭建

      本文链接:https://www.haomeiwen.com/subject/dkkysqtx.html