初学hibernate(1)——使用hibernate annotations注解实体
初学hibernate(一)——使用hibernate annotations注解实体
首先,这个demo是使用maven来管理项目的。数据库是使用mysql。首先,使用eclipse建立一个maven的web项目。如果没有maven,可以建一个普通的动态web项目。
一、配置文件
1、maven的pom.xml文件的内容如下,如果没有使用maven,可以自己手动下载pom.xml中的相应版本的依赖包,当然,junit可以不下。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.kentop</groupId> <artifactId>ktui</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ktui Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.9.0.GA</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.11</version> </dependency> <dependency> <groupId>hibernate-annotations</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.3.0.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>3.3.0.ga</version> </dependency> </dependencies> <build> <finalName>ktui</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>
2、该项目的hibernate配置文件在maven建立的web项目中的路径如下:
其中,hibernate配置文件的内容如下:
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/ktui</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <!--这里是要映射的实体--> <mapping class="net.kentop.ktui.entity.AtmTypes"/> </session-factory> </hibernate-configuration>
二、实体
这里的demo使用的实体内容如下所示:
package net.kentop.ktui.entity; import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="atm_types") public class AtmTypes implements Serializable{ /** * serialVersionUID */ private static final long serialVersionUID = -1556433468904091584L; /** * 主键 */ @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; /** * 应用类型id */ @Column(name="atmtype_id",nullable=false) private String atmTypeId; /** * 应用类型名称 */ @Column(name="atmtype_name",nullable=false) private String atmTypeName; /** * 应用类型图标路径 */ @Column(name="atmtype_icon") private String atmTypeIcon; /** * 提示信息 */ @Column(name="toolTip") private String toolTip; /** * 扩展信息 */ @Column(name="settings") private String settings; /** * 显示次序 */ @Column(name="orders",nullable=false) private int index; /** * 创建日期 */ @Column(name="created_date",length=10,nullable=false) private int createdDate; /** * 创建人 */ @Column(name="created_user",length=10,nullable=false) private String createdUser; /** * 修改日期 */ @Column(name="revised_date",length=32,nullable=false) private int revisedDate; /** * 修改人 */ @Column(name="revised_user",length=32,nullable=false) private String revisedUser; public AtmTypes(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAtmTypeId() { return atmTypeId; } public void setAtmTypeId(String atmTypeId) { this.atmTypeId = atmTypeId; } public String getAtmTypeName() { return atmTypeName; } public void setAtmTypeName(String atmTypeName) { this.atmTypeName = atmTypeName; } public String getAtmTypeIcon() { return atmTypeIcon; } public void setAtmTypeIcon(String atmTypeIcon) { this.atmTypeIcon = atmTypeIcon; } public String getToolTip() { return toolTip; } public void setToolTip(String toolTip) { this.toolTip = toolTip; } public String getSettings() { return settings; } public void setSettings(String settings) { this.settings = settings; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public int getCreatedDate() { return createdDate; } public void setCreatedDate(int createdDate) { this.createdDate = createdDate; } public String getCreatedUser() { return createdUser; } public void setCreatedUser(String createdUser) { this.createdUser = createdUser; } public int getRevisedDate() { return revisedDate; } public void setRevisedDate(int revisedDate) { this.revisedDate = revisedDate; } public String getRevisedUser() { return revisedUser; } public void setRevisedUser(String revisedUser) { this.revisedUser = revisedUser; } }
其中,@Entity声明这个pojo是一个实体bean
@Table注解可以为实体bean映射指定表、目录(catalog)和schema的名字,如果没有定义@Table,则系统自动使用默认值:实体的短类名(不附带包名),name属性的值为对应在数据库中的表名。
@Id注解可以将实体bean中的某个属性定义为标识符,即主键。
@GeneratedValue注解定义标识符的生成策略。有以下4种策略
AUTO ——可以是indentity column列类型,或者sequence类型或者table类型,取决于不同的底层数据库
TABLE——使用表保存id值
IDENTITY ——indentity column
SEQUENCE ——sequence
@Column注解可以将属性映射到列,使用该注解来覆盖默认值,其中,name属性表示该属性映射的列的名字,length表示该列的长度。nullable表示是否可以为空,默认为true,不能为空。
三、工具类
package net.kentop.ktui.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); public static Session getSession(){ return sessionFactory.openSession(); } }
四、测试类
package net.kentop.ktui.manager; import net.kentop.ktui.entity.AtmTypes; import net.kentop.ktui.utils.HibernateUtil; import org.hibernate.Session; public class AtmTest { public static void main(String args[]){ saveAtmType(); } public static void saveAtmType(){ Session session = HibernateUtil.getSession(); session.beginTransaction(); AtmTypes types = new AtmTypes(); types.setAtmTypeName("dfsssdf"); types.setIndex(1); types.setAtmTypeId("dsfsfsfsfsf"); types.setCreatedDate(1212313111); types.setCreatedUser("user"); types.setRevisedDate(1212313111); types.setRevisedUser("user"); session.save(types); session.getTransaction().commit(); } }
运行该类,则会自动在数据库中建立对应的表,并且增加一条记录。