Skip to content

Commit 7392dd9

Browse files
committed
read me
1 parent eb06707 commit 7392dd9

8 files changed

Lines changed: 224 additions & 23 deletions

File tree

README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,91 @@
1-
# openlibs.sql
2-
The lib of java database ORM simple implementation
1+
# openlibs.sql
2+
The lib of java database ORM simple implementation
3+
4+
标签(空格分隔): Github Java Readme
5+
6+
---
7+
8+
[TOC]
9+
#### **Session基本用法**
10+
```java
11+
// get session instance by its factory
12+
Session session = SessionFactory.getSession();
13+
```
14+
```java
15+
// fetch a entity by its key
16+
Message message = session.load(Message.class, "1000");
17+
//TODO
18+
session.close();// close session
19+
```
20+
```java
21+
// save a entity to table
22+
Message message = new Message();
23+
message.setLocale(Locale.CHINA.toString());
24+
message.setContent("HELLO");
25+
message.setMessageId("2000");
26+
session.save(message);
27+
session.close();
28+
```
29+
30+
#### **配置文件和类使用**
31+
32+
配置文件名称:    dbconfig.properties
33+
配置文件类:        Configurator
34+
配置文件工厂类: ConfiguratorFactory
35+
36+
##### **数据库属性文件**
37+
38+
格式如下:
39+
```
40+
#driver name
41+
DRIVER=com.mysql.jdbc.Driver
42+
#database url
43+
URL=jdbc:mysql://localhost:3306/opendb
44+
#database user
45+
USERNAME=root
46+
#database pass
47+
USERPWD=123456
48+
#databse dailect : MYSQL,ORACLE,SQLSERVER
49+
DIALECT=MYSQL
50+
```
51+
##### **配置类用法**
52+
53+
Configurator的属性一一对应配置文件dbconfig.properties里的键
54+
所以可以新建一个配置类实例,替代配置文件:
55+
``` java
56+
Configurator conf = new Configurator();
57+
conf.setDriver("xxx");
58+
conf.setUrl("xxx");
59+
conf.setUserName("xxx");
60+
conf.setUserPwd("xxx");
61+
// option, default value is false
62+
conf.setUsePool(true);
63+
// option, default value is Dialect.MYSQL
64+
conf.setDialect(Dialect.ORACLE);
65+
```
66+
ConfiguratorFactory类负责创建Configurator的实例
67+
```java
68+
// 方法1: 获得默认的配置类实例,将从默认配置文件dbconfig.properties中读取配置并实例化
69+
Configurator fileConf = ConfiguratorFactory.getDefaultInstance();
70+
// 方法2: 获得新的配置类实例,将复制已有的配置实例
71+
Configurator codeConf = ConfiguratorFactory.getInstance(conf);
72+
// 方法3: 重新设置ConfiguratorFactory的默认配置实例,指向已有的配置类实例
73+
ConfiguratorFactory.setConfigurator(conf);
74+
Configurator overrideConf = ConfiguratorFactory.getDefaultInstance();
75+
// 方法4: 读取外部文件实例化配置,配置文件名称可以自由命名
76+
File file = new File("C:\path\filename.properties");
77+
Configurator extenConf = ConfiguratorFactory.getInstance(file);
78+
```
79+
80+
#### **Session高级用法**
81+
##### **实体类定义**
82+
##### **增删改**
83+
84+
##### **查询**
85+
###### **直接SQL语句查询**
86+
###### **简单条件类查询**
87+
###### **智能QueryFilter查询**
88+
89+
##### **简单事务**
90+
91+
##### **连接池**

