maven为不同环境打包(hibernate)-超越昨天的自己系列(6)
超越昨天的自己系列(6)
mvn archetype:create -DgroupId=work -DartifactId=HibernateFirst
最重要的pom.xml,profiles节点中的两个profile就是对不同环境的打包选择不同的properties文件进行了描述,可以看到
<activeByDefault>true</activeByDefault>默认触发的意思,其实就是说我用dev(默认)这个时,env这个参数等于dev,如果选择production的时候,env就等于production了。
单单这样还不能实现对不同环境打包时,使用不同的properties,注意下面build节点,这个节点描述了打包时的行为。
filters节点是类似全局替换的感觉,使用的${user.dir}/env/filter-${env}.properties就能明白上面提到的env的作用了,就是修改了下文件名嘛,靠~。
我们在pom同目录下的env文件夹里放了filter-dev.properties 和 filter-production.properties,就可以啦。
这样的配置就会把properties 文件里的内容自动的去替换了,还有个问题,这个替换的动作针对的文件夹是哪个?
下面的resource节点就是描述这事的。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>work</groupId>
<artifactId>HibernateFirst</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>HibernateFirst</name>
<url>http://maven.apache.org</url>
<profiles>
<!-- 开发环境,默认激活 -->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
<maven.test.skip>true</maven.test.skip>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 生产环境,默认激活 -->
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.24</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies> <!-- Build Settings -->
<build>
<defaultGoal>install</defaultGoal>
<filters>
<filter>${user.dir}/env/filter-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.20.106:/adplugin?autoReconnect=true
jdbc.username=root
jdbc.password=root123
hibernate

public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration()
.configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Employee:
public class Employee {
private Long id; private String firstname; private String lastname; private Date birthDate; private String cellphone; public Employee() { } public Employee(String firstname, String lastname, Date birthdate, String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = birthdate;
this.cellphone = phone; } ....get set....
Test:
Employee.hbm.xmlpublic class Test { public static void main( String[] args )
{
list();
System.out.println( "Hello World!" );
} private static List list() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession(); List employees = session.createQuery("from Employee").list();
System.out.println(employees.size());
session.close();
return employees;
}
}
Employee.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="work.hibernate"> <class name="Employee" table="EMPLOYEE">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="firstname" />
<property name="lastname" column="lastname"/>
<property name="birthDate" type="date" column="birth_date"/>
<property name="cellphone" column="cell_phone"/>
</class> </hibernate-mapping>
hibernate.cfg.xml 注意这里的数据库配置文件中的替换值,就是最前面提到的properties文件中的值,在install的时候自动替换。
<?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 name="mysql">
<property name="hibernate.connection.driver_class">${jdbc.driverClassName}</property>
<property name="hibernate.connection.url">${jdbc.url}</property>
<property name="hibernate.connection.username">${jdbc.username}</property>
<property name="hibernate.connection.password">${jdbc.password}</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">false</property> <property name="hbm2ddl.auto">validate</property>
<mapping resource="Employee.hbm.xml" />
</session-factory> </hibernate-configuration>
--------------------------------------------------
让我们继续前行!
maven为不同环境打包(hibernate)-超越昨天的自己系列(6)的更多相关文章
- 时间作为横轴的图表(morris.js)超越昨天的自己系列(8)
超越昨天的自己系列(8) morris.js的官网有详细的例子:http://www.oesmith.co.uk/morris.js/ 特别注意它的依赖: <link rel="sty ...
- spring和redis的整合-超越昨天的自己系列(7)
超越昨天的自己系列(7) 扯淡: 最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三的,不过前提好像是研究的比较深,有了自己的见解.自认为学习 ...
- Maven根据不同环境打包不同配置文件
开发项目时会遇到这个问题:开发环境,测试环境,生产环境的配置文件不同,打包时经常要手动更改配置文件,更改的少还可以接受,但是如果需要更多个配置文件,手动的方法就显得非常笨重了. 下面介绍一种方法,利用 ...
- maven项目多环境打包问题
1.xxx-api是基于springboot的模块 2.配置文件 application.properties spring.profiles.active=@activeEnv@ applicati ...
- Maven实现多环境打包
在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置 ...
- HashMap归档-超越昨天的自己系列
java HashMap 读一下源码,一个数组存储数据: transient Entry[] table; 内部存key和value的内部类: static class Entry<K,V> ...
- Collections.reverse 代码思考-超越昨天的自己系列(13)
点进Collections.reverse的代码瞄了眼,然后就开始了一些基础知识的收集. 现在发现知道的越多,知道不知道的越多. 列几个记录下: reverse方法源码: /** * Reverses ...
- crontab 移动日志-超越昨天的自己系列(12)
linux上定时执行某些脚本是管理服务器的时候比较常用的场景,比如定时检查进程是否存在,定时启动或关闭进程,定时检查日志删除日志等. 当我打开google百度crontab时长篇大论的一大堆,详细解释 ...
- java进程性能分析步骤-超越昨天的自己系列(11)
java进程load过高分析步骤: top 查看java进程情况 top -Hp 查看某个进程的具体线程情况 printf 0x%x 确认哪一个线程占用cpu比较多,拿出来转成16进制 ...
随机推荐
- MySQL事务隔离级别详解
原文地址:http://xm-king.iteye.com/blog/770721 SQL标准定义了4类隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的.低级别的隔离级 ...
- Mongodb Management Studio
1.服务器管理功能添加服务器,删除服务器 2.服务器,数据库,表,列,索引,树形显示和状态信息查看 3.查询分析器功能.支持select,insert,Delete,update支持自定义分页函数 $ ...
- 分批次从musql取数据,每次取1000条
$t = new Gettags(); $num=$t->sum_tag(); $num=$num/1000; $flag_num=ceil($num); $flag_array=array() ...
- Oracle创建表
//创建表,列的内容 -- Create tablecreate table T_HQ_PC( pinpai VARCHAR2(20) not null, xingh VARCHAR2(40), ji ...
- wScratchPad 实现刮刮卡效果
插件网址http://wscratchpad.websanova.com/
- appjs desktop
/* author: daimajia name: appjs Express example email: daimajia@gmail.com any qu ...
- C-union的使用
union有两个作用: 1,节约空间,如果一个struct存在两个互斥的变量,则可以把这个struct变成union 2,将同一个内存作为多种解释 代码: #include <iostream& ...
- [vijos P1014] 旅行商简化版
昨天早上上课讲旅行商问题,有点难,这周抽空把3^n的算法码码看.不过这个简化版已经够折腾人了. 其一不看解析不知道这是双进程动态规划,不过我看的解析停留在f[i,j]表示第一个人走到i.第二个人走到j ...
- PowerShell工具脚本---按行数切割大文本文件
我编写的PowerShell工具脚本,[按行数切割大(文本)文件],生成n个小文件. 主要目的是为了能够让excel快速处理.或用脚本并发处理文本. 注意: 1 如果有必要,你可以先用其他工具,把大文 ...
- ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来)
ScrollView中嵌入原生ListView或GridView,会出现ListView,GridView显示不全的问题. 解决方法:重新构造一个ListView或GridView,重写OnMeasu ...