maven+Hibernate+mysql环境搭建
项目结构图如下
一,首先是添加依赖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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<packaging>war</packaging> <name>Hibernate</name>
<groupId>com.cyf</groupId>
<artifactId>Hibernate</artifactId>
<version>1.0-SNAPSHOT</version> <build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8888</port>
<maxIdleTime>30000</maxIdleTime>
</connector>
</connectors>
<webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
<contextPath>/</contextPath>
</configuration>
</plugin>
</plugins>
<!--把xml文件也编译-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build> <!-- 属性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency> <!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.5.Final</version>
</dependency> <!-- 添加Log4J依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.4</version>
</dependency> <!-- 添加javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
</dependencies> </project>
二,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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!--显示生成的sql语句-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 禁用了javaEE6的bean-validate -->
<property name="javax.persistence.validation.mode">none</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- 即可通过getCurrentSession 获取线程唯一的session -->
<property name="current_session_context_class">thread</property> <!--在数据库中自动创建表-->
<!--<property name="hbm2ddl.auto">update</property>-->
<!-- 指定ddl的生成方式 -->
<!--<property name="hibernate.hbm2ddl.auto">create</property>--> <mapping resource="com/deppon/test03/entity/PersonEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三,PersonEntity.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.deppon.test03.entity">
<class name="PersonEntity" table="t_person">
<id name="id" column="id" type="int">
<generator class="native"/>
</id>
<property name="name" type="string" column="name"/>
</class>
</hibernate-mapping>
四,PersonEntity.java
package com.deppon.test03.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name = "t_person")
public class PersonEntity implements java.io.Serializable {
private static final long serialVersionUID = -4376187124011546736L; private Integer id;
private String name; @Id
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} @Column(length = 50 , nullable = false , unique = true)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "PersonEntity [id=" + id + ", name=" + name + "]";
} }
五,HibernateUtil.java
package com.deppon.test03.util; import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtil {
/** ThreadLocal Session Map */
public static final ThreadLocal<Session> SESSIONMAP = new ThreadLocal<Session>();
private static final SessionFactory sessionFactory;
private static final Logger LOGGER = Logger.getLogger(HibernateUtil.class); static {
try {
LOGGER.debug("HibernateUti.static - loading cofig");
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
LOGGER.debug("HibernateUtil.static - end");
} catch (Throwable ex) {
ex.printStackTrace();
LOGGER.error("HibernateUti error : ExceptionInInitializerError");
throw new ExceptionInInitializerError(ex);
}
} private HibernateUtil() { } public static Session getSession() throws HibernateException {
Session session = SESSIONMAP.get(); if(session == null) {
session = sessionFactory.openSession();
SESSIONMAP.set(session);
} return session;
} public static void closeSession() throws HibernateException {
Session session = SESSIONMAP.get();
SESSIONMAP.set(null); if(session != null) {
session.close();
}
} }
六,ModelTest.java
package com.deppon.test03.model; import java.util.List; import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Assert;
import org.junit.Test; import com.deppon.test03.entity.PersonEntity;
import com.deppon.test03.util.HibernateUtil; public class ModelTest { @Test
public void testGetSession() {
Session session = HibernateUtil.getSession(); Assert.assertNotNull(session); HibernateUtil.closeSession();
} @Test
public void testExport() {
new SchemaExport(new Configuration().configure()).create(true , true);
} @Test
public void testSave() {
PersonEntity person = new PersonEntity();
// person.setId(2);
person.setName("ccc"); Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction(); session.save(person); tx.commit();
HibernateUtil.closeSession();
} @Test
public void testQuery() {
Session session = HibernateUtil.getSession();
session.beginTransaction(); @SuppressWarnings("unchecked")
List<PersonEntity> personList = session.createQuery("select p from PersonEntity p").list(); for(PersonEntity eachPerson : personList) {
System.out.println(eachPerson);
} session.getTransaction().commit();
HibernateUtil.closeSession();
} }
七,sql语句
/*
Navicat MySQL Data Transfer Source Server : test
Source Server Version : 50717
Source Host : localhost:3306
Source Database : test Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001 Date: 2018-04-07 19:54:35
*/ SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for `t_person`
-- ----------------------------
DROP TABLE IF EXISTS `t_person`;
CREATE TABLE `t_person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
maven+Hibernate+mysql环境搭建的更多相关文章
- [Hibernate 1]Hibernate的环境搭建
一.Hibernate是什么 直接使用JDBC操作数据库的步骤很繁琐,JDBC操作的是关系型数据库,而我们用JAVA开发程序,则使用面向对象的思想.Hibernate正是在这两种不同的模型之间建立关联 ...
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...
- Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
原文地址:http://www.osyunwei.com/archives/7378.html 搬运是为了自己找资料方便. 准备篇 一.环境说明: 操作系统:Windows Server 2012 R ...
- Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: htt ...
- Jenkins+Maven+Git CI环境搭建手册
Jenkins+Maven+Git CI环境搭建手册 环境: OS:Linux version 2.6.32-220.23.2.ali878.el6.x86_64 (ads@kbuild) (gcc ...
- Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】
http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附 ...
- 项目管理利器maven学习笔记(一):maven介绍及环境搭建
maven介绍 maven下载与环境搭建 http://maven.apache.org/download.cgi# 解压到指定位置,比如我解压到D盘 设置maven环境变量 添加一个变量名,变量值为 ...
- eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...
- Eclipse+maven+scala+spark环境搭建
准备条件 我用的Eclipse版本 Eclipse Java EE IDE for Web Developers. Version: Luna Release (4.4.0) 我用的是Eclipse ...
随机推荐
- April Fools Contest 2017 C
Description DO YOU EXPECT ME TO FIND THIS OUT? WHAT BASE AND/XOR LANGUAGE INCLUDES string? DON'T BYT ...
- 题解报告:NYOJ #737 石子合并(一)(区间dp)
描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值 ...
- 162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...
- 01.第一章_C++ Primer学习笔记_开始
1.2 初始输入输出 iostream库里面包含两个基础类型istream和ostream,分别表示输入流和输出流,一个流就是一个字符序列,从IO设备读出或者写入IO设备. 标准的输入输出对象 标准库 ...
- oracle 数据导入、导出
导入导出 --数据导出备份和导入 ------注意 导出和导入 必须是CMD 命令行下操作,而不是SQL编辑器中 --1.导出表 . --exp:导出关键字 ,userid:用户权限 ,file:保存 ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
- 【转】Java泛型方法
1. 定义泛型方法 (1) 如果你定义了一个泛型(类.接口),那么Java规定,你不能在所有的静态方法.静态初块等所有静态内容中使用泛型的类型参数.例如: public class A<T> ...
- Backbone.js之Todo源码浅析
相信每个接触了解过backbone的人都知道todo,网上的关于它的分析教程也都分析乱了.但是,知识只有自己学习领悟才是自己的,话不多说,正文开始. 在分析todo的源码之前,首先我们要知道todo具 ...
- el-select,选择一行,取值行的对象.
<el-select v-model="set_invoice_form.InvoiceType" placeholder="请选择" :disabl ...
- iOS 音频视频制作
--iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...