小白零xml搭建springmvc+Velocity+mybatis之笔记
来源:互联网 发布:淘宝层级对流量的影响 编辑:程序博客网 时间:2024/06/07 13:55
新的一年要开始了,我来总结一下这一年用到的技术,不喜勿喷,本人小白一个凉各位大神海涵。
在2016年刚开始的时候我还只是一个只会用struts2+spring+springjdbc的一个初入茅庐的一个小白不过在这一年当中接触了不少新鲜技术主要包括springmvc,mybatis,zookeeper,elasticsearch,drool,dubbox,reasteasy,freemarker,velocity,shiro,nginx, hibernate,activiti工作流,redis,maven,seajs,git,使用linux命令,学习简单shell命令,以及一些前台组件,在这一年里虽然使用了这些技术让我的眼界大开,不过在这一年的成长当中我认识到了英语的重要性有些文档你不懂英文就是在看天书(我觉得我应该学习一下英语),不过还是非常感谢我们公司带我飞的大神,但是掌握的还是都不太熟练,所以我要在新的一年里继续努力在此写个博客给奋斗的自己画个句号,还有就是多加锻炼自己嘴皮子,实在不知道写什么我就写一下最搭建的框架步骤和遇到的问题(仅供参考不喜勿喷!!!!!)
基于Servlet3.0规范和SpringMVC4注解式配置方式,实现零xml配置。
Servlet规范已出到3.1 附参考地址http://jinnianshilongnian.iteye.com/blog/1912455
maven配置 我就免了 我用的是springboot搭建的,这是我项目的结构目录 因为本人取名太恶心 我就吧名字涂鸦了一下
spring 提供了WebApplicationInitializer接口和AbstractContextLoaderInitializer,AbstractDispatcherServletInitializer,AbstractAnnotationConfigDispatcherServletInitializer这三个抽象类来完成web.xml配置
public abstract class WebXMLInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{ DataSourceConfig.class }; } /** * 配置dispatcherServlet * @return */ @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{ WebMvcConfig.class }; } /** * 标识哪些url要经过这个DispatcherServlet处理 * @return */ @Override protected String[] getServletMappings() { return new String[] {"/"}; } /** * 用于处理项目中的乱码问题 * @return */ @Override protected Filter[] getServletFilters() { CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); //设置编码格式 characterEncodingFilter.setEncoding("UTF-8"); //是否允许设置的encoding 覆盖request和response中已经存在的encodings。 characterEncodingFilter.setForceEncoding(true); return new Filter[] {characterEncodingFilter}; }}
然后是springmvc 的配置代码
//说明类为IoC容器@Configuration/** * useDefaultFilters 是否自动扫描有@Component、@Repository、@Service和@Controller的类 * includeFilters 指定自动扫描的注解类 * basePackages 制定扫描位置 */@ComponentScan(basePackages={ "com" }, useDefaultFilters = false, includeFilters = { @ComponentScan.Filter(value=Controller.class) })//引入配置文件@PropertySource(value = { "classpath:application.properties" })public class WebMvcConfigForVelocity extends WebMvcConfig{ private String suffix; private String prefix; public String getSuffix() { return suffix; } public String getPrefix() { return prefix; } @Autowired protected Environment environment ; /** * 注册一个内部资源视图解析器式 * @param registry * @return */ @Override public UrlBasedViewResolverRegistration initViewResolve(ViewResolverRegistry registry) { //这里只需要在velocityConfig 里设置文件位置即可 VelocityViewResolver velocityViewResolver = new VelocityViewResolver(); UrlBasedViewResolverRegistration velocity = new UrlBasedViewResolverRegistration(velocityViewResolver); //设置编码格式 velocityViewResolver.setContentType("text/html;charset=UTF-8"); //设置文件后缀 velocityViewResolver.setSuffix(getSuffix()); //设置缓存 velocityViewResolver.setCache(false); //设置文件位置 velocityViewResolver.setPrefix(""); //设置解析类 velocityViewResolver.setViewClass(VelocityView.class); //注册当前视图解析 registry.viewResolver(velocityViewResolver); return velocity; } @Bean public VelocityConfigurer velocityConfig(){ VelocityConfigurer velocityConfigurer = new VelocityConfigurer() ; //velocity模板根路径 velocityConfigurer.setResourceLoaderPath(getPrefix()); //添加velocity引擎参数 Properties properties = new Properties() ; properties.put(VelocityEngine.INPUT_ENCODING, "UTF-8") ; properties.put(VelocityEngine.OUTPUT_ENCODING, "UTF-8") ; //设置Velocity引擎参数 velocityConfigurer.setVelocityProperties(properties) ; return velocityConfigurer; }} @Override protected void configureViewResolvers(ViewResolverRegistry registry) { initViewResolve(registry); } /** * 静态资源处理 * 当DispatcherServlet接收到了他匹配的请求,但是找不到相应的Controller,就会把这个请求返回给默认的处理(比如交给tomcat处理) */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { super.configureDefaultServletHandling(configurer); configurer.enable(); } @Resource public void initEnvironment(Environment environment) { if(StringUtils.isNotBlank(environment.getProperty("web.view.suffix"))){ suffix = environment.getProperty("web.view.suffix"); } if(StringUtils.isNotBlank(environment.getProperty("web.view.prefix"))){ prefix = environment.getProperty("web.view.prefix"); } }
我来说一下我在配置velocity时候遇到的问题
1.配置视图路径的时候 只需要在velocityConfig 里设置文件位置即可
2.就是返回vm乱码问题,我刚开始使用的是registry.velocity() 然后没有地方可以设置编码格式 不过可以使用反射来设置而我并没使用反射而我用的是上面代码解决方案 我也不知道好不好。
mybaits 配置
public class DruidDataSourceConfig extends DataSourceConfig{ @Override public DataSource initDataSource(Environment environment) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(environment.getProperty("jdbc.url")); dataSource.setUsername(environment.getProperty("jdbc.username")); dataSource.setPassword(environment.getProperty("jdbc.password")); dataSource.setDriverClassName(environment.getProperty("jdbc.driver")); dataSource.setValidationQuery(environment.getProperty("jdbc.validation.query")); dataSource.setMinIdle(environment.getProperty("jdbc.pool.maxIdle", Integer.class)); dataSource.setMaxActive(environment.getProperty("jdbc.pool.maxActive", Integer.class)); dataSource.setInitialSize(1); dataSource.setMaxWait(60000); dataSource.setQueryTimeout(1000); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setTestWhileIdle(true); dataSource.setMinEvictableIdleTimeMillis(300000); dataSource.setTimeBetweenEvictionRunsMillis(60000); return dataSource; } @Bean public DataSourceTransactionManager txManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver() ; SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml")); Properties sqlSessionFactoryProperties = new Properties(); sqlSessionFactoryProperties.put("roles", "sysrole"); sessionFactory.setConfigurationProperties(sqlSessionFactoryProperties); PageInterceptor helper = new PageInterceptor(); Properties properties = new Properties(); properties.put("dialect", new MySqlDialect()); properties.put("reasonable", true); helper.setProperties(properties); sessionFactory.setPlugins(new Interceptor[] {helper}); sessionFactory.setTypeAliasesPackage("com.oa"); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } @Bean public MapperScannerConfigurer mapperScannerConfigurer(Environment environment){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer() ; //由配置文件中获取mybatis映射DAO的路径 mapperScannerConfigurer.setBasePackage(environment.getRequiredProperty("mybatis.mapper.package") ); //mybatis的接口必须集成mybatis接口的标识接口 mapperScannerConfigurer.setMarkerInterface(Mapper.class); return mapperScannerConfigurer ; }}
public abstract class DataSourceConfig { @Bean(destroyMethod = "close") public DataSource dataSource(Environment environment) { return initDataSource(environment); } public abstract DataSource initDataSource (Environment environment);}
在编写数据库配置的时候我也是一头雾水 遇到很多问题 比如倒错包 没设置dialect ,等等 不过最后还是弄好了。
这样就差不多完成了 不过我还没有完全弄完 比如事务我没测试,junit,也没有配置 代码结构也还没来的及整理 ,等我配置完全之后我在写一篇更好的。
最后希望各位在新的一年里 工资翻番~~~ 也祝我能美梦成真
- 小白零xml搭建springmvc+Velocity+mybatis之笔记
- Spring+SpringMVC+Mybatis+velocity 搭建
- springmvc+mybatis+velocity配置
- maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
- springMvc,Mybatis,velocity 的整合
- 一次笔记--springMVC+Velocity
- 搭建springmvc+ibatis+velocity工程
- SpringMVC+mybatis学习一之环境搭建
- SpringMVC+FreeMarker+MyBatis框架搭建之国际化
- springMVC+spring+MyBatis实战之搭建平台
- 六、初学SpringMVC+Mybatis之SpringMVC基于XML的配置
- SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分
- SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分
- 整理springmvc+mybatis+velocity的整合<一>
- Spring+SpringMVC+Mybatis+Velocity框架集成
- maven+springMVC+mysql+mybatis+velocity+事务整合
- maven+springmvc+spring+mybatis+velocity整合
- 使用Maven创建Springmvc+Mybatis+Velocity项目
- JDBC
- 【Android图像处理】图像处理之-R&B
- Hibernate 的hql查询简介
- 快速排序
- Java使用Double精度问题
- 小白零xml搭建springmvc+Velocity+mybatis之笔记
- 如何查看系统日志
- 写在2017年的前一个小时内
- 如何使用vsprintf, vsnprintf等函数
- 如何解决log4j打印日志出现中文乱码
- Application Data拒绝访问打开方法?
- Autofs 自动挂载服务
- Error:Could not find com.android.support:appcompat-v7 v4等等
- c++对象成员