标签:cto stc mave dep nap 运行 vax hiberna var
StringBoot是一个几乎不需要配置的Java框架,能够帮助开发者专注于业务逻辑,而不需要关心框架的复杂配置.
package com.how2java.springboot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
重写运行,访问/hello就会看到输出的文字了.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>
+application.propertiles下增加
spring.thymeleaf.cache=true
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java //监听目录
之后修改控制类中内容后并ctrl+s保存后,程序会自动重启(需要等待一定时间,约5-6s)
在核心配置文件application.propertiles所在目录下新建两个配置文件
application-dev.properties
application-pro.properties
分别代表开发配置和生产配置.通过修改application.propertiles下spring.profiles.active属性=pro或者dev来切换配置.
核心配置文件中的配置在两套配置中都是通用的.
对最终生成的jar文件也可以通过java -jar target/springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro命令使用对应配置
Sun制定了java持久化规范,Hibernate执行了该规范.Springboot内部集成了Hibernate,让用户简单的调用.
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
package com.example.springboot.pojo;
import javax.persistence.*;
/**
* @author 李文浩的魔法书
* @date 2019/8/15 21:43
*/
@Entity
@Table(name = "category_")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.springboot.dao;
import com.example.springboot.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author 李文浩的魔法书
* @date 2019/8/15 21:45
*/
public interface CategoryDAO extends JpaRepository<Category,Integer> {
}
package com.example.springboot.web;
import com.example.springboot.dao.CategoryDAO;
import com.example.springboot.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author 李文浩的魔法书
* @date 2019/8/15 21:47
*/
@Controller
public class CategoryController {
@Autowired
CategoryDAO categoryDAO;
@RequestMapping("/listCategory")
public String listCategory(Model m) throws Exception {
List<Category> cs=categoryDAO.findAll();
m.addAttribute("cs", cs);
return "listCategory";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
参考资料:
how2j
标签:cto stc mave dep nap 运行 vax hiberna var
原文地址:https://www.cnblogs.com/bestefforts/p/11360906.html