Hibernate对数据库操作(完成作业)
首先建立maven工程
1.配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.wxy</groupId> <artifactId>hibernatefirst</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.1.Final</version> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> </dependencies> </project>
2.配置emp.hbn.xml
id是配置主键
property是配置非主键
id,property后面的name是实体类中该属性的名称,column是数据库中该属性的名称
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="test" > <!--设置主键--> <class name="test.EmpEntity" table="emp"> <id name="firstName" column="first_name" type="java.lang.String"> </id> <property name="empid" type="java.lang.Integer"> <column name="empid"/> </property> <property name="salary" type="java.lang.Integer"> <column name="salary"></column> </property> </class> </hibernate-mapping>
3.配置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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/mybatis</property> <property name="connection.password">2999029aaa</property> <property name="connection.username">root</property> <!--hibernate所使用数据库的方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--执行操作是是否在控制台打印SQL--> <property name="show_sql">true</property> <!--是否对SQL格式化--> <property name="format_sql">true</property> <!--指定自动生成数据表的策略--> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="test.EmpEntity"/> <!--指定关联的hbn.xml--> <mapping resource="emp.hbn.xml"/> </session-factory> </hibernate-configuration>
4.配置实体类EmpEntity.java
这些不是自己写的可以自动生成
package test; import javax.persistence.*; @Entity @Table(name = "emp", schema = "mybatis", catalog = "") public class EmpEntity { private Integer empid; private String firstName=null; private Integer salary; @Basic @Column(name = "empid") public Integer getEmpid() { return empid; } public void setEmpid(Integer empid) { this.empid = empid; } @Id @Column(name = "first_name") public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Basic @Column(name = "salary") public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this.salary = salary; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EmpEntity empEntity = (EmpEntity) o; if (empid != null ? !empid.equals(empEntity.empid) : empEntity.empid != null) return false; if (firstName != null ? !firstName.equals(empEntity.firstName) : empEntity.firstName != null) return false; if (salary != null ? !salary.equals(empEntity.salary) : empEntity.salary != null) return false; return true; } @Override public int hashCode() { int result = empid != null ? empid.hashCode() : 0; result = 31 * result + (firstName != null ? firstName.hashCode() : 0); result = 31 * result + (salary != null ? salary.hashCode() : 0); return result; } @Override public String toString() { return "EmpEntity{" + "empid=" + empid + ", firstName='" + firstName + '\'' + ", salary=" + salary + '}'+'\n'; } }
点击右侧database -> 点击'+' -> 点击datesource -> 选择你的数据库的类型
填上Name User Password的信息 ,然后点击 Test Connection
点击view -> Tool Windows ->Persistence
这时会产生
找到database schema 并点击
选择1)choose data source
2)选择package
3)选择要生成实体类的表,这里我选择了emp,
在下方勾选上add to session factory , generate JPA Annotations
点击OK,就自动生成了实体类了。
5.配置测试类
package com; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistryBuilder; import test.EmpEntity; import javax.imageio.spi.ServiceRegistry; import java.io.IOException; import java.lang.reflect.Array; import java.util.Arrays; import java.util.List; public class HibernateUtil { public static void main(String[] args)throws IOException { //Configuration类负责管理Hibernate的配置信息 Configuration configuration=new Configuration().configure("/hibernate.cfg.xml"); SessionFactory sessionFactory=configuration.buildSessionFactory(); Session session=sessionFactory.openSession(); SQLQuery query=session.createSQLQuery("select * from emp where salary>5000"); List<Object[]> list =query.list(); for(Object[] o:list) { System.out.println(Arrays.toString(o)); } session.close(); sessionFactory.close(); } }
实验结果