![](https://img.haomeiwen.com/i10186693/a6030aa705c56e76.png)
1.什么是ORM
![](https://img.haomeiwen.com/i10186693/01777fa2559560c6.png)
![](https://img.haomeiwen.com/i10186693/2205c591c544f4fa.png)
![](https://img.haomeiwen.com/i10186693/9f97ef16d3f3ec9b.png)
![](https://img.haomeiwen.com/i10186693/c73bf1e95f54fdd7.png)
Hibernate简介
![](https://img.haomeiwen.com/i10186693/b012a02a235b9631.png)
![](https://img.haomeiwen.com/i10186693/0497072cf8ed600e.png)
![](https://img.haomeiwen.com/i10186693/508b65e03be43650.png)
![](https://img.haomeiwen.com/i10186693/45e61b133e22525c.png)
Help/Install New Software/Add/Archive/
查看是否安装成功:New/Other/Hibernate
![](https://img.haomeiwen.com/i10186693/ecdce82bc250b30b.png)
![](https://img.haomeiwen.com/i10186693/9397f5ce3b01d333.png)
(少一个mysql包,添加如下:)
mysql-connector-java.jar包的下载教程
导入Jar包:
JAVAEE/Windows/Preferences/Java/Build Path/User Libraries/New&Add External JARS
![](https://img.haomeiwen.com/i10186693/94c2b2890ef4ab9c.png)
然后Demo/Properties/Build Path/Libraries/Add Library/User Library/勾选三个文件/Finish
![](https://img.haomeiwen.com/i10186693/3ff3b4f8fb83a702.png)
创建Hibernate的配置文件
![](https://img.haomeiwen.com/i10186693/b0ce823da6c9e48e.png)
Demo/sec/New/Other/Hibrenate/H C(cfg.xml)/放在src内/Next/Finish
如果没有出现代码自动提示,需要手动导入dtd文档。
![](https://img.haomeiwen.com/i10186693/dde4b20703aac878.png)
![](https://img.haomeiwen.com/i10186693/ef08674d4674d370.png)
JavaEE/Source/Format(对齐)
![](https://img.haomeiwen.com/i10186693/70b37225f30ce511.png)
这里出现了一个spell错误,但实际没错,解决办法:
Windows/Preferences/输入spell/去掉勾选Enable spell checking/OK
创建持久化类
新建一个类:
Demo/src/New/Class/Students(遵循javabeans的设计原则)
1.公有的类
2.提供公有的不带参数的默认的构造函数
3.属性私有
4.属性setter/getter封装
![](https://img.haomeiwen.com/i10186693/eb4f27365d7be392.png)
(Date需要导入)
不带参数的构造方法:
public students(){}
为了方便生成对象:
右键/Source/Generate Constructor using Fields/OK/去掉super();
用setter/getter进行封装:
右键/Source/Generate Getters and Setters/Select All/OK
为了方便测试,写个toString()方法:
右键/Source/Generate toString()
格式化一下代码:
菜单栏/Source/Format
创建对象-关系映射文件
Demo/src/New/Other/Hibernate/HXM(hbm.xml)/Next/Next/找到需要持久化的学生类/Finish
![](https://img.haomeiwen.com/i10186693/497642779e21617f.png)
hbm.xml文档生成之后需要回到cfg.xml配置文档当中加一个mapping标签
![](https://img.haomeiwen.com/i10186693/119fa6c0212d653a.png)
新建数据库
打开Navicat/某个Demo右键/新建数据库/
![](https://img.haomeiwen.com/i10186693/cec11408cf1b071b.png)
Junit进行测试
![](https://img.haomeiwen.com/i10186693/cf676bcfe286c4e8.png)
执行顺序:Before-Test-After
操作:
Demo/New/Source Folder/test(Folder name)/Finish
//创建测试类
test/New/Class/StudentsTest
![](https://img.haomeiwen.com/i10186693/a534877266c6919c.png)
Alt+Shift+O
通过Hibernate API编写访问数据库的代码
![](https://img.haomeiwen.com/i10186693/c6a370940620b5e8.png)
初始化和释放资源
![](https://img.haomeiwen.com/i10186693/a395b6e92a1d4d8c.png)
![](https://img.haomeiwen.com/i10186693/2395dbb10782e7e6.png)
把学生对象保存到数据库表里面
![](https://img.haomeiwen.com/i10186693/c510bc79f8ada623.png)
JUnit测试
![](https://img.haomeiwen.com/i10186693/f1e08b4db49da9f6.png)
网友评论