欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 杂记

程序员文章站 2024-03-13 15:34:03
...

追加数据到文件

try {
    File file = new File(getBasePath() + statistic + "ip.txt");
    // 如果文件不存在就会创建,然后追加数据
    FileWriter fw = new FileWriter(file, true);
    PrintWriter pw = new PrintWriter(fw);
    pw.println(ip);

    pw.flush();
    fw.flush();

    pw.close();
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
}

重写equals的要点

  1. 自反性 x.equals(x)为true
  2. 对称性 x.equals(y)为true y.equals(x)也为true
  3. 传递性 x.equals(y)为true y.equals(z)也为true 则 x.equals(z)为true
  4. 一致性 当两个对象均未被修改时,反复调用x.equals(y)总会返回相同的值
  5. 非空性 x.equals(null) 返回false
    private int equ;
    @Override
    public boolean equals(Object obj) {
        
        if(this == obj) return true;
        if(obj == null) return false;
        if(this.getClass() != obj.getClass()) return false;
        
        Equals that = (Equals) obj;
        if(this.equ != that.equ) return false;
        return true;
    }

BaseDao

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//import javax.naming.Context;
//import javax.naming.InitialContext;
//import javax.sql.DataSource;
/**打开和关闭数据库连接的工具类,基础Dao*/
public class BaseDao {
    private static final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=数据库名称";
    private static final String UNAME="sa";
    private static final String PWD="ok";
    static{//加载驱动,静态代码块,整个程序运行期间,仅会执行一次
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) { e.printStackTrace(); }
    }
    protected Connection conn;
    protected PreparedStatement ps;
    protected ResultSet rs;
    public void open(){/**打开数据库连接*/
        try {
//       Context context = new InitialContext();
//       DataSource ds=(DataSource)context.lookup("java:comp/env/数据池名称");
//       conn = ds.getConnection();
            conn = DriverManager.getConnection(URL, UNAME, PWD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void close(){
        try {
            if(rs!=null)   rs.close();
            if(ps!=null)   ps.close();
            if(conn!=null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public int update(String sql, Object... params) {
        int count = 0 ;
        try {
            open();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1, params[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            close();
        }

        return count;
    }

    public void query(String sql, Object... params) {
        try {
            open();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i+1, params[i]);
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}

MyBatisUtil

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtil {

    private static final String FILENAME ="mybatis_config.xml";
    private static SqlSessionFactory ssfactory;
    static{// 只在加载时创建一次工厂
        try {
            Reader reader = Resources.getResourceAsReader(FILENAME);
            ssfactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 线程变量, 在共有的资源中,为每一个线程分配一个独立的资源,防止本线程中的资源被另外一个资源使用
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
    /**
     * 打开SqlSession连接
     * @return
     */
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = threadLocal.get();
        if (sqlSession == null) {
            threadLocal.set(ssfactory.openSession());
            sqlSession = threadLocal.get();
        }
        return sqlSession;
    }
    /**
     * 关闭SqlSession连接
     */
    public static void closeSqlsession() {
        SqlSession sqlSession = threadLocal.get();
        if (sqlSession != null) {
            sqlSession.close();
            threadLocal.set(null);
        }
    }
}

Page工具类

import java.util.List;

public class Paging<T> {

    //页面大小常量
    public static final int PAGESIZE_5 = 5;
    public static final int PAGESIZE_10 = 10;
    public static final int PAGESIZE_15 = 15;
    public static final int PAGESIZE_20 = 20;

    private int pageSize;//每页行数
    private int totalCount;//总行数
    private int totalPageCount;//总页数
    private int currPageNo;//当前页数
    private List<T> data;

    public List<T> getData() {
        return data;
    }
    public void setData(List<T> data) {
        this.data = data;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        totalPageCount = (totalCount%pageSize == 0)? (totalCount/pageSize) : (totalCount/pageSize)+1;
    }
    public int getTotalPageCount() {
        return totalPageCount;
    }
    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }
    public int getCurrPageNo() {//----------------------------------
        // 因为totalCount可能因为查不到结果为0,所以totalPageCount计算出来为0 即使前面默认的currPageNo=1总是大于0
        // 所以currPageNo=totalPageCount=0 进入查询0-1=-1所以报错 加判断回避
        if(currPageNo < 1){
            currPageNo=1;
        }else if(currPageNo > totalPageCount && totalPageCount != 0 ){
            currPageNo=totalPageCount;
        }
        return currPageNo;
    }
    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }


}

转载于:https://www.jianshu.com/p/27a4765f935a