MyBatisCrawlerDao.java 3.2 KB
Newer Older
亦蔚然's avatar
亦蔚然 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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);
        }
    }
}