Commit e60b6e65 by Mark

支持MySQL、Oracle、SQL Server、PostgreSQL数据库

parent ad1b4dbd
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version> <version>2.0.6.RELEASE</version>
</parent> </parent>
<properties> <properties>
...@@ -19,13 +19,16 @@ ...@@ -19,13 +19,16 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
<mysql.version>5.1.38</mysql.version>
<druid.version>1.0.28</druid.version> <druid.version>1.0.28</druid.version>
<commons.lang.version>2.6</commons.lang.version> <commons.lang.version>2.6</commons.lang.version>
<commons.io.version>2.5</commons.io.version> <commons.io.version>2.5</commons.io.version>
<commons.configuration.version>1.10</commons.configuration.version> <commons.configuration.version>1.10</commons.configuration.version>
<fastjson.version>1.2.45</fastjson.version> <fastjson.version>1.2.45</fastjson.version>
<velocity.version>1.7</velocity.version> <velocity.version>1.7</velocity.version>
<pagehelper.spring.boot.version>1.2.5</pagehelper.spring.boot.version>
<mysql.version>5.1.38</mysql.version>
<mssql.version>4.0</mssql.version>
<oracle.version>11.2.0.3</oracle.version>
</properties> </properties>
<dependencies> <dependencies>
...@@ -44,11 +47,6 @@ ...@@ -44,11 +47,6 @@
<version>${mybatis.spring.boot.version}</version> <version>${mybatis.spring.boot.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>druid</artifactId> <artifactId>druid</artifactId>
<version>${druid.version}</version> <version>${druid.version}</version>
...@@ -78,6 +76,34 @@ ...@@ -78,6 +76,34 @@
<groupId>org.apache.velocity</groupId> <groupId>org.apache.velocity</groupId>
<version>${velocity.version}</version> <version>${velocity.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.spring.boot.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- oracle驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
</dependency>
<!-- sqlserver驱动 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${mssql.version}</version>
</dependency>
<!-- postgresql驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.renren.config;
import io.renren.dao.*;
import io.renren.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* 数据库配置
*
* @author Mark sunlightcs@gmail.com
*/
@Configuration
public class DbConfig {
@Value("${renren.database: mysql}")
private String database;
@Autowired
private MySQLGeneratorDao mySQLGeneratorDao;
@Autowired
private OracleGeneratorDao oracleGeneratorDao;
@Autowired
private SQLServerGeneratorDao sqlServerGeneratorDao;
@Autowired
private PostgreSQLGeneratorDao postgreSQLGeneratorDao;
@Bean
@Primary
public GeneratorDao getGeneratorDao(){
if("mysql".equalsIgnoreCase(database)){
return mySQLGeneratorDao;
}else if("oracle".equalsIgnoreCase(database)){
return oracleGeneratorDao;
}else if("sqlserver".equalsIgnoreCase(database)){
return sqlServerGeneratorDao;
}else if("postgresql".equalsIgnoreCase(database)){
return postgreSQLGeneratorDao;
}else {
throw new RRException("不支持当前数据库:" + database);
}
}
}
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.renren.controller; package io.renren.controller;
import com.alibaba.fastjson.JSON;
import io.renren.service.SysGeneratorService; import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils; import io.renren.utils.PageUtils;
import io.renren.utils.Query; import io.renren.utils.Query;
...@@ -12,18 +19,14 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -12,18 +19,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* 代码生成器 * 代码生成器
* *
* @author chenshun * @author Mark sunlightcs@gmail.com
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午9:12:58
*/ */
@Controller @Controller
@RequestMapping("/sys/generator") @RequestMapping("/sys/generator")
...@@ -37,12 +40,7 @@ public class SysGeneratorController { ...@@ -37,12 +40,7 @@ public class SysGeneratorController {
@ResponseBody @ResponseBody
@RequestMapping("/list") @RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){ public R list(@RequestParam Map<String, Object> params){
//查询列表数据 PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));
Query query = new Query(params);
List<Map<String, Object>> list = sysGeneratorService.queryList(query);
int total = sysGeneratorService.queryTotal(query);
PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
return R.ok().put("page", pageUtil); return R.ok().put("page", pageUtil);
} }
......
/**
* Copyright 2018 人人开源 http://www.renren.io
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.renren.dao;
import java.util.List;
import java.util.Map;
/**
* 数据库接口
*
* @author Mark sunlightcs@gmail.com
* @since 2018-07-24
*/
public interface GeneratorDao {
List<Map<String, Object>> queryList(Map<String, Object> map);
Map<String, String> queryTable(String tableName);
List<Map<String, String>> queryColumns(String tableName);
}
package io.renren.dao;
import org.apache.ibatis.annotations.Mapper;
/**
* MySQL代码生成器
*
* @author Mark sunlightcs@gmail.com
* @since 2018-07-24
*/
@Mapper
public interface MySQLGeneratorDao extends GeneratorDao {
}
package io.renren.dao;
import org.apache.ibatis.annotations.Mapper;
/**
* Oracle代码生成器
*
* @author Mark sunlightcs@gmail.com
* @since 2018-07-24
*/
@Mapper
public interface OracleGeneratorDao extends GeneratorDao {
}
package io.renren.dao;
import org.apache.ibatis.annotations.Mapper;
/**
* PostgreSQL代码生成器
*
* @author Mark sunlightcs@gmail.com
* @since 2018-07-24
*/
@Mapper
public interface PostgreSQLGeneratorDao extends GeneratorDao {
}
package io.renren.dao;
import org.apache.ibatis.annotations.Mapper;
/**
* SQLServer代码生成器
*
* @author Mark sunlightcs@gmail.com
* @since 2018-07-24
*/
@Mapper
public interface SQLServerGeneratorDao extends GeneratorDao {
}
package io.renren.dao;
import java.util.List;
import java.util.Map;
/**
* 代码生成器
*
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午3:32:04
*/
public interface SysGeneratorDao {
List<Map<String, Object>> queryList(Map<String, Object> map);
int queryTotal(Map<String, Object> map);
Map<String, String> queryTable(String tableName);
List<Map<String, String>> queryColumns(String tableName);
}
/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.renren.service; package io.renren.service;
import io.renren.dao.SysGeneratorDao; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.renren.dao.GeneratorDao;
import io.renren.utils.GenUtils; import io.renren.utils.GenUtils;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -14,29 +26,26 @@ import java.util.zip.ZipOutputStream; ...@@ -14,29 +26,26 @@ import java.util.zip.ZipOutputStream;
/** /**
* 代码生成器 * 代码生成器
* *
* @author chenshun * @author Mark sunlightcs@gmail.com
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午3:33:38
*/ */
@Service @Service
public class SysGeneratorService { public class SysGeneratorService {
@Autowired @Autowired
private SysGeneratorDao sysGeneratorDao; private GeneratorDao generatorDao;
public List<Map<String, Object>> queryList(Map<String, Object> map) { public PageUtils queryList(Query query) {
return sysGeneratorDao.queryList(map); Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
} List<Map<String, Object>> list = generatorDao.queryList(query);
public int queryTotal(Map<String, Object> map) { return new PageUtils(list, (int)page.getTotal(), query.getLimit(), query.getPage());
return sysGeneratorDao.queryTotal(map);
} }
public Map<String, String> queryTable(String tableName) { public Map<String, String> queryTable(String tableName) {
return sysGeneratorDao.queryTable(tableName); return generatorDao.queryTable(tableName);
} }
public List<Map<String, String>> queryColumns(String tableName) { public List<Map<String, String>> queryColumns(String tableName) {
return sysGeneratorDao.queryColumns(tableName); return generatorDao.queryColumns(tableName);
} }
public byte[] generatorCode(String[] tableNames) { public byte[] generatorCode(String[] tableNames) {
......
...@@ -152,7 +152,7 @@ public class GenUtils { ...@@ -152,7 +152,7 @@ public class GenUtils {
*/ */
public static String tableToJava(String tableName, String tablePrefix) { public static String tableToJava(String tableName, String tablePrefix) {
if (StringUtils.isNotBlank(tablePrefix)) { if (StringUtils.isNotBlank(tablePrefix)) {
tableName = tableName.replace(tablePrefix, "" ); tableName = tableName.replaceFirst(tablePrefix, "" );
} }
return columnToJava(tableName); return columnToJava(tableName);
} }
......
...@@ -5,17 +5,44 @@ server: ...@@ -5,17 +5,44 @@ server:
spring: spring:
datasource: datasource:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
#MySQL配置
driverClassName: com.mysql.jdbc.Driver driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/renren_fast?useUnicode=true&characterEncoding=UTF-8&useSSL=false url: jdbc:mysql://localhost:3306/renren_fast?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: renren username: renren
password: 123456 password: 123456
#oracle配置
# driverClassName: oracle.jdbc.OracleDriver
# url: jdbc:oracle:thin:@47.100.206.162:1521:xe
# username: renren
# password: 123456
#SQLServer配置
# driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
# url: jdbc:sqlserver://192.168.10.10:1433;DatabaseName=renren_fast
# username: sa
# password: 123456
#PostgreSQL配置
# driverClassName: org.postgresql.Driver
# url: jdbc:postgresql://192.168.10.10:5432/renren_fast
# username: postgres
# password: 123456
jackson: jackson:
time-zone: GMT+8 time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss date-format: yyyy-MM-dd HH:mm:ss
resources: resources:
static-locations: classpath:/static/,classpath:/views/ static-locations: classpath:/static/,classpath:/views/
# Mybatis配置
mybatis: mybatis:
mapperLocations: classpath:mapper/**/*.xml mapperLocations: classpath:mapper/**/*.xml
pagehelper:
reasonable: true
supportMethodsArguments: true
params: count=countSql
#指定数据库,可选值有【mysql、oracle、sqlserver、postgresql】
renren:
database: mysql
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.renren.dao.SysGeneratorDao"> <mapper namespace="io.renren.dao.MySQLGeneratorDao">
<select id="queryList" resultType="map"> <select id="queryList" resultType="map">
select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
where table_schema = (select database()) where table_schema = (select database())
...@@ -9,16 +9,6 @@ ...@@ -9,16 +9,6 @@
and table_name like concat('%', #{tableName}, '%') and table_name like concat('%', #{tableName}, '%')
</if> </if>
order by create_time desc order by create_time desc
<if test="offset != null and limit != null">
limit #{offset}, #{limit}
</if>
</select>
<select id="queryTotal" resultType="int">
select count(*) from information_schema.tables where table_schema = (select database())
<if test="tableName != null and tableName.trim() != ''">
and table_name like concat('%', #{tableName}, '%')
</if>
</select> </select>
<select id="queryTable" resultType="map"> <select id="queryTable" resultType="map">
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.renren.dao.OracleGeneratorDao">
<resultMap id="tableMap" type="map">
<result column="TABLENAME" property="tableName"></result>
<result column="TABLECOMMENT" property="tableComment"></result>
<result column="COLUMNNAME" property="columnName"></result>
<result column="DATATYPE" property="dataType"></result>
<result column="COLUMNCOMMENT" property="columnComment"></result>
<result column="COLUMNKEY" property="columnKey"></result>
<result column="EXTRA" property="extra"></result>
<result column="CREATETIME" property="createTime"></result>
</resultMap>
<select id="queryList" resultMap="tableMap">
select dt.table_name tableName,
dtc.comments tableComment,
uo.created createTime
from user_tables dt,
user_tab_comments dtc,
user_objects uo
where dt.table_name = dtc.table_name and dt.table_name = uo.object_name and uo.object_type='TABLE'
<if test="tableName != null and tableName.trim() != ''">
and dt.table_name like concat('%', UPPER(#{tableName}))
</if>
order by uo.CREATED desc
</select>
<select id="queryTable" resultMap="tableMap">
select dt.table_name tableName,dtc.comments tableComment,dt.last_analyzed createTime from user_tables dt,user_tab_comments dtc where dt.table_name=dtc.table_name and dt.table_name = UPPER(#{tableName})
</select>
<select id="queryColumns" resultMap="tableMap">
select temp.column_name columnname,
temp.data_type dataType,
temp.comments columnComment,
case temp.constraint_type when 'P' then 'PRI' when 'C' then 'UNI' else '' end "COLUMNKEY",
'' "EXTRA"
from (
select col.column_id,
col.column_name,
col.data_type,
colc.comments,
uc.constraint_type,
-- 去重
row_number() over (partition by col.column_name order by uc.constraint_type desc) as row_flg
from user_tab_columns col
left join user_col_comments colc
on colc.table_name = col.table_name
and colc.column_name = col.column_name
left join user_cons_columns ucc
on ucc.table_name = col.table_name
and ucc.column_name = col.column_name
left join user_constraints uc
on uc.constraint_name = ucc.constraint_name
where col.table_name = upper(#{tableName})
) temp
where temp.row_flg = 1
order by temp.column_id
</select>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.renren.dao.PostgreSQLGeneratorDao">
<resultMap id="tableMap" type="map">
<result column="tablename" property="tableName"></result>
<result column="tablecomment" property="tableComment"></result>
<result column="columnname" property="columnName"></result>
<result column="datatype" property="dataType"></result>
<result column="columncomment" property="columnComment"></result>
<result column="columnkey" property="columnKey"></result>
<result column="extra" property="extra"></result>
<result column="createtime" property="createTime"></result>
</resultMap>
<select id="queryList" resultMap="tableMap">
select t1.tablename as tableName, obj_description(relfilenode, 'pg_class') as tableComment, now() as createTime from pg_tables t1, pg_class t2
where t1.tablename not like 'pg%' and t1.tablename not like 'sql_%' and t1.tablename = t2.relname
<if test="tableName != null and tableName.trim() != ''">
and t1.tablename like concat('%', #{tableName}, '%')
</if>
order by t1.tablename desc
</select>
<select id="queryTable" resultMap="tableMap">
select t1.tablename as tableName, obj_description(relfilenode, 'pg_class') as tableComment, now() as createTime from pg_tables t1, pg_class t2
where t1.tablename = #{tableName} and t1.tablename = t2.relname
</select>
<select id="queryColumns" resultMap="tableMap">
select t2.attname as columnName, pg_type.typname as dataType, col_description(t2.attrelid,t2.attnum) as columnComment, '' as extra,
(CASE t3.contype WHEN 'p' THEN 'PRI' ELSE '' END) as columnKey
from pg_class as t1, pg_attribute as t2 inner join pg_type on pg_type.oid = t2.atttypid
left join pg_constraint t3 on t2.attnum = t3.conkey[1] and t2.attrelid = t3.conrelid
where t1.relname = #{tableName} and t2.attrelid = t1.oid and t2.attnum>0
</select>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.renren.dao.SQLServerGeneratorDao">
<select id="queryList" resultType="map">
select * from
(
select cast(so.name as varchar(500)) as tableName, cast(sep.value as varchar(500)) as tableComment, getDate() as createTime
from sysobjects so
left JOIN sys.extended_properties sep
on sep.major_id=so.id and sep.minor_id=0
where (xtype='U' or xtype='v')
) t where 1=1
<if test="tableName != null and tableName.trim() != ''">
and t.tableName like concat('%', #{tableName}, '%')
</if>
order by t.tableName
</select>
<select id="queryTable" resultType="map">
select * from (
select cast(so.name as varchar(500)) as tableName, 'mssql' as engine,cast(sep.value as varchar(500)) as tableComment, getDate() as createTime
from sysobjects so
left JOIN sys.extended_properties sep on sep.major_id=so.id and sep.minor_id=0
where (xtype='U' or xtype='v')
) t where t.tableName=#{tableName}
</select>
<select id="queryColumns" resultType="map">
SELECT
cast(
b.NAME AS VARCHAR(500)
) AS columnName,
cast(
sys.types.NAME AS VARCHAR(500)
) AS dataType,
cast(
c.VALUE AS VARCHAR(500)
) AS columnComment,
(
SELECT
CASE
count( 1 )
WHEN 1 then 'PRI'
ELSE ''
END
FROM
syscolumns,
sysobjects,
sysindexes,
sysindexkeys,
systypes
WHERE
syscolumns.xusertype = systypes.xusertype
AND syscolumns.id = object_id(A.NAME)
AND sysobjects.xtype = 'PK'
AND sysobjects.parent_obj = syscolumns.id
AND sysindexes.id = syscolumns.id
AND sysobjects.NAME = sysindexes.NAME
AND sysindexkeys.id = syscolumns.id
AND sysindexkeys.indid = sysindexes.indid
AND syscolumns.colid = sysindexkeys.colid
AND syscolumns.NAME = B.NAME
) as columnKey,
'' as extra
FROM
(
select
name,
object_id
from
sys.tables
UNION all select
name,
object_id
from
sys.views
) a
INNER JOIN sys.COLUMNS b ON
b.object_id = a.object_id
LEFT JOIN sys.types ON
b.user_type_id = sys.types.user_type_id
LEFT JOIN sys.extended_properties c ON
c.major_id = b.object_id
AND c.minor_id = b.column_id
WHERE
a.NAME = #{tableName}
and sys.types.NAME != 'sysname'
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment