|
自己也是多方了解,经实际使用打通了这个小关节,现在分享给大家。
第一步,建立一个方言类:
package org.jeecgframework.core.common.hibernate.dialect;
import java.sql.Types;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.type.StringType;
/**
* ClassName: MySQLServer2008Dialect
* @Description: TODO
* @author yokoboy
* @date 2016-6-7
*/
public class MySQLServer2008Dialect extends SQLServer2008Dialect {
public MySQLServer2008Dialect() {
super();
registerHibernateType(Types.NVARCHAR, StringType.INSTANCE.getName());
}
}
第二步修改类DBTypeUtil
package org.jeecgframework.core.util;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.jeecgframework.web.cgform.controller.autolist.CgAutoListController;
import org.jeecgframework.web.system.listener.OnlineListener;
/**
*
* @author 张代浩
*
*/
public class DBTypeUtil {
private static Logger log = Logger.getLogger(DBTypeUtil.class);
/**
* 获取数据库类型
* @return
*/
public static String getDBType(){
String retStr="";
ApplicationContext ctx = OnlineListener.getCtx();
if (ctx==null) {
return retStr;//如果ctx为空,则服务器异常了
}else{
org.springframework.orm.hibernate4.LocalSessionFactoryBean sf = (org.springframework.orm.hibernate4.LocalSessionFactoryBean)ctx.getBean("&sessionFactory");
String dbdialect = sf.getHibernateProperties().getProperty("hibernate.dialect");
log.debug(dbdialect);
if (dbdialect.equals("org.hibernate.dialect.MySQLDialect")) {
retStr="mysql";
}else if (dbdialect.contains("Oracle")) {//oracle有多个版本的方言
retStr = "oracle";
}else if (dbdialect.equals("org.hibernate.dialect.SQLServerDialect")) {
retStr = "sqlserver";
}else if (dbdialect.equals("org.hibernate.dialect.PostgreSQLDialect")) {
retStr = "postgres";
}
// org.jeecgframework.core.common.hibernate.dialect.MySQLServer2008Dialect
else if (dbdialect.equals("org.jeecgframework.core.common.hibernate.dialect.MySQLServer2008Dialect")) {
retStr = "sqlserver";
}
return retStr;
}
}
}
第三步:修改类DbTableUtil
package org.jeecgframework.web.cgform.service.impl.config.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jeecgframework.web.cgform.service.config.DbTableHandleI;
import org.jeecgframework.web.cgform.service.config.DbTableServiceI;
import org.jeecgframework.web.cgform.service.impl.config.DbTableMysqlHandleImpl;
import org.jeecgframework.web.cgform.service.impl.config.DbTableOracleHandleImpl;
import org.jeecgframework.web.cgform.service.impl.config.DbTablePostgresHandleImpl;
import org.jeecgframework.web.cgform.service.impl.config.DbTableServiceMysqlImpl;
import org.jeecgframework.web.cgform.service.impl.config.TableSQLServerHandleImpl;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
/**
* 数据库工具类
* @author jueyue
* 2013年7月6日
*/
public class DbTableUtil {
/**
* 获取列的Map
* key 是 cloumn_name value 是 List<Map<String, Object>>
* @param queryForList
* @return
*/
public static Map<String, Object> getColumnMap(
List<Map<String, Object>> queryForList) {
Map<String, Object> columnMap = new HashMap<String, Object>();
for(int i =0 ;i<queryForList.size();i++){
columnMap.put(queryForList.get(i).get("column_name").toString(), queryForList.get(i));
}
return columnMap;
}
/**
* 把配置中的字段翻译成数据库中的字段 如:A ---> _a
* @param fileName
* @return
*/
public static String translatorToDbField(String fileName){
//去掉转换
return fileName;
// String name = "";
// char[] chars = fileName.toCharArray();
// for(int i =0 ;i<chars.length;i++){
// name+= chars[i]>'A'&&chars[i]<'Z'?("_"+Character.toLowerCase(chars[i])):chars[i];
// }
// return name;
}
/**
* 获取DB 维护表的工具类
* @return
*/
public static DbTableServiceI getTableUtil(Session session) {
DbTableServiceI tableUtil = null;
String dialect = ((SessionImpl)session).getFactory().getDialect()
.getClass().getName();
if (dialect.equals("org.hibernate.dialect.MySQLDialect")) {
tableUtil = new DbTableServiceMysqlImpl();
}
return tableUtil;
}
public static DbTableHandleI getTableHandle(Session session) {
DbTableHandleI dbTableHandle = null;
String dialect = ((SessionImpl)session).getFactory().getDialect()
.getClass().getName();
if (dialect.equals("org.hibernate.dialect.MySQLDialect")) {
dbTableHandle = new DbTableMysqlHandleImpl();
}else if (dialect.contains("Oracle")) {
dbTableHandle = new DbTableOracleHandleImpl();
}else if (dialect.equals("org.hibernate.dialect.PostgreSQLDialect")) {
dbTableHandle = new DbTablePostgresHandleImpl();
}else if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) {
dbTableHandle = new TableSQLServerHandleImpl();
}
else if (dialect.equals("org.jeecgframework.core.common.hibernate.SQLServer2008Dialect")) {
dbTableHandle = new TableSQLServerHandleImpl();
}
return dbTableHandle;
}
/**
* 数据库类型
* @param session
* @return
*/
public static String getDataType(Session session){
String dataType="MYSQL";
String dialect = ((SessionImpl)session).getFactory().getDialect()
.getClass().getName();
if (dialect.equals("org.hibernate.dialect.MySQLDialect")) {
dataType="MYSQL";
}else if (dialect.contains("Oracle")) {
dataType="ORACLE";
}else if (dialect.equals("org.hibernate.dialect.PostgreSQLDialect")) {
dataType = "POSTGRESQL";
}else if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) {
dataType="SQLSERVER";
}
else if (dialect.equals("org.jeecgframework.core.common.hibernate.SQLServer2008Dialect")) {
dataType="SQLSERVER";
}
return dataType;
}
}
每四步,修改配置:jeecg_database.properties#sqlserver
diver_name=org.jeecgframework.core.common.hibernate.dialect.MySQLServer2008Dialect
url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jeecg
username=sa
password=123456
database_name=jeecg
第五步,修改配置:dbconfig.properties
#SQLServer2005\u4ee5\u4e0a
hibernate.dialect=org.jeecgframework.core.common.hibernate.dialect.MySQLServer2008Dialect
validationQuery.sqlserver=SELECT 1
jdbc.url.jeecg=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jeecg
jdbc.username.jeecg=sa
jdbc.password.jeecg=123456
jdbc.dbType=sqlserver
第六步,重启一下ECLIPSE,享受开发的快乐吧! |
|