一般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();
将数据库model添加到配置中
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>();
privatestaticvolatile SqliteConnection conn;
private Session session;
private Map<String, Session> sessionMap = new HashMap<String, Session>();
private Transaction t;
privateboolean isAddClass = false;
private SqliteConnection(){}
publicstatic 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);
}
publicvoid setSession(Session session) {
this.session = session;
}
public Transaction getTransaction() {
return t;
}
publicvoid setTransaction(Transaction t) {
this.t = t;
}
/**
* 复制文件
* @param file1
* @param file2
* @throws IOException
*/
privatesynchronizedvoid 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();
}
}
}
/**
* 开启事务
*/
publicsynchronizedvoid openTransaction(String dbName){
SessionFactory sessionFactory = getSessionFactory(dbName);
Session session = sessionFactory.getCurrentSession();
Transaction t = session.beginTransaction();
transactionMap.put(dbName, t);
sessionMap.put(dbName, session);
}
/**
* 提交事务
*/
publicsynchronizedvoid commit(String uuid){
Transaction t = transactionMap.get(uuid);
if(t != null){
t.commit();
}
transactionMap.remove(uuid);
sessionMap.remove(uuid);
}
/**
* 事务回滚
*/
publicsynchronizedvoid rollBack(String uuid){
Transaction t = transactionMap.get(uuid);
if(t != null){
t.rollback();
}
transactionMap.remove(uuid);
sessionMap.remove(uuid);
}
/**
* 设置配置
* @param model
*/
privatesynchronizedvoid setCfg(){
loadProp();
cfg.setProperties(properties);
addCfgClass();
}
/**
* 设置配置的url
*/
privatesynchronizedvoid setCfgUrl(String dbName){
cfg.setProperty("hibernate.connection.url", prefix + path + dbName + ".db");
}
/**
* 添加配置的注解类
*/
privatesynchronizedvoid addCfgClass(){
if(!isAddClass){
cfg.addAnnotatedClass(Model.class);
isAddClass = true;
}
}
/**
* 获取sessionFactory
* @param model
* @return
*/
publicsynchronized SessionFactory getSessionFactory(String dbName){
SessionFactory factory = sessionFactoryMap.get(dbName);
if(factory == null){
conn.init(dbName);
factory = cfg.buildSessionFactory();
sessionFactoryMap.put(dbName, factory);
}
return factory;
}
/**
* 加载配置文件
*/
privatesynchronizedvoid 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();
}
}
/**
* 初始化数据
*/
privatesynchronizedvoid 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数据库了。