java|springboot学习随笔

1.使用配置
eclipse、jdk1.8、maven3.5.4、tomcat8、MySQL
2.新建maven web项目,容易出现新建不成功,缺少maven-archetype-webapp-1.0-alpha-4.jar之类的,百度搜索该jar包,保存之后,cmd管理员权限进入到该jar包保存路径下,执行以下命令(注:后边jar包名称为下载名称),如果该命令执行失败,检查maven、jdk配置是否完成,jdk和maven版本是否匹配,之后执行命令mvn clean; 最后再执行以下命令,再去eclipse新建项目即可

mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-webapp -Dversion=1.0 -Dpackaging=jar -Dfile=maven-archetype-webapp-1.0-alpha-4.jar

3.新建完毕之后应不会报错
修改pom文件,也可一个一个添加,在这我的pom文件做参考
4.0.0 cn.miliyf yiyang war 0.0.1-SNAPSHOT Maven Webapp Archetype http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE 1.2.0 5.1.39 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis-spring-boot} org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-tomcat provided org.springframework.boot spring-boot-legacy 1.1.0.RELEASE mysql mysql-connector-java ${mysql-connector} 【java|springboot学习随笔】org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-war-plugin 2.1.1 utf8 trueyangcom.miliyf.YiyangApplication true true

新建YiyangApplication类该类一定要在controller层级之上,不然springboot会扫描不到
//Spring Boot 应用的标识 @SpringBootApplication @ComponentScan("com.miliyf") //@MapperScan("com.miliyf.fan.dao") @EnableScheduling public class YiyangApplication { protected final static Logger logger = LoggerFactory.getLogger(YiyangApplication.class); public static void main(String[] args) { System.out.println("--------start--------"); SpringApplication.run(YiyangApplication.class, args); logger.info("YiyangApplication is sussess!"); System.out.println("--------end--------"); }}

新建controller控制类(@RequestMapping可以不写,运行main方法也可以运行访问http://localhost:8080即可;写的话类名,方法名之上都要写,不然会访问不到)--至此可以main方法运行访问
@Controller @RequestMapping("/yiyang") public class SampleController { @Autowired private userService userService; @RequestMapping("/yang") public String sayHello() { System.out.println("------------------123"); return "index"; } }

4.部署到Tomcat上
新建ServletInitializer类extends SpringBootServletInitializer类,与YiyangApplication类同级别
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Configuration; @Configuration public class ServletInitializer extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // TODO Auto-generated method stub System.out.println("======================start=========================="); return builder.sources(YiyangApplication.class); }}

5,使用eclipse打成war包,将war包复制到tomcat的webapp下,启动bin目录下的startup.bat--至此访问http://localhost:8080/yiyang/yang
6.记得在resources下新建文件templates/index.html; application.properties
该文件内容作参考
## shujuku spring.datasource.url=jdbc:mysql://localhost:3306/yiyang spring.datasource.username=yang spring.datasource.password=yang spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= truespring: thymeleaf: encoding: UTF-8 mode: HTML5 prefix: classpath:/templates/ suffix: .html check-template: true cache: falsemybatis.typeAliasesPackage=com.miliyf.fan.entity mybatis.mapperLocations=classpath:/mybatis/mapper/*.xml# spring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jspapplication.hello=Hello GODlogging.path=/user/local/log logging.level.com.favorites=DEBUG logging.level.org.springframework.web=INFO logging.level.org.hibernate=ERROR logging.level.com.miliyf=debug

每个人有每个人遇到的问题,仅适合本人,共享作为参考

    推荐阅读