spring中集成hibernate
1.hibernate框架是什么?
(1)hibernate对jdbc进行的封装
(2)开源的轻量级框架。
2.hibernate思想:ORM(对象关系映射)
(1)让实体类和数据库中的表一一对应(表和实体类对应、实体属性和表字段对应)
(2)不直接操作数据库表,而是直接操作实体类
3.搭建hibernate
(1)pom中的依赖
<!-- 关系型数据库整合时需配置 如hibernate jpa等 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.1.RELEASE</version> </dependency> <!-- Hibernate 依赖配置 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.0.CR2</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.4.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.3.0.CR2</version> </dependency>
(2).hibernate的核心配置文件
<context:property-placeholder location="classpath:/config.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSource"/> <property name="packagesToScan"> <list> <value>pojo</value> </list> </property>
<!--
<property name="mappingResources">
<list>
<value>xml文件位置</value> </list>
</property>
--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop> </props> </property> </bean>
(3)映射文件(若使用注解和不适用配置文件)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pojo.User" table="user" > <id name="id" column="name" >
<generator class="native"/>
</id>
<property name="name" column="name" />
<property name="age" column="age"/>
</class>
</hibernate-mapping>
(4).实体
/***
* hibernate对实体类的要求
* (1)属性必须是private 且有public的get和set方法
* (2)必须有唯一标识属性来表示实体类
* (3)属性的类型建议用包装类型
* hibernate实体类有三种状态:
* (1)瞬时态:没有id,与session没有关系
* (2)持久态:有id,与session有关系
* (3)托管太:有id,和session无关
*/
@Entity
@Table (name = "user")
public class User {
/**
* id作为主键,主键有很多生成策略
* (1)native自增长:根据选择的数据库自动选择主键的生成方式
* (2)uuid:字段用String类型
* (3)identity 需要数据库支持自增长,mysql支持,oracle不支持
*/
@Id
@GeneratedValue
@Column (name = "id")
private int id;
@Column (name = "name", nullable = false, length = 100)
private String name;
@Column (name = "age", nullable = false)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}