专栏名称: Guo_1_9
目录
相关文章推荐
51好读  ›  专栏  ›  Guo_1_9

基于SpringBoot的后台管理系统(数据源配置、日志记录配置及实现(重点))(三)

Guo_1_9  · 掘金  ·  · 2018-03-05 02:54

正文

请到「今天看啥」查看全文


@Configuration public class WebConfig { /** * druidServlet注册 */ @Bean public ServletRegistrationBean druidServletRegistration () { ServletRegistrationBean registration = new ServletRegistrationBean( new StatViewServlet()); registration.addUrlMappings( "/druid/*" ); return registration; } /** * druid监控 配置URI拦截策略 */ @Bean public FilterRegistrationBean druidStatFilter () { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( new WebStatFilter()); //添加过滤规则. filterRegistrationBean.addUrlPatterns( "/*" ); //添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter( "exclusions" , "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid,/druid/*" ); //用于session监控页面的用户名显示 需要登录后主动将username注入到session里 filterRegistrationBean.addInitParameter( "principalSessionName" , "username" ); return filterRegistrationBean; } /** * druid数据库连接池监控 */ @Bean public DruidStatInterceptor druidStatInterceptor () { return new DruidStatInterceptor(); } /** * druid数据库连接池监控 */ @Bean public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator () { BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator(); beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class); beanTypeAutoProxyCreator.setInterceptorNames( "druidStatInterceptor" ); return beanTypeAutoProxyCreator; } /** * druid 为druidStatPointcut添加拦截 * @return */ @Bean public Advisor druidStatAdvisor () { return new DefaultPointcutAdvisor(druidStatPointcut(), druidStatInterceptor()); } }

3、接下来我们在看看数据源的配置,先摘抄点我之前的笔记。配置H2数据库和JDBC的。

H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主流的数据库,因此采用H2作为开发期的数据库非常方便。(数据存储在内存中)。

还需要注意的是 DataSource 数据源主要有两种方式实现:

  • 1、直接数据库连接,因为每次都要进行三次握手(远程),所有性能较差。
  • 2、就是采用池化技术,比如上面说的阿里的druid(号称性能最强大,安全措施,还可以监控),之前最常用的是C3PO,DBCP,需要的时候直接从池子中拿,用完直接还回去。DataSource实现原理是对连接进行缓存,从而提高效率,资源的重复利用。
@Configuration
public class DataConfig {

  @Bean
  public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .addScript("schema.sql")
            .addScript("my-test-data.sql")
            .build();
  }
-----------------------------------------------------------------------------
  @Bean
  public JdbcOperations jdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }

}

4、需要补充一点的是:老外很多都在用底层的JDBC技术,因为原生,效率高。 jdbcTemplate 是Spring对JDBC进一步封装。命名参数的使用。这种思想理解了吗?

其实还有一种更绝绝的那就是Spring Date。只要继承了 Repository 接口,你就拥有了18个方法,不满足你的话,还可以自己定义,还有一个就是 JpaRepository 建议了解下。

private static final String SELECT_SPITTLE = "select sp.id, s.id as spitterId, s.username, s.password, s.fullname, s.email, s.updateByEmail, sp.message, sp.postedTime from Spittle sp, Spitter s where sp.spitter = s.id";
private static final String SELECT_SPITTLE_BY_ID = SELECT_SPITTLE + " and sp.id=?";
private static final String SELECT_SPITTLES_BY_SPITTER_ID = SELECT_SPITTLE + " and s.id=? order by sp.postedTime desc";
private static final String SELECT_RECENT_SPITTLES = SELECT_SPITTLE + " order by sp.postedTime desc limit ?";

public List<Spittle> findBySpitterId(long spitterId) {
  return jdbcTemplate.query(SELECT_SPITTLES_BY_SPITTER_ID, new SpittleRowMapper(), spitterId);
}
public List<Spittle> findBySpitterId(long spitterId) {
  return jdbcTemplate.query(SELECT_SPITTLES_BY_SPITTER_ID, new SpittleRowMapper(), spitterId);
}

5、接下来我们就是配置数据源了,

本来想录个Gif,但我软件出BUG了,有什么好推荐的么?为了不占地方,只放一张。关于日志的,自行脑补。好想给大家分享我的书签,太多有用的了。

  • @Component spring初始化的时候,spring会把所有添加@Component注解的类作为使用自动扫描注入配置路径下的备选对象,同时在初始化spring的@Autowired
  • @Controller注解是一个特殊的Component,它允许了实现类可以通过扫描类配置路径的方式完成自动注入,通常@Controller是结合@RequestMapping注解一起使用的。
  • @ConfigurationProperties 注解用于外部化(externalized)配置,提供 prefix 和 locations 两个属性指定属性文件的来源和属性的前缀
/**
 * <p>数据库数据源配置</p>
 * <p>说明:这个类中包含了许多默认配置,若这些配置符合您的情况,您可以不用管,若不符合,建议不要修改本类,建议直接在"application.yml"中配置即可</p>
 */
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidProperties {

    private String url = "jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";

    private String username = "root"






请到「今天看啥」查看全文