公司orm框架解析

1.debug注意

  • 如果你的代码有错,打印的错误信息应该有你写的代码 .
  • 要从你的代码进去

2.实例化dao才会根据配置文件建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.esen.gxdc.dao;

import java.util.List;

import com.esen.gxdc.entity.BikeQRCodeInfo;
import com.esen.gxdc.util.Server;
import com.esen.jdbc.ConnectFactoryManagerImpl;
import com.esen.jdbc.ConnectionFactory;
import com.esen.jdbc.DefaultConnectionFactory;
import com.esen.jdbc.orm.EntityInfoManager;
import com.esen.jdbc.orm.EntityInfoManagerFactory;
import com.esen.jdbc.orm.Query;
import com.esen.jdbc.orm.QueryResult;
import com.esen.jdbc.orm.Session;
import com.esen.jdbc.orm.SessionFactory;
import com.esen.jdbc.orm.SessionFactoryBuilder;
import com.esen.jdbc.orm.helper.EntityAdvFunc;
import com.esen.util.exp.Expression;

public class BikeQRCodeInfoDao {

/**
* xml配置文件路径
*/
private static final String category_MAPPING = "/com/esen/mapping/bikeQRCodeInfo-mapping.xml";

/**
* 静态的私有对象实例
*/
private static BikeQRCodeInfoDao instance;

/**
* sessionFactory
*/
private SessionFactory sessionFactory;

/**
* entityName
*/
private static String entityName = "bikeQRCodeEntity";

/**
* 私有化构造方法,初始化
*/
private BikeQRCodeInfoDao() {
load();
repairTable();
}

/**
* 获得单一实例对象,主要线程安全
*
* @return
*/
public static synchronized BikeQRCodeInfoDao getInstance() {
if (instance == null) {
instance = new BikeQRCodeInfoDao();
}
return instance;
}

/**
* 初始化,连接池,sessionFactory
*/
private void load() {
// 创建连接池ConnectionFactory,以及连接池管理对象,将ConnectionFactory放入管理对象中,名称是“*”
// 以下代码服务器只会创建一次,如果已经创建过连接池,请不要重复创建
ConnectionFactory fct = Server.getInstance().getConnectionFactory();
ConnectFactoryManagerImpl fm = new ConnectFactoryManagerImpl();
fm.setConnectionFactory("*", fct);
DefaultConnectionFactory.set(fm);

// 根据xml文件,build解析成实体类的管理对象 EntityInfoManager
EntityInfoManager entityManager = EntityInfoManagerFactory
.buildFromURL(this.getClass().getResource(
"/com/esen/gxdc/mapping/bikeQRCodeInfo-mapping.xml"));

// 创建SessionFactory工厂
sessionFactory = SessionFactoryBuilder.build("*", entityManager);
}

/**
* 初始化时,修复表结构
*/
private void repairTable() {
Session session = sessionFactory.openSession();
try {
EntityAdvFunc.repairTable(session, entityName, null);
} finally {
session.close();
}
}

/**
* 添加数据
*
* @param s
*/
public void add(BikeQRCodeInfo s) {
Session session = sessionFactory.openSession();
try {
session.add(entityName, s);
} finally {
session.close();
}
}

/**
* 根据name查询信息
*
* @param name
* @return
*/
public List query(String cname) {
Session session = sessionFactory.openSession();
// Connection conn=session.getConnection();
try {
Query queryExe = session.createQuery(BikeQRCodeInfo.class, entityName);

// 这句话相当于 select id,name from tablename where name=? order by id asc
QueryResult<BikeQRCodeInfo> result = queryExe.query(new Expression(
"category_cname=?"), "category_cid=false", new String[] {
"category_cid", "category_cname" }, cname);

return result.list(-1, -1);
} finally {
session.close();
}
}

}

2.数据库映射配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<entity name="bikeQRCodeEntity" table="bike_QRCodeInfo" bean="com.esen.gxdc.entity.BikeQRCodeInfo">
<caches>
<cache name="cache" enable="false" maxsize="1000" idle="100" />
</caches>
<properties>
<property name="company" field="company_" caption="公司名称" captionkey="" nullable="false" length="100"/>
<property name="bikeId" field="bikeId_" caption="车号" captionkey="" nullable="true" length="200" />
<property name="userId" field="userId_" caption="用户id" captionkey="" nullable="true" length="100" />
<property name="uploadTime" field="uploadTime_" caption="上传时间" captionkey="" nullable="true" length="100" />
</properties>

</entity>
</mapping>

3.数据库配置文件

1
2
3
4
5
6
7
8
9
10
#charset UTF-8
#Tue Dec 04 15:33:36 CST 2018
logLevel=ERROR
catalog=
maxWait=10000
maxActive=20
password=1
url=jdbc\:oracle\:thin\:@172.21.11.41\:1521\:orcl
driverClassName=oracle.jdbc.driver.OracleDriver
username=whgxdc