提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.db.service; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.guns.base.db.context.SqlSessionFactoryContext; |
|
4 |
import cn.stylefeng.guns.base.db.entity.DatabaseInfo; |
|
5 |
import cn.stylefeng.guns.base.db.exception.DataSourceInitException; |
|
6 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
|
7 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
|
8 |
import cn.stylefeng.guns.db.mapper.DatabaseInfoMapper; |
|
9 |
import cn.stylefeng.guns.base.db.model.params.DatabaseInfoParam; |
|
10 |
import cn.stylefeng.guns.base.db.model.result.DatabaseInfoResult; |
|
11 |
import cn.stylefeng.guns.base.db.service.DatabaseInfoService; |
|
12 |
import cn.stylefeng.guns.base.tenant.consts.TenantConstants; |
|
13 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
14 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
15 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
16 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
17 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
18 |
import org.apache.ibatis.session.SqlSessionFactory; |
|
19 |
import org.springframework.stereotype.Service; |
|
20 |
import org.springframework.transaction.annotation.Transactional; |
|
21 |
|
|
22 |
import java.io.Serializable; |
|
23 |
import java.sql.Connection; |
|
24 |
import java.sql.DriverManager; |
|
25 |
import java.sql.SQLException; |
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
/** |
|
29 |
* <p> |
|
30 |
* 数据库信息表 服务实现类 |
|
31 |
* </p> |
|
32 |
* |
|
33 |
* @author stylefeng |
|
34 |
* @since 2019-06-15 |
|
35 |
*/ |
|
36 |
@Service |
|
37 |
public class DatabaseInfoServiceImpl extends ServiceImpl<DatabaseInfoMapper, DatabaseInfo> implements DatabaseInfoService { |
|
38 |
|
|
39 |
@Override |
|
40 |
@Transactional(rollbackFor = Exception.class) |
|
41 |
public void add(DatabaseInfoParam param) { |
|
42 |
|
|
43 |
//判断数据库连接是否可用 |
|
44 |
Connection conn = null; |
|
45 |
try { |
|
46 |
Class.forName(param.getJdbcDriver()); |
|
47 |
conn = DriverManager.getConnection( |
|
48 |
param.getJdbcUrl(), param.getUserName(), param.getPassword()); |
|
49 |
} catch (Exception e) { |
|
50 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.INIT_DATASOURCE_ERROR); |
|
51 |
}finally { |
|
52 |
if (conn != null) { |
|
53 |
try { |
|
54 |
conn.close(); |
|
55 |
} catch (SQLException e) { |
|
56 |
e.printStackTrace(); |
|
57 |
} |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
//判断dbName是否重复 |
|
62 |
String dbName = param.getDbName(); |
|
63 |
List<DatabaseInfo> db_name = this.list(new QueryWrapper<DatabaseInfo>().eq("db_name", dbName)); |
|
64 |
if (db_name.size() > 0) { |
|
65 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.REPEAT_ERROR); |
|
66 |
} |
|
67 |
|
|
68 |
//数据库中插入记录 |
|
69 |
DatabaseInfo entity = getEntity(param); |
|
70 |
this.save(entity); |
|
71 |
|
|
72 |
//先判断context中是否有了这个数据源名称 |
|
73 |
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryContext.getSqlSessionFactorys().get(param.getDbName()); |
|
74 |
if (sqlSessionFactory != null) { |
|
75 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.NAME_REPEAT_ERROR); |
|
76 |
} |
|
77 |
|
|
78 |
//往上下文中添加数据源 |
|
79 |
SqlSessionFactoryContext.addSqlSessionFactory(param.getDbName(), entity); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public void delete(DatabaseInfoParam param) { |
|
84 |
|
|
85 |
//如果是租户数据库不能删除 |
|
86 |
DatabaseInfo databaseInfo = this.getById(param.getDbId()); |
|
87 |
if (databaseInfo.getDbName().startsWith(TenantConstants.TENANT_DB_PREFIX)) { |
|
88 |
throw new DataSourceInitException(DataSourceInitException.ExEnum.DELETE_TENANT_ERROR); |
|
89 |
} |
|
90 |
|
|
91 |
this.removeById(getKey(param)); |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public void update(DatabaseInfoParam param) { |
|
96 |
DatabaseInfo oldEntity = getOldEntity(param); |
|
97 |
DatabaseInfo newEntity = getEntity(param); |
|
98 |
ToolUtil.copyProperties(newEntity, oldEntity); |
|
99 |
this.updateById(newEntity); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public DatabaseInfoResult findBySpec(DatabaseInfoParam param) { |
|
104 |
return null; |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public List<DatabaseInfoResult> findListBySpec(DatabaseInfoParam param) { |
|
109 |
return null; |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public LayuiPageInfo findPageBySpec(DatabaseInfoParam param) { |
|
114 |
Page pageContext = getPageContext(); |
|
115 |
IPage page = this.baseMapper.customPageList(pageContext, param); |
|
116 |
return LayuiPageFactory.createPageInfo(page); |
|
117 |
} |
|
118 |
|
|
119 |
private Serializable getKey(DatabaseInfoParam param) { |
|
120 |
return param.getDbId(); |
|
121 |
} |
|
122 |
|
|
123 |
private Page getPageContext() { |
|
124 |
return LayuiPageFactory.defaultPage(); |
|
125 |
} |
|
126 |
|
|
127 |
private DatabaseInfo getOldEntity(DatabaseInfoParam param) { |
|
128 |
return this.getById(getKey(param)); |
|
129 |
} |
|
130 |
|
|
131 |
private DatabaseInfo getEntity(DatabaseInfoParam param) { |
|
132 |
DatabaseInfo entity = new DatabaseInfo(); |
|
133 |
ToolUtil.copyProperties(param, entity); |
|
134 |
return entity; |
|
135 |
} |
|
136 |
|
|
137 |
} |