Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
引入了Mybatis作为新的数据访问方式 同时也增加了相关的Mybatis配置和sql映射xml
- Loading branch information
1 parent
e72d0dd
commit 7942cbb
Showing
8 changed files
with
209 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/github/DeeJay0921/MybatisCrawlerDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.github.DeeJay0921; | ||
|
||
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; | ||
|
||
public class MybatisCrawlerDao implements CrawlerDao { | ||
private SqlSessionFactory sqlSessionFactory; | ||
|
||
public MybatisCrawlerDao() { | ||
try { | ||
String resource = "db/mybatis/mybatis-config.xml"; | ||
InputStream inputStream = Resources.getResourceAsStream(resource); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String getNextLinkThenDelete() throws SQLException { | ||
String link; | ||
// 这里的openSession 的参数autoCommit一定要为true,否则每次的删除就没有被提交到数据库 | ||
try (SqlSession session = sqlSessionFactory.openSession(true)) { | ||
link = session.selectOne( | ||
"com.github.DeeJay0921.mybatis.selectNextLink"); // 这边输入Mapper.xml里面的命名空间加Select语句的id | ||
if (link != null) { | ||
session.delete("com.github.DeeJay0921.mybatis.deleteLink", link); | ||
} | ||
} | ||
return link; | ||
} | ||
|
||
@Override | ||
public void insertNewsIntoDatabase(String link, String articleTitle, String articleContent) throws SQLException { | ||
try (SqlSession session = sqlSessionFactory.openSession(true)) { | ||
session.insert("com.github.DeeJay0921.mybatis.insertNews", new News(articleTitle, articleContent, link)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isLinkProcessed(String link) throws SQLException { | ||
int count; | ||
try (SqlSession session = sqlSessionFactory.openSession()) { | ||
count = session.selectOne("com.github.DeeJay0921.mybatis.countLink", link); | ||
} | ||
return 0 != count; | ||
} | ||
|
||
@Override | ||
public void insertLinkIntoProcessed(String link) { | ||
this.insertIntoDifferentTable("LINKS_ALREADY_PROCESSED", link); | ||
} | ||
|
||
@Override | ||
public void insertLinkIntoToBeProcessed(String href) { | ||
this.insertIntoDifferentTable("LINKS_TO_BE_PROCESSED", href); | ||
} | ||
|
||
private void insertIntoDifferentTable(String tableName, String link) { | ||
HashMap<String, String> param = new HashMap<>(); | ||
param.put("tableName", tableName); | ||
param.put("link", link); | ||
try (SqlSession session = sqlSessionFactory.openSession(true)) { | ||
session.insert("com.github.DeeJay0921.mybatis.insertLink", param); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.github.DeeJay0921; | ||
|
||
import java.math.BigInteger; | ||
|
||
public class News { | ||
private BigInteger id; | ||
private String title; | ||
private String content; | ||
private String url; | ||
|
||
public News(String title, String content, String url) { | ||
this.title = title; | ||
this.content = content; | ||
this.url = url; | ||
} | ||
|
||
public BigInteger getId() { | ||
return id; | ||
} | ||
|
||
public void setId(BigInteger id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
<mapper namespace="com.github.DeeJay0921.mybatis"> | ||
<!--从库里读取下一条待爬取的链接 返回结果类型为String--> | ||
<select id="selectNextLink" resultType="String"> | ||
SELECT link FROM LINKS_TO_BE_PROCESSED LIMIT 1 | ||
</select> | ||
<!--从库里删除访问过的链接--> | ||
<delete id="deleteLink" parameterType="String"> | ||
delete from LINKS_TO_BE_PROCESSED where link = #{link} | ||
</delete> | ||
<!--插入一个News 包名要写全 插入的字段已News类内部的字段为准--> | ||
<insert id="insertNews" | ||
parameterType="com.github.DeeJay0921.News"> | ||
insert into NEWS (TITLE, CONTENT, URL) | ||
values (#{title},#{content},#{url}) | ||
</insert> | ||
<!--从已经访问过的连接池里读取链接看是否存在--> | ||
<select id="countLink" resultType="int" parameterType="String"> | ||
SELECT count(link) FROM LINKS_ALREADY_PROCESSED where LINK = #{link} | ||
</select> | ||
<!--采用动态SQL决定向那张表插入链接--> | ||
<insert id="insertLink" parameterType="HashMap"> | ||
insert into | ||
<choose> | ||
<when test="tableName == 'LINKS_TO_BE_PROCESSED'"> | ||
LINKS_TO_BE_PROCESSED | ||
</when> | ||
<otherwise> | ||
LINKS_ALREADY_PROCESSED | ||
</otherwise> | ||
</choose> | ||
(LINK) | ||
values (#{link}) | ||
</insert> | ||
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE configuration | ||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-config.dtd"> | ||
<configuration> | ||
<environments default="development"> | ||
<environment id="development"> | ||
<transactionManager type="JDBC"/> | ||
<dataSource type="POOLED"> | ||
<property name="driver" value="org.h2.Driver"/> | ||
<property name="url" value="jdbc:h2:file:./news"/> | ||
<!--<property name="username" value="${username}"/>--> | ||
<!--<property name="password" value="${password}"/>--> | ||
</dataSource> | ||
</environment> | ||
</environments> | ||
<mappers> | ||
<!--映射关系文件--> | ||
<mapper resource="db/mybatis/myMapper.xml"/> | ||
</mappers> | ||
</configuration> |