A-Hibernate-Maven创建
1.pom.xml
<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>com.iflytek.lhq</groupId>
<artifactId>hibernate03</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hibernate03 Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<!-- jar包私服路径 -->
<repository>
<id>central-repos</id>
<name>central Releases</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件私服路径 -->
<pluginRepository>
<id>plugin-repos</id>
<name>Team Nexus Repository</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- JSTL及其依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<type>jar</type>
</dependency>
<!-- servle和JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.1</version>
</dependency>
<!--hibernate start -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!-- java持久化jar,hibernate依赖该jar -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.0.GA</version>
</dependency>
<!-- hibernate end -->
<!-- log4j日志处理 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- slf4j日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<!-- log连接工具,在运行时搭配调用者喜好的日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
</dependencies>
<!-- 版本配置信息 -->
<properties>
<jdk.version>1.8</jdk.version>
<jetty.version>7.6.14.v20131031</jetty.version>
<webserver.port>8181</webserver.port>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<!--打包的包名,myweb.war -->
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Compiler编译插件,设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!-- jetty插件 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<connectors>
<connector
implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${webserver.port}</port>
</connector>
</connectors>
<webAppConfig>
<contextPath>/${project.artifactId}</contextPath>
</webAppConfig>
<systemProperties>
<systemProperty>
<name>org.mortbay.util.URI.charset</name>
<value>${project.build.sourceEncoding}</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>hibernate03</display-name>
</web-app>
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>
<!-- 配置数据库信息,使用hibernate自带连接池,不稳定可以使用c3p0 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">10</property>
<!-- 配置hibernate信息 -->
<!-- 指定数据库方言,如MySQL,Oracle等,不同数据库的数据类型,语法稍有差别, hibernate将其转换成能理解的语句,MySQL分页:limit,Oracle分页:rownum -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 将session绑定到当前线程,确保session是单线程 -->
<property name="current_session_context_class">thread</property>
<!-- 不使用二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- 是否显示sql -->
<property name="show_sql">true</property>
<!-- 格式化sql -->
<property name="format_sql">true</property>
<!-- 自动生成数据库的策略,update第一次创建表,以后更新 -->
<property name="hbm2ddl.auto">update</property>
<!-- 加载映射文件 -->
<mapping resource="com/iflytek/lhq/pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
4.User.hbm.xml
<?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 package="com.iflytek.lhq.pojo">
<class name="User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="userName" column="user_name" />
<property name="birthDay" type="timestamp" column="birthday" />
</class>
</hibernate-mapping>