hibernate 访问 mysql demo

2017-08-27

90

0

技术:hibernate

运行环境:IDEA 15.2 + jdk8 + windows 10+ hibernate5.2.10.Final

demo功能:提供一个java通过hibernate访问mysql基础操作

前提条件: 准备mysql 运行实例一个, 初始化数据库sql

添加maven依赖

<!--hibernate start-->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.10.Final</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.2.10.Final</version>
</dependency>
<!--hibernate end-->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

编写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>
        <!-- 方言类 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <!--连接MySQL的用户名 -->
        <property name="connection.username">root</property>
        <!--连接MySQL的密码 -->
        <property name="connection.password">123456</property>
        <!--连接MySQL的驱动类名 -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="current_session_context_class">thread</property>
        <!--以格式良好的方式显示SQL语句 -->
        <property name="format_sql">true</property>
        <!--显示SQL语句 -->
        <property name="show_sql">false</property>
        <!-- 表结构更新机制 -->
        <property name="hbm2ddl.auto">update</property>
        <mapping class="com.demoworld.entities.tbBank"/>
    </session-factory>
</hibernate-configuration>

编写基本Dao访问抽象

1. HibernateSessionFactory  --数据库访问Session工厂, 负责打开一个数据库会话,打开后方可进行增删改查。完事儿后记得关闭

2. IBaseDao  --访问db接口操作, 比如增、删、改、查单个、查多个、其他查询

3. AbstractBaseDao  --IBaseDao实现的抽象访问, 具体的访问都继承这个

4. BankDao.java

public class BankDao extends AbstractBaseDao<tbBank> {
}

编写实体类,打hibernate注解,让hibernate可以认识这个是实体类

@Table(name = "tb_bank")
@Entity
public class tbBank {
    @Id
    private String userid;
    private String username;
    private double balance;

    ....get set 自己生成下, 不知道的百度
}

测试添加对象

private BankDao dao = new BankDao();
@Test
public void add() {
    tbBank tmp = new tbBank();
    tmp.setBalance(Double.parseDouble(System.currentTimeMillis() % 10000 + "." + System.currentTimeMillis() % 56000));
    tmp.setUserid(UUID.randomUUID().toString());
    tmp.setUsername("francis");
    dao.add(tmp);
    dao.close();
    System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(tmp));
}

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think