Commit e60b6e65 by Mark

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

parent ad1b4dbd
......@@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<version>2.0.6.RELEASE</version>
</parent>
<properties>
......@@ -19,13 +19,16 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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>
<commons.lang.version>2.6</commons.lang.version>
<commons.io.version>2.5</commons.io.version>
<commons.configuration.version>1.10</commons.configuration.version>
<fastjson.version>1.2.45</fastjson.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>
<dependencies>
......@@ -44,11 +47,6 @@
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
......@@ -78,6 +76,34 @@
<groupId>org.apache.velocity</groupId>
<version>${velocity.version}</version>
</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>
<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;
import com.alibaba.fastjson.JSON;
import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
......@@ -12,18 +19,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* 代码生成器
*
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午9:12:58
* @author Mark sunlightcs@gmail.com
*/
@Controller
@RequestMapping("/sys/generator")
......@@ -37,28 +40,23 @@ public class SysGeneratorController {
@ResponseBody
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> 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());
PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));
return R.ok().put("page", pageUtil);
}
/**
* 生成代码
*/
@RequestMapping("/code")
public void code(String tables, HttpServletResponse response) throws IOException{
byte[] data = sysGeneratorService.generatorCode(tables.split(","));
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
}
/**
* 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;
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.PageUtils;
import io.renren.utils.Query;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -14,29 +26,26 @@ import java.util.zip.ZipOutputStream;
/**
* 代码生成器
*
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午3:33:38
* @author Mark sunlightcs@gmail.com
*/
@Service
public class SysGeneratorService {
@Autowired
private SysGeneratorDao sysGeneratorDao;
private GeneratorDao generatorDao;
public List<Map<String, Object>> queryList(Map<String, Object> map) {
return sysGeneratorDao.queryList(map);
}
public PageUtils queryList(Query query) {
Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
List<Map<String, Object>> list = generatorDao.queryList(query);
public int queryTotal(Map<String, Object> map) {
return sysGeneratorDao.queryTotal(map);
return new PageUtils(list, (int)page.getTotal(), query.getLimit(), query.getPage());
}
public Map<String, String> queryTable(String tableName) {
return sysGeneratorDao.queryTable(tableName);
return generatorDao.queryTable(tableName);
}
public List<Map<String, String>> queryColumns(String tableName) {
return sysGeneratorDao.queryColumns(tableName);
return generatorDao.queryColumns(tableName);
}
public byte[] generatorCode(String[] tableNames) {
......
......@@ -152,7 +152,7 @@ public class GenUtils {
*/
public static String tableToJava(String tableName, String tablePrefix) {
if (StringUtils.isNotBlank(tablePrefix)) {
tableName = tableName.replace(tablePrefix, "" );
tableName = tableName.replaceFirst(tablePrefix, "" );
}
return columnToJava(tableName);
}
......
......@@ -5,17 +5,44 @@ server:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
#MySQL配置
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/renren_fast?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: renren
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:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
resources:
static-locations: classpath:/static/,classpath:/views/
# Mybatis配置
mybatis:
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"?>
<!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 table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
where table_schema = (select database())
select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables
where table_schema = (select database())
<if test="tableName != null and tableName.trim() != ''">
and table_name like concat('%', #{tableName}, '%')
and table_name like concat('%', #{tableName}, '%')
</if>
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 id="queryTable" 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()) and table_name = #{tableName}
</select>
</select>
<select id="queryColumns" resultType="map">
select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
......
<?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