Browse Source

完善配置

master
453530270@qq.com 2 years ago
parent
commit
9f7f796507
  1. 12
      pom.xml
  2. 21
      src/main/java/io/xtfs/jwebfs/DataSourceConfig.java
  3. 114
      src/main/java/io/xtfs/jwebfs/configure/DruidConfig.java
  4. 27
      src/main/java/io/xtfs/jwebfs/constant/ChConstant.java
  5. 41
      src/main/java/io/xtfs/jwebfs/mapper/WbFileMapper.java
  6. 78
      src/main/resources/mapper/webfs/WbFileMapper.xml
  7. 38
      src/test/java/io/xtfs/jwebfs/mapper/WbFileMapperTest.java

12
pom.xml

@ -35,6 +35,12 @@
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>2.0.7</version> <version>2.0.7</version>
</dependency> </dependency>
<!-- ali jars -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency> <dependency>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId> <artifactId>HikariCP</artifactId>
@ -65,6 +71,12 @@
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

21
src/main/java/io/xtfs/jwebfs/DataSourceConfig.java

@ -1,21 +0,0 @@
package io.xtfs.jwebfs;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource(Environment env){
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
ds.setUsername(env.getProperty("spring.datasource.username"));
ds.setPassword(env.getProperty("spring.datasource.password"));
ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
return ds;
}
}

114
src/main/java/io/xtfs/jwebfs/configure/DruidConfig.java

