我可不允许还有人不会配置SpringBoot项目吧,这里可以看到最完整的项目配置。

我可不允许还有人不会配置SpringBoot项目吧,这里可以看到最完整的项目配置。,第1张

我可不允许还有人不会配置SpringBoot项目吧,这里可以看到最完整的项目配置。 1、首先写pom.xml进行引入依赖
       
            org.springframework.boot
            spring-boot-starter-data-redis-reactive
        
        
            org.apache.commons
            commons-pool2
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
                
                    org.slf4j
                    slf4j-log4j12
                
            
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        


        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        
        
        
            com.baomidou
            mybatis-plus-generator
            3.4.1
        
        
        
            org.apache.velocity
            velocity-engine-core
            2.2
        

        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.4.0
        

        
        
            com.alibaba
            fastjson
            1.2.68
        

        
        
            org.apache.commons
            commons-lang3
        
2、将打包的build标签写入
    
        
            
                src/main/resources
                
                    **
public class MyQuickCode {
    public static void main(String[] args) {
        //1、构造生成器
        AutoGenerator ag = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("fmdxz");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(true); //重新生成时文件是否覆盖
        gc.setServiceName("%sService"); //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
//        gc.setSwagger2(false);//开启Swagger2模式
        ag.setGlobalConfig(gc);
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/c4?useUnicode=true&characterEncoding=UTF-8" +
                "&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull" +
                "&allowMultiQueries=true");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("DXZDXZ4811");
        dsc.setDbType(DbType.MYSQL);
        ag.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(null); //模块名
        pc.setParent("com.dxz");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        ag.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("comments","url");//对那一张表生成代码
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
//        strategy.setTablePrefix("t_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式 *** 作

        strategy.setRestControllerStyle(true); //restful api风格控制器
//        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        ag.setStrategy(strategy);

        // 6、执行
        ag.execute();
    }
}
5、将mybatis-plus的配置填写完成

配置

@Configuration
@MapperScan(value = "com.dxz.mapper")
public class MybatisPlusConfig {
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
6、将redis的配置写好

redis配置

@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.lettuce.pool.max-active}")
    private Integer maxActive;

    @Value("${spring.redis.lettuce.pool.max-idle}")
    private Integer maxIdle;

    @Value("${spring.redis.lettuce.pool.min-idle}")
    private Integer minIdle;

    @Value("${spring.redis.lettuce.pool.max-wait}")
    private Long maxWait;

    
    @Bean
    LettuceConnectionFactory redisConnectionFactory(){
        //连接池的配置
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();//新建一个连接池配置
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        //根据自己写的连接池的配置进行新建一个lettuce连接池配置
        LettucePoolingClientConfiguration lettucePoolingClientConfiguration =
                LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();
        //新建一个redis配置
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(host);
        redisConfig.setPort(port);
        //配置redis连接密码,若没有密码则可以直接注释
        //redisConfig.setPassword(RedisPassword.of(password.toCharArray()));
        //返回一个新的lettuce连接工厂
        return new LettuceConnectionFactory(redisConfig,lettucePoolingClientConfiguration);
    }

    @Bean
    public RedisTemplate myRedisTemplate(LettuceConnectionFactory redisConnectionFactory){
        //新建一个template模板
        RedisTemplate template = new RedisTemplate();
        //将上面创建好的lettuce工厂导入
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());// key序列化
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());// value序列化
        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());// Hash key序列化
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());// Hash value序列化
        return template;
    }

}
7、含有日期和结果的工具类

日期工具类

@Component
public class DateUtil {
    //首先创建一个时间转换工具类
    private static DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //创建一个日志工具
    protected static Logger logger = LoggerFactory.getLogger(DateUtil.class);//参数为当前类

    
    public static String parse(final Date date) {
        if (date == null) {
            return null;
        }
        return sdf.format(date);
    }

    
    public static String now() {
        return sdf.format(new Date());
    }

    
    public static Date format(final String date) {
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

结果工具类
public abstract class ResultUtil {
    private static JSONObject results = null;

    public static JSONObject success() {
        results = new JSONObject();
        results.put("code", Constant.SUCCESS_CODE);
        results.put("data", new JSONObject());
        results.put("msg", "");
        return results;
    }

    public static JSONObject success(final int code, final JSONObject data) {
        results = new JSONObject();
        results.put("code", code);
        results.put("data", data);
        results.put("msg", "");
        return results;
    }

    public static JSONObject content(final JSONObject data) {
        results = new JSONObject();
        results.put("code", Constant.SUCCESS_CODE);
        results.put("data", data);
        results.put("msg", "");
        return results;
    }

    public static JSONObject failure(final int code, final String msg) {
        results = new JSONObject();
        results.put("code", code);
        results.put("data", new JSONObject());
        results.put("msg", msg);
        return results;
    }
}
8、关于Redis的接口类和服务类

接口类

@Component
public class RedisMapper {

    @Autowired
    @Qualifier("myRedisTemplate")
    private RedisTemplate redisTemplate;

    public void set(String key,Object value){
        try{
            ValueOperations stringObjectValueOperations = redisTemplate.opsForValue();
            stringObjectValueOperations.set(key,value);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public void set(String key, Object value, long expired){
        try{
            ValueOperations stringObjectValueOperations = redisTemplate.opsForValue();
            stringObjectValueOperations.set(key,value,expired, TimeUnit.SECONDS);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public Object get(String key){
        try{
            ValueOperations stringObjectValueOperations = redisTemplate.opsForValue();
            return stringObjectValueOperations.get(key);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
服务接口类
public interface RedisService {

    
    void set(String key,Object value);

    
    void set(String key,Object value,long expired);

    
     T get(String key);
}
服务实现类
@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    RedisMapper redisMapper;

    @Override
    public void set(String key, Object value) {
        redisMapper.set(key,value);
    }

    @Override
    public void set(String key, Object value, long expired) {
        redisMapper.set(key,value,expired);
    }

    @Override
    public  T get(String key) {
        Object o = redisMapper.get(key);
        return (T) o;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5716291.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存