解决不了bug,那就解决自己,配置SSH代码,如果对你有帮助,点赞关注,成为朋友。
安装好后–建表
1):建一个数据库[salary]
create database salary;
2)建表并插入数据
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for teacherinfo
-- ----------------------------
DROP TABLE IF EXISTS `teacherinfo`;
CREATE TABLE `teacherinfo` (
`tno` int(5) NOT NULL,
`teachername` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(16) NULL DEFAULT NULL,
`sex` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`title` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`tno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of teacherinfo
-- ----------------------------
INSERT INTO `teacherinfo` VALUES (1, '张三', 40, '女', '讲师');
INSERT INTO `teacherinfo` VALUES (2, '李四', 48, '女', '教授');
INSERT INTO `teacherinfo` VALUES (3, '王五', 39, '男', '副教授');
INSERT INTO `teacherinfo` VALUES (4, '赵六', 55, '男', '教授');
INSERT INTO `teacherinfo` VALUES (5, '孙七', 50, '女', '副教授');
SET FOREIGN_KEY_CHECKS = 1;
对数据库的命令有不熟悉的可参考数据库的命令
这些jar包放在微信公众号:新白者,回复hibernate,可以领取hibernate的jar包,回复数据库,可以领取连接数据的jar包
放在lib下:
在Eclipse菜单项中选择Windows->Show View->Data Source Explorer
点击->新建–选择MySQL–next–填写信息–点击测试[Test Connection]–弹出Ping succeeded【说明测试成功】
1)、安装hibernate插件
help–Eclipse Marketplace–find【输入hibernate tools】–安装
安装好后出现这个界面:
选择自己需要的就行,不要全勾上,不然速度就慢了,静等安装。
然后发现报错>
An error occurred while collecting items to be installed session
context was:(profile=epp.package.jee,
phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=,
action=).
解决了几个小时,网上的办法都试了还是没能解决,被这个bug弄烦了,我就放弃了,不用这个hibernate tools,自己一个个敲代码。
也遇到了报错,一步步分析出来了,配置hibernate所遇到的几个报错,分别解析如何解决
这些jar包这个在微信公众号:新白者,回复hibernate,可以领取hibernate的jar包,回复数据库,可以领取连接数据的jar包
包的命名
package com.lwc.entity;
public class user {
public String tno;
public String teachername;
public Integer age;
public String sex;
public String title;
public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Teacherinfo [tno=" + tno + ", teachername=" + teachername + ", age=" + age + ", sex=" + sex + ", title="
+ title + "]";
}
}
在com.lwc.entity下新建
然后找到other(其它),在xml里点击next(下一步)
输入user.hbm.xml,点击完成
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 1、配置类和表对应 class标签
name属性:实体类全路径
table属性:数据库表名称 -->
<class name="com.lwc.entity.user" table="teacherinfo">
<!-- 2、配置实体类id和表的id对应 hibernate要求实体类有一个 属性唯一值 hibernate要求表有字段作为唯一值 -->
<!-- id标签 name属性:实体类里面id属性名称 column属性:生成的表的字段名称 -->
<id name="tno" column="tno">
<!-- native:生成表的id值就是主键自动增长 -->
<!-- <generator class="native"></generator> --></id>
<property name="teachername" column="teachername"> </property>
<property name="age" column="age"> </property>
<property name="sex" column="sex"> </property>
<property name="title" column="title"> </property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/salary?characterEncoding=UTF-8&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/lwc/entity/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.lwc.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.jupiter.api.Test;
import com.lwc.entity.user;
public class hibernateTest {
@Test
public void testAdd() {
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
user us = new user();
us.setTno("12");
us.setTeachername("新白");
us.setAge(18);
us.setSex("男");
us.setTitle("教授");
session.save(us);
tx.commit();
session.close();
sessionFactory.close();
}
}