超越昨天的自己系列(6)

  使用ibatis开发中,耗在dao层的开发时间,调试时间,差错时间,以及适应修改需求的时间太长,导致项目看起来就添删改查,却特别费力。
 
  在项目性能要求不高的情况下,开始寻找比较能快速点的开发框架,首先从dao层开始,考虑到hibernate的逆向工程,以及零sql的标语,项目开始尝试使用。
 
  自己想打一个hibernate底层的项目,顺便熟悉下hibernate。
首先用maven 命令构建一个java项目
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>
filter-dev.properties文件,其中放数据库连接的信息,全局变量,图片服务器地址,公共目录,log打印文件地址啊什么的:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.20.106:/adplugin?autoReconnect=true
jdbc.username=root
jdbc.password=root123

hibernate

接下来就是hibernate的使用了:
使用比较低端的xml方式,也比较简单。
目录结构:
 
 
HibernateUtil:用于产生SessionFactory
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)的更多相关文章

  1. 时间作为横轴的图表(morris.js)超越昨天的自己系列(8)

    超越昨天的自己系列(8) morris.js的官网有详细的例子:http://www.oesmith.co.uk/morris.js/ 特别注意它的依赖: <link rel="sty ...

  2. spring和redis的整合-超越昨天的自己系列(7)

    超越昨天的自己系列(7) 扯淡:  最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三的,不过前提好像是研究的比较深,有了自己的见解.自认为学习 ...

  3. Maven根据不同环境打包不同配置文件

    开发项目时会遇到这个问题:开发环境,测试环境,生产环境的配置文件不同,打包时经常要手动更改配置文件,更改的少还可以接受,但是如果需要更多个配置文件,手动的方法就显得非常笨重了. 下面介绍一种方法,利用 ...

  4. maven项目多环境打包问题

    1.xxx-api是基于springboot的模块 2.配置文件 application.properties spring.profiles.active=@activeEnv@ applicati ...

  5. Maven实现多环境打包

    在开发的过程中,经常需要面对不同的运行环境(开发环境.测试环境.生产环境.内网环境.外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置 ...

  6. HashMap归档-超越昨天的自己系列

    java HashMap 读一下源码,一个数组存储数据: transient Entry[] table; 内部存key和value的内部类: static class Entry<K,V> ...

  7. Collections.reverse 代码思考-超越昨天的自己系列(13)

    点进Collections.reverse的代码瞄了眼,然后就开始了一些基础知识的收集. 现在发现知道的越多,知道不知道的越多. 列几个记录下: reverse方法源码: /** * Reverses ...

  8. crontab 移动日志-超越昨天的自己系列(12)

    linux上定时执行某些脚本是管理服务器的时候比较常用的场景,比如定时检查进程是否存在,定时启动或关闭进程,定时检查日志删除日志等. 当我打开google百度crontab时长篇大论的一大堆,详细解释 ...

  9. java进程性能分析步骤-超越昨天的自己系列(11)

    java进程load过高分析步骤: top 查看java进程情况     top -Hp 查看某个进程的具体线程情况   printf 0x%x 确认哪一个线程占用cpu比较多,拿出来转成16进制   ...

随机推荐

  1. MapReduce 重要组件——Recordreader组件 [转]

    (1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...

  2. oracle查询语句大全

    1.查处oracle数据库的字符编码格式 select * from v$nls_parameters t where t.PARAMETER='NLS_CHARACTERSET'; 如果查出的是ZH ...

  3. js基础之事件

    一.event对象 document.onclick=function(ev){ oEvent=event?event:ev;//兼容性写法 alert(oEvent.clientX); alert( ...

  4. bzoj 1834: [ZJOI2010]network 网络扩容

    #include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...

  5. setsockopt的作用

    功能描述:        获取或者设置与某个套接字关联的选 项.选项可能存在于多层协议中,它们总会出现在最上面的套接字层.当操作套接字选项时,选项位于的层和选项的名称必须给出.为了操作套接字层的选项, ...

  6. Section 1.4 Packing Rectangles

    本来是USACO Training的1.4.1的,但是介于今早过了食物链想起了这道题实在是太怨念了,翻出自己写的AC程序居然有5KB!! 思路很简单,枚举,而且就图中的六种情况.但是第六种变化状况太多 ...

  7. Map学习

    1.Query Operations(查询操作) int size();boolean isEmpty(); boolean containsKey(Object key);boolean conta ...

  8. IIS 6.0 401 错误

    1.错误号401.1 症状:HTTP 错误 401.1 - 未经授权:访问由于凭据无效被拒绝 分析:  由于用户匿名访问使用的账号(默认是IUSR_机器名)被禁用,或者没有权限访问计算机,将造成用户无 ...

  9. MongoDB 聚合 (转) 仅限于C++开发

    MongoDB除了基本的查询功能,还提供了很多强大的聚合工具,其中简单的可计算集合中的文档个数, 复杂的可利用MapReduce做复杂数据分析. 1.count count返回集合中的文档数量 db. ...

  10. MySQL数据类型(四)

    一.数据类型 二.整型类型 tinyInt: 1个字节:-128-127(有符号) 是否有符号,可以定义时,使用unsign标识,表示无符号的,不写表示有符号的 Create table studen ...