SpringBoot,自己研究了好几天,以前也是没有接触过这类的框架,不过原理吧,也就是那么些个原理,毕竟都是Spring开源下的子框架。

好了,回归正题,今天晚上研究了好久,写出来了无配置文件的javaConfig配置,Demo集成了SpringMvc + mybatis + boot  ,里面也含有hibernate的core包,用来生成数据库表结构的。

首先 ,添加如下boot官方提供的依赖包:

 1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 <dependency>
6 <groupId>org.mybatis.spring.boot</groupId>
7 <artifactId>mybatis-spring-boot-starter</artifactId>
8 <version>1.1.1</version>
9 </dependency>
10 <!-- jpa-->
11 <dependency>
12 <groupId>org.springframework.boot</groupId>
13 <artifactId>spring-boot-starter-data-jpa</artifactId>
14 <version>1.5.7.RELEASE</version>
15 </dependency>

上面的就是配置中所需要的架包,因为本demo体现的是无配置文件,所以demo里不会有什么配置文件(yaml除外)

我使用的数据源是阿里的Druid  另外还有分页的架包以及mybatis整合spring的架包

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
jar包现在已经分配完毕(注意jar包的版本号,个人发现很多问题都出在了jar包冲突或者版本不一致的身上了)
本demo 演示的编辑工具是 IDEA 因为我创建的是springboot项目,所以编辑器会自动给我生成了application.yml
下来我贴上这个内容
server:
port: 8081
context-path: / #开启spring AOP配置
aop:
auto: true
proxy-target-class: true
# jpa 配置
# jpa:
# show-sql: true
## open-in-view: true
# hibernate:
# ddl-auto: update
#mybatis:
# type-aliases-package: com.example.model
# mapper-locations: classpath:mapper/*.xml
# configuration:
# use-new-id-generator-mappings:

注:数据源的配置也可以放在 yml文件里,也很方便,因为本demo演示的是在javaConfig里面配置,所以就没有配置在yml里面。

下来创建配置文件(jdbc.properties以及 javaConfig )

jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true
jdbc.user=root
jdbc.password=
#jdbc.driver=com.mysql.jdbc.Driver
jdbc.initialSize=5
jdbc.minIdle=1
jdbc.maxActive=10
jdbc.filters=stat

这和普通大多数项目的一样,可以粘过来!

现在,我来开始配置 数据源 以及 业务层配置

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; /**
* 配置业务层
*/
@Configuration//配置的注解
@EnableTransactionManagement//事务管理的注解
@PropertySource("classpath:jdbc.properties")//属性扫描注解
public class SqlSessionFactoryConfig implements TransactionManagementConfigurer{
  //创建全局数据源
protected DataSource dataSource;
//mybatis配置
private static String Mybatis_Config = "mybatis-config.xml";
//类的别名
private static String ALIASMODEL = "com.example.model";
  //mapper 包的位置
private static String MAPPERPATH = "com.example.mapper";
  //mapper.xml 编写sql的文件
private static String MAPPERXMLPATH = "mapper/*.xml";
  
private static String SQLSESSIONBEAN = "sqlSessionFactoryBean"; /**
* 配置数据源
    先去jdbc.properties里扫描 然后进行配置
* @return
*/
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(
@Value("${jdbc.url}")String url,
@Value("${jdbc.user}")String username,
@Value("${jdbc.password}")String password) {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
try {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
this.dataSource = dataSource;
sqlSessionFactory.setDataSource(dataSource);
//设置mybatis
sqlSessionFactory.setConfigLocation(new ClassPathResource(Mybatis_Config));
//设置模型类别名
sqlSessionFactory.setTypeAliasesPackage(ALIASMODEL);
//配置分页插件
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("dialect","mysql");
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
pageHelper.setProperties(properties);
sqlSessionFactory.setPlugins(new Interceptor[]{pageHelper});
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//*.mapper.xml的地址(根据你的项目自行修改)
sqlSessionFactory.setMapperLocations(resolver.getResources(MAPPERXMLPATH));
//设置mapper sql文件的扫描路径
return sqlSessionFactory.getObject(); } catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 配置数据模板
* @param sqlSessionFactory
* @return
*/
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
} /**
* Spring整合Mapper
* @return
*/
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName(SQLSESSIONBEAN);
//*.mapper(*.dao)的包名(根据你的项目自行修改)
mapperScannerConfigurer.setBasePackage(MAPPERPATH);
//配置通用Mapper,详情请查阅官方文档
Properties properties = new Properties();
//tk.mybatis.mapper.common.Mapper
properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
properties.setProperty("notEmpty", "false");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
//使用的数据库类型名称(MySQL,Oracle,Postgresql...)
properties.setProperty("IDENTITY", "MySQL");//
mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer;
}
   
    /**
* 事务的控制管理 将数据源注入事务内
* @return
*/
@Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource); } }

好了,业务层的配置完了,简单吧!!!

嘿嘿嘿,其实这个和spring的applicationContext.xml配置完全是一样的,我们都知道 Spring最大的特性就是  IOC和AOP,因此只需要将以前配置的bean的class拿到,然后用bean的特定实现类进行set其配置就哦了!!!

web Controller层也是一样的。

无非是照猫画虎,照鸡画猴

下来,我直接贴代码吧!

import java.util.ArrayList;
import java.util.List; @Configuration
public class WebMvcConfig {
//配置视图解析器
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".ftl");
resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
return resolver;
}
//上传文件配置
@Bean(name = "multipartResolver")
public CommonsFileUploadSupport commonsFileUploadSupport(){
CommonsFileUploadSupport resolver = new CommonsMultipartResolver();
resolver.setMaxInMemorySize(40960);
resolver.setMaxUploadSize(10485760000L);
return resolver;
}
//异常解析拦截器 过滤
@Bean
public HandlerInterceptor interceptor(){
HandlerInterceptor interceptor = new ExceptionInterceptor();
List<MappedInterceptor> interceptors = new ArrayList<>();
MappedInterceptor mappedInterceptor = new MappedInterceptor(new String []{
"/js/**","/image/**","/uplaod/**","/**/*.jpeg","/**/*.jpg"
,"/**/*.gif","/**/*.svg","/**/*.html"
},interceptor);
interceptors.add(mappedInterceptor);
return interceptor;
}
}

有没有和SpringMVC的配置文件一样?

可以和以前的SpringMVC的配置来对比一下,下面我贴上SpringMVC的配置XML文件   大家来对比看看哈~~

<mvc:interceptors>
<mvc:interceptor>
<!-- 对所有的请求拦截使用/** ,对某个模块下的请求拦截使用:/myPath/* -->
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/**/*.html" />
<mvc:exclude-mapping path="/**/*.css" />
<mvc:exclude-mapping path="/**/*.js" />
<mvc:exclude-mapping path="/**/*.jpeg" />
<mvc:exclude-mapping path="/**/*.gif" />
<mvc:exclude-mapping path="/**/*.png" />
<mvc:exclude-mapping path="/**/*.eot" />
<mvc:exclude-mapping path="/**/*.otf" />
<mvc:exclude-mapping path="/**/*.svg" />
<mvc:exclude-mapping path="/**/*.ttf" />
<mvc:exclude-mapping path="/**/*.woff" />
<mvc:exclude-mapping path="/**/*.woff2" />
<ref bean="exceptionInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<bean name="exceptionInterceptor" class="yf.wuchw.web.ExceptionInterceptor" />
<mvc:default-servlet-handler /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
<!--配置jsp显示ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>

有没有一种照鸡画猴的感觉,嘿嘿,至于其它的注解配置,都和Spring一样的。Controller 层 加上@Controller  Service 加上@Service

注意:Mapper 层要加上 @Mapper

注:转载请注明出处;原文链接:http://www.cnblogs.com/ChoviWu/p/8180022.html

最新文章

  1. ORACLE分区表梳理系列(二)- 分区表日常维护及注意事项(红字需要留意)
  2. Markdown 完全指南
  3. PS色调均化滤镜的快捷实现(C#源代码)。
  4. swift webView 提出这样的要求你能忍吗?
  5. Oracle 给已创建的表增加自增长列
  6. CentOS Git的还原和操作
  7. Python 列表如何获得一个指定元素所在的下标
  8. NOJ1018-深度遍历二叉树
  9. 1 ubuntu下装setuptools
  10. ORACLE DATABASE 10G FALSHBACK 知识整理
  11. iPhone 设置铃声
  12. 清北学堂 NOIP2017模拟赛 越赛越心塞
  13. Xcode 中的断言
  14. python闭包和装饰器
  15. input 标签,不可更改
  16. 【腾讯Bugly干货分享】经典随机Crash之二:Android消息机制
  17. C#获取MySql 数据常用的代码
  18. CentOS的Gearman安装
  19. UVM/OVM中的factory【zz】
  20. LinqToHubble介绍及简单使用步骤——LinqToHubble是对HubbleDotnet的封装

热门文章

  1. PHP.29-TP框架商城应用实例-后台6-商品会员添加-价格、级别
  2. windows禁用/启用hyper-V,解决hyper-V与模拟器同时启用时造成冲突
  3. 栈和队列&amp;前缀,中缀,后缀
  4. windows 定时任务 - 定时关机
  5. PJSIP-PJLIB-Socket
  6. 《Cracking the Coding Interview》——第2章:链表——题目2
  7. python2.7写入文件时指定编码为utf-8
  8. 孤荷凌寒自学python第五十三天使用python写入和修改Firebase数据库中记录
  9. android配置开发环境
  10. sqlalchemy presto 时间比较