星空网 > 软件开发 > Java

玩玩Hibernate(二)hibernate

新建一个hSpider的工程,引入前面已经建立的lib

并为其建立一个hibernate.cfg.

 1 <??> 2 <!DOCTYPE hibernate-configuration PUBLIC 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5  6 <hibernate-configuration> 7  8   <session-factory> 9 10     <!-- Database connection settings 数据库的配置 -->11     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>12     <property name="connection.url">jdbc:mysql://localhost:3306/hSpider</property>13     <property name="connection.username">root</property>14     <property name="connection.password"></property>15 16     <!-- JDBC connection pool (use the built-in) hibernate自带连接池,暂不使用 -->17     <!-- <property name="connection.pool_size">1</property> -->18 19     <!-- SQL dialect 数据库方言,这里我们才爱用MySQL-->20     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>21 22     <!-- Enable Hibernate's automatic session context management 新功能,暂不使用 -->23     <!-- <property name="current_session_context_class">thread</property> -->24 25     <!-- Disable the second-level cache 二级缓存,放置不管 -->26     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>27 28     <!-- Echo all executed SQL to stdout 设置show_sql为true表示让hibernate将生成sql语句在控制台打印出来 -->29     <property name="show_sql">true</property>30 31     <!-- Drop and re-create the database schema on startup 是否让hibernate自动为我们创建表 -->32     <!-- <property name="hbm2ddl.auto">update</property> -->33 34     <mapping resource="hibernateSpider/edNews.hbm./> <!-- 这里是将需要mapping的文件进行再次声明 -->35 36   </session-factory>37 38 </hibernate-configuration>

 

新建`hSpider`包依次点击打开HibernateSpider->右键src->New->Package
新建`edNews`类依次点击打开HibernateSpider->src->hSpider->New->Class

玩玩Hibernate(二)hibernate玩玩Hibernate(二)hibernate
public class edNews {  private int id;  private String ednews;   public int getId() {    return id;  }  public void setId(int id) {    this.id = id;  }    public String getNews(){    return ednews;  }    public void setNews(news ednews){    this.ednews = ednews.ednews;  }}

edNews

并为其新建一个edNews.hbm.

<??><!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>  <class name="hibernateSpider.edNews" table="ednews">    <id name="id" type="int">      <column name="id" />      <generator class="increment" />    </id>    <property name="news" type="string">      <column name="news" length="255" />    </property>  </class></hibernate-mapping>

新建一个news类(用于显示)

玩玩Hibernate(二)hibernate玩玩Hibernate(二)hibernate
 1 public class news { 2  3     public String ednews;                         4     5     // 构造方法初始化数据  6     public news() {  7       ednews = "";  8     }  9    10     @Override 11     public String toString() { 12       return "公告:" + ednews + "\n"; 13     } 14 }

News

新建一个Spider类,这个是爬虫代码的实现

玩玩Hibernate(二)hibernate玩玩Hibernate(二)hibernate
 1 package hibernateSpider; 2  3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.net.URL; 6 import java.net.URLConnection; 7 import java.util.ArrayList; 8 import java.util.regex.Matcher; 9 import java.util.regex.Pattern;10 11 public class Spider {12   public static String SendGet(String url) {   13     // 定义一个字符串用来存储网页内容 14     String result = ""; 15     // 定义一个缓冲字符输入流 16     BufferedReader in = null; 17  18     try { 19       // 将string转成url对象 20       URL realUrl = new URL(url); 21       // 初始化一个链接到那个url的连接 22       URLConnection connection = realUrl.openConnection(); 23       // 开始实际的连接 24       connection.connect(); 25       // 初始化 BufferedReader输入流来读取URL的响应 26       in = new BufferedReader(new InputStreamReader( 27           connection.getInputStream(), "UTF-8")); 28       // 用来临时存储抓取到的每一行的数据 29       String line; 30       while ((line = in.readLine()) != null) { 31         // 遍历抓取到的每一行并将其存储到result里面 32         result += line; 33       } 34     } catch (Exception e) { 35       System.out.println("发送GET请求出现异常!" + e); 36       e.printStackTrace(); 37     } 38     // 使用finally来关闭输入流 39     finally { 40       try { 41         if (in != null) { 42           in.close(); 43         } 44       } catch (Exception e2) { 45         e2.printStackTrace(); 46       } 47     } 48     return result; 49  50   } 51   52     public static ArrayList<news> GetNews(String content) { 53     // 预定义一个ArrayList来存储结果 54     ArrayList<news> results = new ArrayList<news>(); 55     // 用来匹配标题 56     Pattern questionPattern = Pattern.compile("ggtz/\\d{4}.shtml.+?>(.+?)<"); 57     Matcher questionMatcher = questionPattern.matcher(content); 58     // 用来匹配url,也就是问题的链接 59     Pattern urlPattern = Pattern.compile("ggtz/\\d{4}.shtml.+?>(.+?)<"); 60     Matcher urlMatcher = urlPattern.matcher(content); 61  62     // 问题和链接要均能匹配到 63     boolean isFind = questionMatcher.find() && urlMatcher.find(); 64  65     while (isFind) { 66       // 定义一个news对象(公告对象)来存储抓取到的信息 67       news newsTemp = new news(); 68       newsTemp.ednews= questionMatcher.group(1); 69 70  71       // 添加成功匹配的结果 72       results.add(newsTemp); 73       // 继续查找下一个匹配对象 74       isFind = questionMatcher.find() && urlMatcher.find(); 75     } 76     return results; 77   } 78   79 80 }

Spider

 最后,测试一下结果

玩玩Hibernate(二)hibernate玩玩Hibernate(二)hibernate
 1 public class MainTest { 2  3   4   public static void main(String[] args) {  5   6     // 定义即将访问的链接  7   8     String url = "http://jwc.gdut.edu.cn/";  9   // 访问链接并获取页面内容 10     String content = Spider.SendGet(url); 11     // 获取该页面的所有的命题对象 12     ArrayList<news> myNews = Spider. GetNews(content); 13     // 打印结果 14     for(int i = 0; i < myNews.size(); i++){15       System.out.println(myNews.get(i)); 16   17       edNews aNew = new edNews()  ;//新建我们需要存储的类对象,并且设置其对象的一些属性18       aNew.setId(i);19       aNew.setNews(myNews.get(i));20 21       {22       //Configuration主要用以读取配置文件23         Configuration cfg = new Configuration();24         SessionFactory sf = cfg.configure().buildSessionFactory();25         //buildSessionFactory();得到一个创建Session的工场26         Session ss = sf.openSession();27         ss.beginTransaction();//OK,将操作放入事务中28         ss.save(aNew);//保存你的对象29         ss.getTransaction().commit();//得到事务并提交 30     31         ss.close();//Session关闭32         sf.close();//工厂关闭33         34         35       }36     }37 }38 }

MainTest

玩玩Hibernate(二)hibernate玩玩Hibernate(二)hibernate




原标题:玩玩Hibernate(二)hibernate

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

空派流程:https://www.goluckyvip.com/tag/35185.html
空派美国fba:https://www.goluckyvip.com/tag/35186.html
空派美国fba多少钱:https://www.goluckyvip.com/tag/35187.html
空派美国fba头程:https://www.goluckyvip.com/tag/35188.html
空派日本fba:https://www.goluckyvip.com/tag/35189.html
Tophatter:https://www.goluckyvip.com/tag/3519.html
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流