com.loy.upm.InitSql Maven / Gradle / Ivy
/*
* Copyright Loy Fu. 付厚俊
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.loy.upm;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.Statement;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.loy.e.core.api.InitDbService;
/**
*
* @author Loy Fu qq群 540553957 http://www.17jee.com
* @since 1.7
* @version 1.0.0
*
*/
@Service(value = "initDbService")
public class InitSql implements InitDbService {
static final Log logger = LogFactory.getLog(InitSql.class);
@Autowired
JdbcTemplate jdbcTemplate;
@PostConstruct
public void init() throws Exception {
int count = jdbcTemplate.queryForObject("select count(*) from loy_user", Integer.class);
Connection con = jdbcTemplate.getDataSource().getConnection();
con.setAutoCommit(false);
Statement statement = con.createStatement();
if (count == 0) {
ClassPathResource classPathResource = new ClassPathResource("e_init_ds.sql");
BufferedReader br = new BufferedReader(
new InputStreamReader(classPathResource.getInputStream(), "UTF-8"));
String s = null;
while ((s = br.readLine()) != null) {
if (StringUtils.isNotBlank(s) && !s.startsWith("--")) {
logger.info(s);
if (s.endsWith(";")) {
s = s.substring(0, s.length() - 1);
}
statement.execute(s);
}
}
con.commit();
con.close();
br.close();
}
}
}