spring boot 习惯大于配置 比springMVC简介很多,开发过成功免去了烦人的XML配置。项目采用的导出jar包的方式配置(jar依赖内置tomcat),maven管理依赖,pom配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring boot 配置文件:application.properties(放置在项目src文件下即可),前2个配置就是允许无密码访问shutdown,关闭由main启动jar包。
#\u542F\u7528shutdown
endpoints.shutdown.enabled=true
#\u7981\u7528\u5BC6\u7801\u9A8C\u8BC1
endpoints.shutdown.sensitive=false
server.port=80
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = ****
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.cache=false
上面2个文件就已经配置好了spring boot,下面实现具体功能。
实体类:如果没有在数据库建表和字段,实体类可自动创建:(可使用注视配置字段长度、是否为空等)
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private int height1;
private int sex;
private Date birthday;
private Date sendtime; // 日期类型,格式:yyyy-MM-dd HH:mm:ss
private BigDecimal price;
private float floatprice;
private double doubleprice;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getHeight1() {
return height1;
}
public int getSex() {
return sex;
}
public Date getBirthday() {
return birthday;
}
public Date getSendtime() {
return sendtime;
}
public BigDecimal getPrice() {
return price;
}
public float getFloatprice() {
return floatprice;
}
public double getDoubleprice() {
return doubleprice;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHeight1(int height1) {
this.height1 = height1;
}
public void setSex(int sex) {
this.sex = sex;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setSendtime(Date sendtime) {
this.sendtime = sendtime;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public void setFloatprice(float floatprice) {
this.floatprice = floatprice;
}
public void setDoubleprice(double doubleprice) {
this.doubleprice = doubleprice;
}
}
数据操作接口类
import org.springframework.data.repository.CrudRepository;
public interface UserRespositroy extends CrudRepository<User , Integer> {
User findUserById(Integer id);
}
controller控制器:
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class DemoController {
@Autowired
UserRespositroy userRespositroy;
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
@RequestMapping("/hello")
//@ResponseBody
public String hello(Map<String,Object> map){
Iterable<User> user = userRespositroy.findAll();
for(User u : user){
System.out.println(u.getDoubleprice());
}
map.put("key", "11111111111111111");
return "/hello";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoController.class, args);
}
}
模板:
在src下建立文件夹main/resources/templates,把hello.html放置在里面即可