Hibernate注解的作用是不要另外再写与实体类关联的映射xml文件
第一步:导入Hibernate 和 数据库 的Jar包至项目中WebRoot/WEB-INF/lib
第二步:在项目中的src目录写hibernate.cfg.xml(与数据库创建连接在这实现)
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/chose?characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property><!--后台显示sql语句,fail为不显示sql语句-->
<mapping resource="com/hib/test/Course.xml" /><!-- 映射与实体类关联的xml文件 -->
<mapping class="com.hib.test.User" /[color=red]><!-- 映射注解的实体类-->[/color] </session-factory>
</hibernate-configuration>
第三步:创建实体类
与数据库关联的类叫做实体类
package com.hib.test;
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
第四步:在创建的实体类中使用hibernate注解
package com.hib.test;
[color=red]import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//hibernate注解[/color]
[color=red]@Entity
@Table(name = "t_user")[/color]
public class User {
private Long id;
private String username;
private String password;
[color=red]@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键自增的方式[/color]
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
[color=red]@Column(name = "username")[/color]
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
[color=red]@Column(name = "password")[/color]
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注意:
1、如果在实体类的类名和关联的数据库表名相同,注解可写成:
[color=red]@Entity
@Table[/color]
2、如果在实体类声明的属性与所要关联数据库表的字段相同,注解可写成:
@Column
第五步:在hibernate.cfg.xml中映射实体类
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/chose?characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property><!--后台显示sql语句,fail为不显示sql语句-->
<mapping resource="com/hib/test/Course.xml" /><!-- 映射与实体类关联的xml文件 -->
[color=red]
<mapping class="com.hib.test.User" /><!--- 映射注解的实体类-->[/color] </session-factory>
</hibernate-configuration>
第一步:导入Hibernate 和 数据库 的Jar包至项目中WebRoot/WEB-INF/lib
第二步:在项目中的src目录写hibernate.cfg.xml(与数据库创建连接在这实现)
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/chose?characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property><!--后台显示sql语句,fail为不显示sql语句-->
<mapping resource="com/hib/test/Course.xml" /><!-- 映射与实体类关联的xml文件 -->
<mapping class="com.hib.test.User" /[color=red]><!-- 映射注解的实体类-->[/color] </session-factory>
</hibernate-configuration>
第三步:创建实体类
与数据库关联的类叫做实体类
package com.hib.test;
public class User {
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
第四步:在创建的实体类中使用hibernate注解
package com.hib.test;
[color=red]import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//hibernate注解[/color]
[color=red]@Entity
@Table(name = "t_user")[/color]
public class User {
private Long id;
private String username;
private String password;
[color=red]@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键自增的方式[/color]
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
[color=red]@Column(name = "username")[/color]
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
[color=red]@Column(name = "password")[/color]
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注意:
1、如果在实体类的类名和关联的数据库表名相同,注解可写成:
[color=red]@Entity
@Table[/color]
2、如果在实体类声明的属性与所要关联数据库表的字段相同,注解可写成:
@Column
第五步:在hibernate.cfg.xml中映射实体类
<?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="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/chose?characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property><!--后台显示sql语句,fail为不显示sql语句-->
<mapping resource="com/hib/test/Course.xml" /><!-- 映射与实体类关联的xml文件 -->
[color=red]
<mapping class="com.hib.test.User" /><!--- 映射注解的实体类-->[/color] </session-factory>
</hibernate-configuration>