本想整理一下springboot的优缺点的,还是拉倒吧,这东西都是仁者见仁智者见智,最低现在我打算用它并且觉得还不错,那就上路吧
框架这个东西还是得自己亲自搭建,这样知道哪里会出现问题,下次不再犯就行了
目录
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>1.5.6.RELEASE
</version>
-
<relativePath />
-
</parent>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
</dependency>
引入jar后写application.yml,
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot username: root password: root logging: file: mtgg.log level: debug mybatis: type-aliases-package: com.next mapper-locations: classpath:mapper/*.xml
server:
port:8080
这个配置还是要写的,不写会报错找不到DataSource,
启动类
@SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }注意启动类放到业务层的同级或父级目录中 这应该就能启动了,在Application页面右键启动即可,访问http://localhost:8080访问index.html,hello baby
如果要经过API,直接写一个ajax请求/first/one即可,
初步的搭建就不要复杂了,慢慢再加东西,这里就是注意要写datasource,springboot的一些注解还是需要好好理解一下
@RestController @RequestMapping("/first") public class AController { @Autowired private AliPayBean aliPayBean; @RequestMapping("/one") public String one() throws WxErrorException { System.out.println("hello baby"); System.out.println(aliPayBean.getAppId()); System.out.println(aliPayBean.getServerUrl()); System.out.println(aliPayBean.getDomain()); return "hello baby"; } @RequestMapping("/two") public String two(){ String str=String.valueOf(System.currentTimeMillis()); System.out.println(str); return str; } }
下面记录一下如何发不到tomcat服务器中
1、修改打包形式
再pom.xml中设置,<packaging>war</packaging>
2、移除嵌入式tomcat插件
在pom.xml里找到spring-boot-starter-web接点,修改为
-
<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>
-
<dependency>
-
<groupId>javax.servlet
</groupId>
-
<artifactId>javax.servlet-api
</artifactId>
-
<version>3.1.0
</version>
-
<scope>provided
</scope>
-
</dependency>
3、修改启动类,重写初始化方法
可以在原类Application中继承SpringBootServletInitializer,也可以在类的同级添加一个SpringBootStartApplication类
/** * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法 */ public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的Application启动类 return builder.sources(Application.class); } }
4、在项目根目录下,pom.xml同级,命令:mvn clean package,Build Success打包成功,在target目录下将war包放到tomcat的webapps目录下,设置好路径启动tomcat即可解压部署,最后输入http://localhost:8080/打包项目名,访问主页
我的springboot demo地址:http://download.csdn.net/download/goligory/10046865
借鉴:http://blog.csdn.net/javahighness/article/details/52515226