技术:hibernate
运行环境:IDEA 15.2 + jdk8 + windows 10+ hibernate5.2.10.Final
demo功能:提供一个java通过hibernate访问mysql基础操作
前提条件: 准备mysql 运行实例一个, 初始化数据库sql
org.hibernate
hibernate-core
5.2.10.Final
mysql
mysql-connector-java
5.1.39
org.hibernate
hibernate-c3p0
5.2.10.Final
com.google.code.gson
gson
2.8.1
junit
junit
4.12
org.hibernate.dialect.MySQLDialect
jdbc:mysql://localhost:3306/test
root
123456
com.mysql.jdbc.Driver
thread
true
false
update
1. HibernateSessionFactory --数据库访问Session工厂, 负责打开一个数据库会话,打开后方可进行增删改查。完事儿后记得关闭
2. IBaseDao --访问db接口操作, 比如增、删、改、查单个、查多个、其他查询
3. AbstractBaseDao --IBaseDao实现的抽象访问, 具体的访问都继承这个
4. BankDao.java
public class BankDao extends AbstractBaseDao {
}
@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));
}
欢迎添加微信,互相学习↑↑↑ -_-
白老虎
programming is not only to solve problems, ways to think
grafana 级连 菜单 templating (variables) 配置
rocketmq 集群搭建 (2master + 2slave + 2namesrv)
AI 机器人 抓取 微信 聊天中的 百度网盘 分享地址和密码