java 调用ORMlite 实现sqlite 本地文件对象存储_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 调用ORMlite 实现sqlite 本地文件对象存储

java 调用ORMlite 实现sqlite 本地文件对象存储

 2018/4/27 18:56:28  阿拉扫思密达  程序员俱乐部  我要评论(0)
  • 摘要:java调用ORMlite实现sqlite本地文件对象存储,原创,转载请注明出处!SqliteHelper.javapackageannhoa.sqlite;importjava.sql.SQLException;importjava.util.Collection;importjava.util.List;importjava.util.logging.Logger;importcom.j256.ormlite.dao.Dao;importcom.j256.ormlite.dao
  • 标签:实现 文件 Java SQL

?

java 调用ORMlite 实现sqlite 本地文件对象存储,原创,转载请注明出处!

SqliteHelper.java

?

class="java" name="code">package annhoa.sqlite;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * @author annhoa
 * @param <T>
 * @date create 2018/1/19 update 2018/4/27
 * @decript sqlite 
 */
@SuppressWarnings({"unchecked", "rawtypes", "static-access"})
public class SqliteHelper {
    private static final Logger logger = Logger.getLogger(SqliteHelper.class.toString());
    private static Dao daoSupport;

    private static String SQLPATH = "jdbc:sqlite:sqlite.db";
    
    public static <T> Dao getDaoSupport(Class<T> clazz) throws SQLException {
        daoSupport = DaoManager.createDao(getConnectionSource(), clazz);
        return daoSupport;
    }

    public void setDaoSupport(Dao daoSupport) {
        this.daoSupport = daoSupport;
    }

    public static <T> void init(Class<T> clazz) {
        try {
            TableUtils.createTableIfNotExists(getConnectionSource(), clazz);
        } catch (Exception exception) {
            logger.info("创建表失败:{}"+ exception.getMessage());
        }
    }

    public static ConnectionSource getConnectionSource() throws SQLException {
        return new JdbcConnectionSource(SQLPATH);
    }
    
    
    /**
	 * 新增
     * @param <T>
	 * 
	 * @param t
	 *            泛型对象
	 * @throws SQLException
	 */
	public <T> void save(T t) throws SQLException {
		init(t.getClass());
		this.getDaoSupport(t.getClass()).create(t);
	}

	/**
	 * 根据Id删除
	 * @param <T>
	 *
	 * @param id
	 *            对象id参数
	 * @return
	 * @throws SQLException
	 */
	public <T> int deleteById(T t,Integer id) throws SQLException {
		return this.getDaoSupport(t.getClass()).deleteById(id);
	}

	/**
	 * 根据对象删除
	 * @param <T>
	 * 
	 * @param t
	 *            泛型对象
	 * @return
	 * @throws SQLException
	 */
	public <T> int deleteById(T t) throws SQLException {
		return this.getDaoSupport(t.getClass()).delete(t);
	}

	/**
	 * 根据多个对象ID批量删除
	 * @param <T>
	 * 
	 * @param ids
	 *            对象id参数集合
	 * @return
	 * @throws SQLException
	 */
	public <T> int deleteByIds(T t , Collection<Integer> ids) throws SQLException {
		return this.getDaoSupport(t.getClass()).deleteIds(ids);
	}

	/**
	 * 更新对象
	 * @param <T>
	 * 
	 * @param t
	 *            泛型对象
	 * @return
	 * @throws SQLException
	 */
	public <T> int update(T t) throws SQLException {
		return this.getDaoSupport(t.getClass()).update(t);
	}

	/**
	 * 根据对象id更新对象
	 * @param <T>
	 * 
	 * @param t
	 *            泛型对象
	 * @param id
	 *            对象id参数
	 * @return
	 * @throws SQLException
	 */
	public <T> int update(T t, Integer id) throws SQLException {
		return this.getDaoSupport(t.getClass()).updateId(t, id);
	}

	/**
	 * 根据对象id查询对象
	 * @param <T>
	 * 
	 * @param id
	 *            对象id参数
	 * @return
	 * @throws SQLException
	 */
	public <T> T queryForId(T t, Integer id) throws SQLException {
		return  (T) this.getDaoSupport(t.getClass()).queryForId(id);
	}

	/**
	 * 查询所有列表
	 * @param <T>
	 * @param <T>
	 * 
	 * @return
	 * @throws SQLException
	 */
	public <T> List<T> queryForAll(T t) throws SQLException {
		return this.getDaoSupport(t.getClass()).queryForAll();
	}

    
}

?

?

SqliteEntity.java

package annhoa.sqlite;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "sqliteEntity")
public class SqliteEntity {
	@DatabaseField(generatedId = true)
	private int id;
	@DatabaseField(columnName = "name")
	private String name;
	@DatabaseField(columnName = "address")
	private String address;
	public SqliteEntity() {
		super();
	}
	
	public SqliteEntity(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
}

?

SqliteTest.java

package annhoa.sqlite;

import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SqliteTest {

	public  static void main(String[] args) {
		SqliteHelper  sqliteHelper= new SqliteHelper();
		
		try {
//			SqliteEntity sqliteEntity = new SqliteEntity( "axiba0", "ajax:java:127.0.0.0");
//			SqliteEntity sqliteEntity1 = new SqliteEntity( "axiba1", "ajax:java:127.0.0.1");
//			sqliteHelper.save( sqliteEntity);
//			sqliteHelper.save( sqliteEntity1);
			List<SqliteEntity> list= sqliteHelper.queryForAll(new SqliteEntity());
			System.out.println(list.size());	
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i).getId());	
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

?

所需jar:

?

上一篇: thinkphp——上传新图并且删除旧图的操作 下一篇: 没有下一篇了!
发表评论
用户名: 匿名