在Maven中有三大模块,分别是依赖、仓库、生命周期和插件,我们接下来下来介绍下依赖,为了方便起见我们还是以案例来说:
1、首先建立一个maven项目,这里我建立一个user的项目
2、接下来我们在这个项目中要使用到hibernate框架,此时就要在此项目中加入hibernate的支持,而之前我们讲到maven会自动的为我们加入jar包,但是前提是我们要找到怎么样编写hibernate的引用,如何编写呢?接下来我们就要牵涉到依赖的查询,在maven中所有的依赖都是通过坐标来保存的(GAV-->groupId,artifactId,version),在网上有一些仓库为我们提供了一下项目的坐标,比如:http://mvnrepository.com/,http://www.sonatype.org/nexus/等等,这里我们就使用mvnrepository仓库了,在里面搜索hibernate就能找到对应的hibernate包,如下:
如图,只需要将其中的dependency拷贝到当前项目的pom.xml中即可,如下:
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.10</version> 6 <scope>test</scope> 7 </dependency> 8 <dependency> 9 <groupId>org.hibernate</groupId> 10 <artifactId>hibernate-core</artifactId> 11 <version>4.2.5.Final</version> 12 </dependency> 13 </dependencies>
保存后,maven就会首先检查本地仓库中是否有hibernate的支持包,如果没有的话则会到网上进行下载,这个过程可能会稍微有点慢!
这里附上hibernate的配置文件和数据库的创建脚本:
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 <session-factory> 8 <property name="dialect"> 9 org.hibernate.dialect.MySQLDialect 10 </property> 11 <property name="connection.url"> 12 jdbc:mysql://localhost:3306/user 13 </property> 14 <property name="connection.username">root</property> 15 <property name="connection.password">wangzhen</property> 16 <property name="connection.driver_class"> 17 org.gjt.mm.mysql.Driver 18 </property> 19 <property name="myeclipse.connection.profile">mysql</property> 20 <property name="show_sql">true</property> 21 <property name="format_sql">true</property> 22 23 <mapping class="com.lq.wangzhen.user.vo.User"/> 24 </session-factory> 25 </hibernate-configuration>
数据库user的创建脚本:
3、完成后我们在src下再建立一个文件夹,src/main/resources用来保存hibernate的资源文件和log4j的日志文件,这样的话,我们还要加入log4j的jar包,我们继续到仓库里面搜索log4j对应的依赖坐标。
继续在pom.xml中加入对log4j的支持:
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.10</version> 6 <scope>test</scope> 7 </dependency> 8 <dependency> 9 <groupId>org.hibernate</groupId> 10 <artifactId>hibernate-core</artifactId> 11 <version>4.2.5.Final</version> 12 </dependency> 13 <dependency> 14 <groupId>log4j</groupId> 15 <artifactId>log4j</artifactId> 16 <version>1.2.17</version> 17 </dependency> 18 </dependencies>
4、因为要进行数据库的连接,所以这里还要加入对mysql数据库的依赖:
继续在pom.xml中加入对mysql的依赖支持:
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.10</version> 6 <scope>test</scope> 7 </dependency> 8 <dependency> 9 <groupId>org.hibernate</groupId> 10 <artifactId>hibernate-core</artifactId> 11 <version>4.2.5.Final</version> 12 </dependency> 13 <dependency> 14 <groupId>log4j</groupId> 15 <artifactId>log4j</artifactId> 16 <version>1.2.17</version> 17 </dependency> 18 <dependency> 19 <groupId>mysql</groupId> 20 <artifactId>mysql-connector-java</artifactId> 21 <version>5.1.26</version> 22 </dependency> 23 </dependencies>
5、编写vo类User:
1 package com.lq.wangzhen.user.vo; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 9 @Entity 10 @Table(name = "t_user") 11 public class User { 12 13 private Integer id; 14 private String username; 15 private String password; 16 private String email; 17 18 @Id 19 @GeneratedValue 20 public Integer getId() { 21 return id; 22 } 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 public String getUsername() { 27 return username; 28 } 29 public void setUsername(String username) { 30 this.username = username; 31 } 32 public String getPassword() { 33 return password; 34 } 35 public void setPassword(String password) { 36 this.password = password; 37 } 38 public String getEmail() { 39 return email; 40 } 41 public void setEmail(String email) { 42 this.email = email; 43 } 44 45 46 }
对应的表t_user的创建脚本:
1 create table t_user 2 ( 3 id int primary key auto_increment, 4 username varchar(100), 5 password varchar(32), 6 email varchar(100) 7 );
6、编写测试类,在编写测试类之前我们要首先编写Hibernate的一个工具类,用来获得Session对象:
1 package com.lq.wangzhen.user.vo; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.cfg.Configuration; 6 7 public class HibernateUntil { 8 9 private static SessionFactory factory = null; 10 11 static{ 12 13 factory = new Configuration().configure().buildSessionFactory(); 14 } 15 16 public static Session openSession(){ 17 return factory.openSession(); 18 } 19 }
然后在编写测试类:
1 package com.lq.wangzhen.test; 2 3 import org.hibernate.Session; 4 import org.junit.Assert; 5 import org.junit.Test; 6 7 import com.lq.wangzhen.user.vo.HibernateUntil; 8 import com.lq.wangzhen.user.vo.User; 9 10 public class TestUser { 11 12 @Test 13 public void testAdd(){ 14 Session session = HibernateUntil.openSession(); 15 session.beginTransaction(); 16 17 User u = new User(); 18 u.setUsername("zhangsan"); 19 u.setPassword("123456"); 20 u.setEmail("[email protected]"); 21 session.save(u); 22 Assert.assertTrue(u.getId()>0); 23 session.getTransaction().commit(); 24 } 25 }
通过myeclipse在pom.xml右键点击,运行Maven test,如图:
此时我们查看数据库,就可以看到数据库中多了一条数据:
然后运行maven install就可以进行程序的打包操作了: