基于中科视拓的seetaface6封装的免费人脸识别项目后端接口
shentao
2025-09-22 4e24fd913e7b048436aa7e5001cf875baac81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.code2roc.fastface.config;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.code2roc.fastface.db.MasterJdbcTemplate;
import com.code2roc.fastface.db.PackagesSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteOpenMode;
 
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
 
@Configuration
public class DataSourceConfig {
    @Value("${datasource.master.driverClassName}")
    private String driverClassName;
    @Value("${datasource.master.url}")
    private String dbUrl;
    @Value("${datasource.master.username}")
    private String username;
    @Value("${datasource.master.password}")
    private String password;
 
    @Bean
    @Primary
    @Qualifier("masterDataSource")
    public DataSource masterDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        if(driverClassName.equals("com.mysql.cj.jdbc.Driver")){
            if(!dbUrl.contains("useOldAliasMetadataBehavior")){
                dbUrl += "&useOldAliasMetadataBehavior=true";
            }
            if(!dbUrl.contains("useAffectedRows")){
                dbUrl += "&useAffectedRows=true";
            }
        }
        if(driverClassName.equals("org.sqlite.JDBC")){
            initSqliteFile(dbUrl.replace("jdbc:sqlite:",""));
            SQLiteConfig config= new SQLiteConfig();
            config.setOpenMode(SQLiteOpenMode.OPEN_URI);
            config.setOpenMode(SQLiteOpenMode.READWRITE);
            config.setOpenMode(SQLiteOpenMode.SHAREDCACHE);
            config.setOpenMode(SQLiteOpenMode.NOMUTEX);
            datasource.addConnectionProperty(SQLiteConfig.Pragma.OPEN_MODE.pragmaName, String.valueOf(config.getOpenModeFlags()));
            datasource.addConnectionProperty(SQLiteConfig.Pragma.JOURNAL_MODE.pragmaName, SQLiteConfig.JournalMode.WAL.toString() );
        }
        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        //configuration
        datasource.setInitialSize(1);
        datasource.setMinIdle(3);
        datasource.setMaxActive(20);
        datasource.setMaxWait(60000);
        datasource.setTimeBetweenEvictionRunsMillis(60000);
        datasource.setMinEvictableIdleTimeMillis(60000);
        datasource.setValidationQuery("select 'x'");
        datasource.setTestWhileIdle(true);
        datasource.setTestOnBorrow(false);
        datasource.setTestOnReturn(false);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(20);
        if(driverClassName.equals("org.sqlite.JDBC")){
            datasource.setPoolPreparedStatements(false);
        }else{
            datasource.setPoolPreparedStatements(true);
        }
        datasource.setLogAbandoned(false); //移除泄露连接发生是是否记录日志
 
        try {
            if(!driverClassName.equals("org.sqlite.JDBC")){
                datasource.setFilters("stat,slf4j");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        datasource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");//connectionProperties);
 
        return datasource;
    }
 
    public static void initSqliteFile(String filePath){
        File file = new File(filePath);
 
        File dir = file.getParentFile();
        if(!dir.exists()){
            dir.mkdirs();
        }
 
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    @Primary
    @Bean("masterTransactionManager")
    public DataSourceTransactionManager MasterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }
 
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("allow", ""); //白名单
        reg.addInitParameter("loginUsername", "admin123");
        reg.addInitParameter("loginPassword", "abcd@1234");
        return reg;
    }
 
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        final PackagesSqlSessionFactoryBean sessionFactory = new PackagesSqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource());
        //关闭驼峰转换,防止带下划线的字段无法映射
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(false);
        MybatisConfiguration mybatisConfiguration =  sessionFactory.getConfiguration();
        if(mybatisConfiguration==null){
            mybatisConfiguration = new MybatisConfiguration();
        }
        sessionFactory.setConfiguration(mybatisConfiguration);
        return sessionFactory.getObject();
    }
 
    @Bean
    public MasterJdbcTemplate jdbcTemplate(){
        MasterJdbcTemplate jdbcTemplate = null;
        try{
            jdbcTemplate = new MasterJdbcTemplate(masterDataSource());
        }catch (Exception e){
            e.printStackTrace();
        }
        return jdbcTemplate;
    }
}