java 调用ORMlite 实现sqlite 本地文件对象存储
程序员文章站
2022-07-02 22:03:20
...
java 调用ORMlite 实现sqlite 本地文件对象存储,原创,转载请注明出处!
SqliteHelper.java
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:
ormlite-core-5.0.jar
ormlite-jdbc-5.0.jar
sqlite-jdbc-3.21.0.1.jar