提交 1523aeb9 编写于 作者: 亦蔚然's avatar 亦蔚然

使用了MyBatis

上级 8c3c3ad7
......@@ -75,6 +75,12 @@
<artifactId>annotations</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
......@@ -90,6 +96,16 @@
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
......
......@@ -17,7 +17,7 @@ import java.util.stream.Collectors;
public class Crawler {
CrawlerDao dao = new JdbcCrawlerDao();
CrawlerDao dao = new MyBatisCrawlerDao();
public void run() throws SQLException, IOException {
String link = null;
......@@ -33,7 +33,8 @@ public class Crawler {
// 分析页面url将它们放到即将处理的url池子中去
parseUrlsFromAndStoreIntoDatabase(doc);
storeIntoDatabaseIfItIsNewsPage(doc, link);
dao.updataDatabase(link, "insert into LINKS_ALREADY_PROCESSED(link) values (?)");
dao.insertProcessedLinked(link);
// dao.updataDatabase(link, "insert into LINKS_ALREADY_PROCESSED(link) values (?)");
} else {
// 不感兴趣
continue;
......@@ -54,7 +55,8 @@ public class Crawler {
if (href.toLowerCase().startsWith("javascript")) {
continue;
}
dao.updataDatabase(href, "insert into LINKS_TO_BE_PROCESSED(link) values (?)");
dao.insertLinkToBeProcessed(href);
// dao.updataDatabase(href, "insert into LINKS_TO_BE_PROCESSED(link) values (?)");
}
}
......
......@@ -3,14 +3,13 @@ package com.github.weiranyi;
import java.sql.SQLException;
public interface CrawlerDao {
String getNextLink(String sql) throws SQLException;
String getNextLinkThenDelete() throws SQLException;
void updataDatabase(String link, String sql) throws SQLException;
void insertNewsIntoDataBase(String url, String title, String content) throws SQLException;
boolean isLinkProcessed(String link) throws SQLException;
void insertProcessedLinked(String link);
void insertLinkToBeProcessed(String href);
}
......@@ -28,7 +28,7 @@ public class JdbcCrawlerDao implements CrawlerDao {
return link;
}
public String getNextLink(String sql) throws SQLException {
private String getNextLink(String sql) throws SQLException {
ResultSet resultSet = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
resultSet = statement.executeQuery();
......@@ -75,4 +75,10 @@ public class JdbcCrawlerDao implements CrawlerDao {
}
return false;
}
// jdbc
@Override
public void insertProcessedLinked(String link) {}
@Override
public void insertLinkToBeProcessed(String href) {}
}
package com.github.weiranyi;
import com.github.weiranyi.entity.News;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class MyBatisCrawlerDao implements CrawlerDao {
private SqlSessionFactory sqlSessionFactory;
public MyBatisCrawlerDao() {
String resource = "db/mybatis/config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
// 获取下一个链接再删除
@Override
public String getNextLinkThenDelete() throws SQLException {
// SqlSession openSession(boolean autoCommit);这里设计事务,必须提交才生效,要设置参数为true
try (SqlSession session = sqlSessionFactory.openSession(true)) {
String url = session.selectOne("com.github.weiranyi.MyMapper.selectNextAvailableLink");
if (url != null) {
session.delete("com.github.weiranyi.MyMapper.deleteLink", url);
}
return url;
}
}
// 插入新闻到数据库
@Override
public void insertNewsIntoDataBase(String url, String title, String content) throws SQLException {
try (SqlSession session = sqlSessionFactory.openSession(true)) {
session.insert("com.github.weiranyi.MyMapper.insertNews", new News(url, title, content));
}
}
//
@Override
public boolean isLinkProcessed(String link) throws SQLException {
try (SqlSession session = sqlSessionFactory.openSession(true)) {
int count = (Integer) session.selectOne("com.github.weiranyi.MyMapper.countLink", link);
return count != 0;
}
}
// 高级MyBatis操作
@Override
public void insertProcessedLinked(String link) {
// 创建一个map集合对象
Map<String, Object> param = new HashMap<>();
// 设置表名
param.put("tableName", "links_already_processed");
// 设置链接
param.put("link", link);
// SqlSession openSession(boolean autoCommit);这里设计事务,必须提交才生效,要设置参数为true
try (SqlSession session = sqlSessionFactory.openSession(true)) {
session.insert("com.github.weiranyi.MyMapper.insertLink", param);
}
}
@Override
public void insertLinkToBeProcessed(String href) {
Map<String, Object> param = new HashMap<>();
// 设置表名
param.put("tableName", "links_to_be_processed");
// 设置链接
param.put("link", href);
// SqlSession openSession(boolean autoCommit);这里设计事务,必须提交才生效,要设置参数为true
try (SqlSession session = sqlSessionFactory.openSession(true)) {
session.insert("com.github.weiranyi.MyMapper.insertLink", param);
}
}
}
package com.github.weiranyi;
package com.github.weiranyi.entity;
/**
* @author: https://github.com/weiranyi
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册