@ -0,0 +1,114 @@
package io.xtfs.jwebfs.configure;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import io.xtfs.jwebfs.constant.ChConstant;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.io.FileReader;
import java.util.Properties;
/**
* druid 数据库链接配置
* 涉及多数据源此处需要明确指定扫描包的位置
*/
@Configuration
@MapperScan(basePackages = {"io.xtfs.jwebfs.mapper"},sqlSessionFactoryRef = "chSqlSessionFactory")
public class DruidConfig {
private Log log = LogFactory.getLog(DruidConfig.class); //初始化日志
static final String CHMAPPER_LOCATION = "classpath:mapper/webfs/*.xml";
/**
* 配置为主要数据源
* @return
* @throws Exception
*/
@Bean(name = "chDataSource")
@Qualifier("chDataSource")
public DataSource druidDataSource() throws Exception{
//配置文件的真实路径
String dbconf = ChConstant.WORK_DIR + ChConstant.DB_CONF;
Properties prop = new Properties();
prop.load(new FileReader(dbconf));
log.info("master druid:"+dbconf);
//使用datasource方式
DruidDataSource dataSource = new DruidDataSource();
String dburl = prop.getProperty("druid.url");
dataSource.setUrl(dburl);
dataSource.setUsername(prop.getProperty("druid.username"));
dataSource.setPassword(prop.getProperty("druid.password"));
return dataSource;
}
@Bean
public ServletRegistrationBean<Servlet> druidServlet() {
// 进行 druid 监控的配置处理
ServletRegistrationBean<Servlet> srb = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
// 白名单
srb.addInitParameter("allow", "127.0.0.1");
// 黑名单
//srb.addInitParameter("deny", "192.168.31.253");
// 用户名
//srb.addInitParameter("loginUsername", "root");
// 密码
//srb.addInitParameter("loginPassword", "root");
// 是否可以重置数据源
srb.addInitParameter("resetEnable", "false");
log.info("druid监控配置:" + srb);
return srb;
}
@Bean
public FilterRegistrationBean<Filter> filterRegistrationBean() {
FilterRegistrationBean<Filter> frb = new FilterRegistrationBean<>();
frb.setFilter(new WebStatFilter());
// 所有请求进行监控处理
frb.addUrlPatterns("/*");
// 排除名单
frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
return frb;
}
@Bean(name="chSqlSessionFactory")
public SqlSessionFactory aisSqlSessionFactory(@Qualifier("chDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DruidConfig.CHMAPPER_LOCATION));
log.info("chSqlSessionFactory"+bean.toString());
return bean.getObject();
}
//配置事务
@Bean(name="transactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("chDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
//
@Bean(name="sqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("chSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

27
src/main/java/io/xtfs/jwebfs/constant/ChConstant.java

@ -0,0 +1,27 @@
package io.xtfs.jwebfs.constant;
import io.xtfs.jwebfs.configure.EnvConfig;
/**
* 系统常量
*/
public class ChConstant {
//项目运行目录
public static final String WORK_DIR = EnvConfig.yxDbConf();
//数据库链接文件路径
public static final String DB_CONF = "/conf/conf.properties";
//分页大小
public static final int PAGESIZE=12;
//上传目录
public static final String UPLOAD_DIR = "/upload";
//密钥
public static final String SYS_SECRECT="An`)bBmyjf`.";
//加密短语
public static final String SYS_SECRECT_SHORT_WORD="9149D";
}

41
src/main/java/io/xtfs/jwebfs/mapper/WbFileMapper.java

@ -0,0 +1,41 @@
package io.xtfs.jwebfs.mapper;
import io.xtfs.jwebfs.bean.WbFile;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public interface WbFileMapper {
/**
* 新增
* @param wbFile
* @return
*/
int addRs(WbFile wbFile);
/**
* 列表
* @param map
* @param start
* @param limit
* @return
*/
List<WbFile> getWbFileByMap(@Param("map") Map map, int start, int limit);
/**
* 详情
* @param id
* @return
*/
WbFile getWbfileById(Integer id);
/**
* 统计
* @param map
* @return
*/
int countRs(@Param("gmap")Map map);
}

78
src/main/resources/mapper/webfs/WbFileMapper.xml

@ -0,0 +1,78 @@
<?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="yc.mall.api.mapper.WbFileMapper">
<sql id="column">id.filename,filehash</sql>
<sql id="tbName">wbfile</sql>
<!-- 查询详情 -->
<select id="getWbfileById" resultType="io.xtfs.jwebfs.bean.WbFile" parameterType="java.lang.Integer">
SELECT <include refid="column"/>
FROM <include refid="tbName"/>
WHERE id=#{id}
</select>
<!-- 带分页查询-->
<select id="getWbFileByMap" resultType="io.xtfs.jwebfs.bean.WbFile" parameterType="java.util.Map">
SELECT <include refid="column"/>
FROM <include refid="tbName"/>
<if test="gmap!=null and gmap!=''">
<where>
<foreach collection="gmap" index="k" item="v" separator="and">
<if test="k!='sokey'">
${k}=${v}
</if>
<if test="k=='sokey'">
${v}
</if>
</foreach>
</where>
</if>
<if test="start!=-1">
LIMIT #{start},#{limit};
</if>
</select>
<!-- 统计记录-->
<select id="countRs" resultType="java.lang.Integer" parameterType="java.util.Map">
SELECT COUNT(*)
FROM <include refid="tbName"/>
<if test="gmap!=null and gmap!=''">
<where>
<foreach collection="gmap" index="k" item="v" separator="and">
<if test="k!='sokey'">
${k}=${v}
</if>
<if test="k=='sokey'">
${v}
</if>
</foreach>
</where>
</if>
</select>
<!--添加 -->
<insert id="addRs" parameterType="io.xtfs.jwebfs.bean.WbFile" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO <include refid="tbName"/>
(filename,filehash)
VALUES
(#{filename},#{filehash})
</insert>
<!--更新-->
<update id="updateRS" parameterType="io.xtfs.jwebfs.bean.WbFile">
UPDATE <include refid="tbName"/>
<set>
<if test="filename!=null">
filename=#{filename},
</if>
<if test="filehas!=null">
filehash=#{filehash}
</if>
</set>
WHERE good_id=#{goods_id}
</update>
</mapper>

38
src/test/java/io/xtfs/jwebfs/mapper/WbFileMapperTest.java

@ -0,0 +1,38 @@
package io.xtfs.jwebfs.mapper;
import io.xtfs.jwebfs.bean.WbFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class WbFileMapperTest {
@Autowired
private WbFileMapper wbFileMapper;
@Test
public void addRs() {
WbFile wbFile = new WbFile();
wbFile.setFilehash("ss");
wbFile.setFilenmae("test");
int rt= wbFileMapper.addRs(wbFile);
System.out.println(rt);
}
@Test
public void getWbFileByMap() {
}
@Test
public void getWbfileById() {
}
@Test
public void countRs() {
}
}
Loading…
Cancel
Save