【Maven實戰】依賴的範圍

在Maven中有三大模塊,分別是依賴、倉庫、生命週期和插件,咱們接下來下來介紹下依賴,爲了方便起見咱們仍是以案例來講:java

一、首先創建一個maven項目,這裏我創建一個user的項目mysql

二、接下來咱們在這個項目中要使用到hibernate框架,此時就要在此項目中加入hibernate的支持,而以前咱們講到maven會自動的爲咱們加入jar包,可是前提是咱們要找到怎麼樣編寫hibernate的引用,如何編寫呢?接下來咱們就要牽涉到依賴的查詢,在maven中全部的依賴都是經過座標來保存的(GAV-->groupId,artifactId,version),在網上有一些倉庫爲咱們提供了一下項目的座標,好比:http://mvnrepository.com/http://www.sonatype.org/nexus/等等,這裏咱們就使用mvnrepository倉庫了,在裏面搜索hibernate就能找到對應的hibernate包,以下:sql

如圖,只須要將其中的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的支持包,若是沒有的話則會到網上進行下載,這個過程可能會稍微有點慢!session

這裏附上hibernate的配置文件和數據庫的建立腳本:app

 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的建立腳本:框架

三、完成後咱們在src下再創建一個文件夾,src/main/resources用來保存hibernate的資源文件和log4j的日誌文件,這樣的話,咱們還要加入log4j的jar包,咱們繼續到倉庫裏面搜索log4j對應的依賴座標。eclipse

繼續在pom.xml中加入對log4j的支持:maven

 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>

 

四、由於要進行數據庫的鏈接,因此這裏還要加入對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>

 

五、編寫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 );

 

六、編寫測試類,在編寫測試類以前咱們要首先編寫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("admin@admin.com");
21         session.save(u);
22         Assert.assertTrue(u.getId()>0);
23         session.getTransaction().commit();
24     }
25 }

 

經過myeclipse在pom.xml右鍵點擊,運行Maven test,如圖:

此時咱們查看數據庫,就能夠看到數據庫中多了一條數據:

而後運行maven install就能夠進行程序的打包操做了:

相關文章
相關標籤/搜索