启动类
继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer类,覆盖configure()方法,用于打包。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
// 用于构建war文件并进行部署
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
排除 spring-boot-starter-tomcat
1)这个其实不是必须的,就是不排除这个,打包后jar包里面多了tomcat插件相关的jar,看起来不严谨。
2)添加spring-boot-starter-tomcat依赖,scope设置为provided
3)还有说明一下,如果测试阶段用 mvn clean spring-boot:run 启动,是万万不能排除 tomcat依赖的。所以,我是在开发阶段注释掉这个依赖,在部署打包前,再让这个依赖生效。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspringboot</groupId>
<artifactId>MySpringBoot03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot 的启动器 -->
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myspringboot.App</mainClass> <!--此处为启动类的全限定类名-->
</configuration>
</plugin>
</plugins>
</build>
</project>
Controller内容
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.myspringboot.dao.UserDao;
import com.myspringboot.pojo.Users;
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserDao userDao; //继承JpaRepository
@RequestMapping("/get")
@ResponseBody
public Object get(Integer id) {
System.out.println("get success");
return userDao.findOne(id);
}
@RequestMapping("/add")
@ResponseBody
public Object add(String name,Integer age) {
Users users = new Users();
users.setAge(age);
users.setName(name);
userDao.save(users);
System.out.println("add success");
return "ok";
}
@RequestMapping("/addByJpa")
@ResponseBody
public Object addByJpa() {
Users users = new Users();
users.setAge(18);
users.setName("lisi");
userDao.save(users);
System.out.println("add success");
return "ok";
}
@RequestMapping("/addByJpa2")
@ResponseBody
public Object addByJpa(String name,Integer age) {
Users users = new Users();
users.setAge(age);
users.setName(name);
userDao.save(users);
System.out.println("add success2");
return "ok";
}
}
UserDao内容
注意:一定要继承JpaRepository
import org.springframework.data.jpa.repository.JpaRepository;
import com.myspringboot.pojo.Users;
public interface UserDao extends JpaRepository<Users, Integer> {
}
Users内容
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name ="users")
public class Users {
@Id //少了这个注释会导致启动springboot报错
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="age")
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
application.properties内容
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=123456789
打包
在项目根目录执行maven 命令: mvn clean package
很多朋友不习惯添加 clean ,我觉得很奇怪,注意,一定要加上这个。
或者右键项目名称,点击Run as > Maven build,弹出以下窗口:
在Goals输入 clean package,然后点击Run
部署到Tomcat
在 target 文件夹下找到war,或文件夹,直接拷贝到 tomcat下的webapps文件夹下即可。
注意:
我们的SpringBoot的项目访问路径为:http:ip/port/…后面的是requestMapping的值,而部署到了tomcat中访问的地址应该为:http:ip/port/项目名称/…
http://127.0.0.1:8080/MySpringBoot03-0.0.1-SNAPSHOT/users/get?id=9