日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Annotation 的第一个工程

發布時間:2025/5/22 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Annotation 的第一个工程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、什么是 Annotation?

??????? java.lang.annotation,接口 Annotation。對于Annotation,是Java5的新特性,JDK5引入了Metadata(元數據)很容易的就能夠調用Annotations。Annotations提供一些本來不屬于程序的數據,比如:一段代碼的作者或者告訴編譯器禁止一些特殊的錯誤。An annotation 對代碼的執行沒有什么影響。Annotations使用@annotation的形式應用于代碼:類(class),屬性(attribute),方法(method)等等。一個Annotation出現在上面提到的開始位置,而且一般只有一行,也可以包含有任意的參數。

?

二、Annotation 有什么作用?

?????? 在創建EJB組件時,必需提供一些定義,使得EJB組件使用一些服務例如:安全服務,持久化服務,事務服務。EJB容器可以提供這些服務,這樣EJB只要實現業務邏輯就可以了。但是說到底EJB容器使用EJB組件的元數據來提供這些服務,在以前EJB的元數據是以XML配置文件形式出現的,這些配置文件與EJB源文件是分開的。????? EJB的部署人員無法了解EJB本身的信息,如果EJB組件的創建者用注。

?

三、Annotation 第一個工程,操作步驟如下:

1、建立新的 java 項目,名為:annotation_test,如下圖:

?????

?

?

2、加入響應的 jar 包

? ? a)、菜單 Window --> Preferences --> Java --> Build Path --> User Libraries,點擊按鈕?“New”,命名為 “annotation”,如下圖:

????????????

?

???? b)、選擇“annotation”,點擊“Add External JARs...”,把下面的這些 jar 引入添加進去,分別是:

? ? ? ? ? ? ? hibernate-distribution\3.3.2\hibernate-distribution-3.3.2.GA\hibernate3.jar(核心JAR包

? ? ? ? ? ? ? hibernate-distribution\3.3.2\hibernate-distribution-3.3.2.GA\lib\required\ 目錄下的所有 jar 包

? ? ? ? ? ? ? slf4j-1.5.8\slf4j-nop.jar

????????????? hibernate-annotations\3.4.0\hibernate-annotations-3.4.0.GA\hibernate-annotations.jar

????????????? hibernate-annotations\3.4.0\hibernate-annotations-3.4.0.GA\lib\ejb3-persistence.jar

????????????? hibernate-annotations\3.4.0\hibernate-annotations-3.4.0.GA\lib\hibernate-commons-annotations.jar

? ? ? ? ? ? ? 點擊按鈕 “確定”,

? ? ? ? ? ? ??如下圖:

?????????????

?

?

3、引入 mysql 的 JDBC 驅動包

? ? ??a)、菜單 Window --> Preferences --> Java --> Build Path --> User Libraries,點擊按鈕?“New”,命名為 “Driver”,然后點擊 “Driver”,點擊按鈕 “Add External JARs...”,點擊按鈕 “確定”, 如下圖:

???????????

?

?????b)、選擇“driver”,點擊“Add External JARs...”,把 Driver\MySql\mysql-connector-java-5.1.47\mysql-connector-java-5.1.47\mysql-connector-java-5.1.47.jar 引入添加進去,如下圖:

?????????????

?

?

4、將驅動引入項目工程,點擊項目(hibernate_test),鼠標右擊,選擇 Build Path --> Add Libraries... --> User Library,點擊按鈕 “Next”;

? ? ??注意:選中工程,如果右鍵選中 Build Path 里面沒有?Add Libraries...?,右鍵工程選中 Configure Build Path ,進入后,左邊欄選中 java Build Path,再選中右邊的 Libraries,再選?Add Libraries... ,然后再勾選?“annotation”?與 “driver” 添加就可以了

? ? ? 如下圖:

?????

?

5、登陸本地 MySQL,創建數據庫 annotation,并使用 annotation?庫,在創建 teacher?表,步驟如下:

????? mysql -hlocalhost -uroot -p,敲回車,然后輸入密碼

????? create datebase annotation;

???? ?use annotation;

????? create table teacher(id int primary key, name varchar(20), title varchar(10));

????

?

6、建立 hibernate 配置文件 hibernate.cfg.xml,這個配置文件應建立在 src 目錄下面,如下圖:

?

?hibernate.cfg.xml 文件里面的內容為:

? ? ?注意:??

? ? ? ? ? ? a)、從參考文檔中拷貝

?????????? ?b)、修改對應的數據庫連接

??????????? c)、注釋掉暫時用不上的內容?

<?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><!-- Database connection settings --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost/annotation</property> <!-- annotation是數據庫的名字 --><property name="connection.username">root</property><property name="connection.password">root</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">true</property><!-- Drop and re-create the database schema on startup --><!-- hbm2ddl.auto : Hibernate-mapping Data definition language.automatic --><!-- create: 表示如果數據庫里面沒有這張表, 就會自動去創建這張表 --><property name="hbm2ddl.auto">create</property><mapping class="annotation.model.Teacher"/> <!-- 這個是Annotation的寫法 --></session-factory> </hibernate-configuration>

  

?

7、建立?Teacher 類,如下圖:

?

Teacher 類的內容為:?

package annotation.model;/*** JDBC 是一個標準* MySql Driver/Oracle Driver 等都是 JDBC 的實現* 或者說 hibernate 是 JDBC 的實現*/import javax.persistence.Entity; // 從包名可以看出, 它不依賴hibernate import javax.persistence.Id;@Entity public class Teacher {private int id;private String name;private String title;@Id // 把注解Id加到主鍵上public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;} }

?

?

8、在 hibernate.cfg.xml 中建立映射 <Mapping class=../>,將 <mapping class="annotation.model.Teacher"/>?添加到?配置文件中,如下圖:

?

?

9、寫一個測試類,如下圖:

?

測試類的內容為:

package annotation.test;/*** 目前用 main 來測試*/import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import annotation.model.Teacher;public class TeacherTest {public static void main(String[] args) {Teacher t = new Teacher();t.setId( 1 );t.setName( "I Love you" );t.setTitle( "高級" );Configuration cfg = new AnnotationConfiguration();SessionFactory sf = cfg.configure().buildSessionFactory();Session session = sf.openSession();// 在hibernate里面, 所有的操作都應放在事務里面session.beginTransaction();// 將數據保存到數據庫里面session.save( t );// 提交數據session.getTransaction().commit();// 關掉數據庫連接session.close();sf.close();String str = "\u4ea4\u6613\u6210\u529f";System.out.println( str );} }

?

?

10、如果測試類成功執行,則可以通過sql語句查看數據,如下圖:

?

轉載于:https://www.cnblogs.com/dgm2088/p/9904214.html

總結

以上是生活随笔為你收集整理的Annotation 的第一个工程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。