sqlite学习-01集成hibernate

桂浩言
2023-12-01

  一般sqlite数据库,用作嵌入式数据库,用于安卓开发,本文应项目需求用于javaSE开发使用。


1.依赖:

		<dependency>
			<groupId>org.xerial</groupId>
			<artifactId>sqlite-jdbc</artifactId>
			<version>3.19.3</version>
		</dependency>

2.数据库连接:

由于我采用的数据库路径是数据库名称,且需要变化,所以没有采用配置数据元的方式。

2.1读取配置文件:

loadProp();

2.2添加注解类(数据库模型):

addCfgClass();

2.3初始化连接数据:

init();

2.4修改数据库连接路径:

setCfgUrl();

public class SqliteConnection {
	private InputStream dbStream;
	//db文件路径
	private String path;
	private String prefix;
	//配置文件
	private Properties properties = new Properties();
	private Configuration cfg = new Configuration();
	private Map<String, Transaction> transactionMap = new HashMap<String, Transaction>();
	private Map<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();
	
	private static volatile SqliteConnection conn;
	private Session session;
	private Map<String, Session> sessionMap = new HashMap<String, Session>();
	private Transaction t;
	private boolean isAddClass = false;
	
	private SqliteConnection(){}
	public static SqliteConnection getInstance(){
		if(conn == null){
			synchronized(SqliteConnection.class){
				if(conn == null){
					conn = new SqliteConnection();
					conn.setCfg();
				}
			}
		}
		return conn;
	}
	
	public Session getSession() {
		return session;
	}
	public Session getSession(String dbName) {
		return sessionMap.get(dbName);
	}
	public void setSession(Session session) {
		this.session = session;
	}
	public Transaction getTransaction() {
		return t;
	}
	public void setTransaction(Transaction t) {
		this.t = t;
	}
	/**
	 * 复制文件
	 * @param file1
	 * @param file2
	 * @throws IOException
	 */
	private synchronized void copyFile(String file1,String file2) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file1);
			fos = new FileOutputStream(file2);
			int temp;
			while((temp=fis.read())!=-1){
				fos.write(temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(fis != null) {
					fis.close();
				}
				if(fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	 }
	/**
	 * 开启事务
	 */
	public synchronized void openTransaction(String dbName){
		SessionFactory sessionFactory = getSessionFactory(dbName);
		Session session = sessionFactory.getCurrentSession();
		Transaction t = session.beginTransaction();
		transactionMap.put(dbName, t);
		sessionMap.put(dbName, session);
	}
	/**
	 * 提交事务
	 */
	public synchronized void commit(String uuid){
		Transaction t = transactionMap.get(uuid);
		if(t != null){
			t.commit();
		}
		transactionMap.remove(uuid);
		sessionMap.remove(uuid);
	}
	/**
	 * 事务回滚
	 */
	public synchronized void rollBack(String uuid){
		Transaction t = transactionMap.get(uuid);
		if(t != null){
			t.rollback();
		}
		transactionMap.remove(uuid);
		sessionMap.remove(uuid);
	}
	/**
	 * 设置配置
	 * @param model
	 */
	private synchronized void setCfg(){
		loadProp();
		cfg.setProperties(properties);
		addCfgClass();
	}
	/**
	 * 设置配置的url
	 */
	private synchronized void setCfgUrl(String dbName){
		cfg.setProperty("hibernate.connection.url", prefix + path + dbName + ".db");
	}
	/**
	 * 添加配置的注解类
	 */
	private synchronized void addCfgClass(){
		if(!isAddClass){
			cfg.addAnnotatedClass(Model.class);
			isAddClass = true;
		}
	}
	/**
	 * 获取sessionFactory
	 * @param model
	 * @return
	 */
	public synchronized SessionFactory getSessionFactory(String dbName){
		SessionFactory factory = sessionFactoryMap.get(dbName);
		if(factory == null){
			conn.init(dbName);
			factory = cfg.buildSessionFactory();
			sessionFactoryMap.put(dbName, factory);
		}
		return factory;
	}
	
	/**
	 * 加载配置文件
	 */
	private synchronized void loadProp(){
		try {
			//获取文件位置,转为流,并加载
			properties.load(SqliteConnection.class.getClassLoader().getResourceAsStream("sqlite.properties"));
			Properties platformProperties = new Properties();
			platformProperties.load(SqliteConnection.class.getClassLoader().getResourceAsStream("root.properties"));
			path = platformProperties.getProperty("root") + "/" ;
			prefix = properties.getProperty("prefix");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 初始化数据
	 */
	private synchronized void init(String dbName){
		/*
		 * 判断文件是否存在
		 * 如果不存在,复制数据库文件,并且重命名
		 */
		File file = new File(path + dbName + ".db");
		if(!file.exists()) {
			try {
					dbStream = SqliteConnection.class.getClassLoader().getResourceAsStream("sqlite.db");
				FileUtils.copyInputStreamToFile(dbStream, file);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					dbStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		setCfgUrl(dbName);
	}
	
	
}
最后添加手写的编程式事务,便可以使用hibernate连接sqlite数据库了。


 类似资料: