maven为不同环境打包(hibernate)

  最重要的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节点就是描述这事的。

xsi:schemaLocation4.0.0workHibernateFirst1.0-SNAPSHOTjarHibernateFirst${user.dir}/env/filter-${env}.propertiessrc/main/resourcestrue

filter-dev.properties文件,其中放数据库连接的信息,全局变量,图片服务器地址,公共目录,log打印文件地址啊什么的:

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.20.106:3306/adplugin?autoReconnect=truejdbc.username=rootjdbc.password=root123

hibernate

接下来就是hibernate的使用了:

使用比较低端的xml方式,也比较简单。

目录结构:

HibernateUtil:用于产生SessionFactory

public class HibernateUtil {SessionFactory sessionFactory = buildSessionFactory();private static SessionFactory buildSessionFactory() {try { 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 { 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

DOCTYPE hibernate-mapping PUBLIC”-//Hibernate/Hibernate Mapping DTD 3.0//EN””http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”

hibernate.cfg.xml 注意这里的数据库配置文件中的替换值,美国服务器,就是最前面提到的properties文件中的值,在install的时候自动替换。

DOCTYPE hibernate-configuration PUBLIC”-//Hibernate/Hibernate Configuration DTD 3.0//EN””http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”${jdbc.driverClassName}${jdbc.url}${jdbc.username}${jdbc.password}1org.hibernate.dialect.MySQLDialectthreadorg.hibernate.cache.NoCacheProviderfalsevalidate

————————————————–

让我们继续前行!

这一次是一个告别,或者一个永远的告别,

maven为不同环境打包(hibernate)

相关文章:

你感兴趣的文章:

标签云: