Sprigmvc项目转为springboot:
是否有老掉牙的springmvc项目,想转成springboot项目,看这个文章就对了。
说明
- 如果你的项目连maven项目都不是,请自行转为maven项目,在按照本教程进行。
- 本教程适用于spring+springmvc+mybatis+shiro的maven项目。
1.修改pom文件依赖
删除之前的spring依赖,添加springboot依赖
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!-- 这个是剔除掉自带的 tomcat部署的--><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><!-- tomcat容器部署 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><!--<scope>compile</scope>--></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!-- 支持 @ConfigurationProperties 注解 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency></dependencies>添加springboot构建插件
12345678910111213141516171819202122<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>1.5.9.RELEASE</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins>
2.添加application启动文件
注意,如果Application在controller,service,dao的上一层包里,无需配置@ComponentScan
,
否则,需要指明要扫描的包。
3.添加springboot配置文件
- 在resources下面添加application.properties文件
- 添加基本配置1234567891011121314151617#默认前缀server.contextPath=/# 指定环境spring.profiles.active=local# jsp配置spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp#log配置文件logging.config=classpath:logback-cms.xml#log路径logging.path=/Users/mac/work-tommy/cms-springboot/logs/#数据源spring.datasource.name=adminDataSourcespring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/mycms?useUnicode=true&autoReconnect=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456
4.使用@Configuration注入配置
注入mybatis配置,分页插件请自主选择
12345678910111213141516171819202122232425262728293031323334353637383940414243444546"com.kuwo.dao",sqlSessionTemplateRef = "adminSqlSessionTemplate")(basePackages =public class AdminDataSourceConfig {"adminDataSource")(name ="spring.datasource")(prefix =public DataSource adminDataSource() {return DataSourceBuilder.create().build();}"adminSqlSessionFactory")(name =public SqlSessionFactory adminSqlSessionFactory(@Qualifier("adminDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);//分页插件// PageHelper pageHelper = new PageHelper();PagePlugin pagePlugin = new PagePlugin();// Properties props = new Properties();// props.setProperty("reasonable", "true");// props.setProperty("supportMethodsArguments", "true");// props.setProperty("returnPageInfo", "check");// props.setProperty("params", "count=countSql");// pageHelper.setProperties(props);//添加插件bean.setPlugins(new Interceptor[]{pagePlugin});// 添加mybatis配置文件bean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis/mybatis-config.xml"));// 添加mybatis映射文件bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/system/*.xml"));return bean.getObject();}"adminTransactionManager")(name =public DataSourceTransactionManager adminTransactionManager(@Qualifier("adminDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}"adminSqlSessionTemplate")(name =public SqlSessionTemplate adminSqlSessionTemplate(@Qualifier("adminSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}添加Interceptor配置,注意addInterceptor的顺序,不要搞乱了
1234567public class InterceptorConfiguration extends WebMvcConfigurerAdapter{public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginHandlerInterceptor());}}添加shiro配置文件
- 注意:本来使用redis做session缓存,但是和shiro集成发现一个问题,user对象存储以后,从shiro中获取后,无法进行类型转换,所以暂时放弃了redis做session缓存。
|
|
总结
搞了一天时间把项目转成springboot,查阅各种资料,希望这篇文章能够为你带来帮助。