src/openthinks/libs/sql/dhibernate/support/DefaultSession.java renamed to src/openthinks/libs/sql/dhibernate/support/DefaultSessionImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* @author dmj
1010
* @version 17:20 2010/11/19
1111
*/
12-
class DefaultSession extends AbstractSession {
12+
class DefaultSessionImpl extends AbstractSession {
1313
private final BaseDao baseDao;
1414

15-
public DefaultSession() {
15+
public DefaultSessionImpl() {
1616
baseDao = new SessionDaoImpl();
1717
}
1818

19-
public DefaultSession(Configurator configurator) {
19+
public DefaultSessionImpl(Configurator configurator) {
2020
baseDao = new SessionDaoImpl();
2121
baseDao.setConfigurator(configurator);
2222
}

src/openthinks/libs/sql/dhibernate/support/SessionFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public static void setDefaultConfigurator(Configurator defaultConfigurator) {
3030
*/
3131
public static Session getSession() {
3232
if (defaultConfigurator == null)
33-
return new DefaultSession();
33+
return new DefaultSessionImpl();
3434
else
35-
return new DefaultSession(defaultConfigurator);
35+
return new DefaultSessionImpl(defaultConfigurator);
3636
}
3737

3838
/**
@@ -42,6 +42,6 @@ public static Session getSession() {
4242
* @return Session
4343
*/
4444
public static Session getSession(Configurator configurator) {
45-
return new DefaultSession(configurator);
45+
return new DefaultSessionImpl(configurator);
4646
}
4747
}

src/openthinks/libs/sql/lang/Configurator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,16 @@ public boolean isUsePool() {
124124
}
125125

126126
/**
127-
* 获取数据库方言类别, current support 3 dialect: mysql, oracle, sqlserver
128-
*
129-
* @return
127+
* 获取数据库方言类别, current support 3 dialect: mysql, oracle, sqlserver<BR>
128+
* The default value is Dialect.MYSQL
129+
* @return Dialect
130130
*/
131131
public Dialect getDialect() {
132-
return Dialect.toDialect(getProperty(DIALECT));
132+
try {
133+
return Dialect.toDialect(getProperty(DIALECT));
134+
} catch (Exception e) {
135+
return Dialect.MYSQL;
136+
}
133137
}
134138

135139
public void setDialect(Dialect dialect) {

test/openthinks/libs/sql/LoadTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* under the License.
1818
*
1919
* @Title: LoadTest.java
20-
* @Package sql
2120
* @Description: TODO
2221
* @author dailey
2322
* @date 2012-11-5
@@ -38,7 +37,7 @@
3837
*/
3938
public class LoadTest {
4039

41-
// @Test
40+
@Test
4241
public void testListEntity() {
4342
Session session = SessionFactory.getSession();
4443
List<MessageE> list = session.list(MessageE.class, "SELECT * FROM message");
@@ -49,7 +48,7 @@ public void testListEntity() {
4948

5049
}
5150

52-
// @Test
51+
@Test
5352
public void testJPAList() {
5453
Session session = SessionFactory.getSession();
5554
List<Message> list = session.list(Message.class, "SELECT * FROM message");
@@ -59,7 +58,7 @@ public void testJPAList() {
5958
session.close();
6059
}
6160

62-
// @Test
61+
@Test
6362
public void testListByClass() {
6463
Session session = SessionFactory.getSession();
6564
List<Message> list = session.list(Message.class);

test/openthinks/libs/sql/Message.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import javax.persistence.Column;
44
import javax.persistence.Entity;
5-
import javax.persistence.GeneratedValue;
6-
import javax.persistence.GenerationType;
75
import javax.persistence.Id;
86
import javax.persistence.Table;
97

@@ -12,7 +10,6 @@
1210
public class Message {
1311

1412
@Id
15-
@GeneratedValue(strategy = GenerationType.AUTO)
1613
@Column(name = "message_id")
1714
private String messageId;
1815
@Column(name = "message_locale")
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* @Title: QueryTest.java
20+
* @Package openthinks.libs.sql
21+
* @Description: TODO
22+
* @author dailey.yet@outlook.com
23+
* @date 2015年7月30日
24+
* @version V1.0
25+
*/
26+
package openthinks.libs.sql;
27+
28+
import java.util.List;
29+
import java.util.Locale;
30+
31+
import openthinks.libs.sql.dhibernate.Session;
32+
import openthinks.libs.sql.dhibernate.support.SessionFactory;
33+
import openthinks.libs.sql.dhibernate.support.query.Query;
34+
import openthinks.libs.sql.dhibernate.support.query.impl.EqualsFilter;
35+
import openthinks.libs.sql.dhibernate.support.query.impl.NotEqualsFliter;
36+
37+
import org.junit.After;
38+
import org.junit.Assert;
39+
import org.junit.Before;
40+
import org.junit.Test;
41+
42+
/**
43+
* @author dailey.yet@outlook.com
44+
*
45+
*/
46+
public class QueryTest {
47+
Session session;
48+
final Message testMsg = new Message();
49+
final Message testMsg2 = new Message();
50+
51+
/**
52+
* @throws java.lang.Exception
53+
*/
54+
@Before
55+
public void setUp() throws Exception {
56+
session = SessionFactory.getSession();
57+
session.disableAutoClose();
58+
testMsg.setMessageId("QueryTest_MSG_ID_1000");
59+
testMsg.setLocale(Locale.CHINA.toString());
60+
testMsg.setContent("简单测试");
61+
62+
testMsg2.setMessageId("QueryTest_MSG_ID_1000");
63+
testMsg2.setLocale(Locale.US.toString());
64+
testMsg2.setContent("Simple Test");
65+
session.save(testMsg);
66+
session.save(testMsg2);
67+
}
68+
69+
/**
70+
* @throws java.lang.Exception
71+
*/
72+
@After
73+
public void tearDown() throws Exception {
74+
session.delete(testMsg);
75+
session.delete(testMsg2);
76+
session.close();
77+
}
78+
79+
@Test
80+
public void simpleQuerytest() {
81+
82+
Query<Message> query = session.createQuery(Message.class);
83+
84+
List<Message> msgs = query.addFilter(new EqualsFilter().filter("messageId").eq(testMsg.getMessageId()))
85+
.execute();
86+
87+
Assert.assertEquals(2, msgs.size());
88+
89+
for (Message e : msgs) {
90+
Assert.assertEquals(testMsg.getMessageId(), e.getMessageId());
91+
}
92+
93+
}
94+
95+
@Test
96+
public void simpleQuerytest2() {
97+
98+
Query<Message> query = session.createQuery(Message.class);
99+
100+
List<Message> msgs = query.addFilter(new EqualsFilter().filter("messageId").eq(testMsg.getMessageId()))
101+
.addFilter(new NotEqualsFliter().filter("locale").neq(Locale.CHINA.toString())).execute();
102+
103+
Assert.assertEquals(1, msgs.size());
104+
105+
Message msg = msgs.get(0);
106+
107+
Assert.assertNotNull(msg);
108+
109+
Assert.assertEquals(Locale.US.toString(), msg.getLocale());
110+
111+
}
112+
}

test/openthinks/libs/sql/SaveTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
*/
3939
public class SaveTest {
4040

41-
// @Test
42-
public void testSave(){
41+
@Test
42+
public void testSave() {
4343
Session session = SessionFactory.getSession();
4444
Message message = new Message();
4545
message.setLocale(Locale.CHINA.toString());
@@ -48,9 +48,9 @@ public void testSave(){
4848
session.save(message);
4949
session.close();
5050
}
51-
51+
5252
@Test
53-
public void testUpdate(){
53+
public void testUpdate() {
5454
Session session = SessionFactory.getSession();
5555
Message message = new Message();
5656
message.setLocale(Locale.CHINA.toString());

0 commit comments

Comments
 (0)