<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/todo-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${db.driver}" /> <property name="jdbcUrl" value="${db.url}" /> <property name="user" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:config/schema.sql" /> <jdbc:script location="classpath:config/data.sql" /> </jdbc:initialize-database>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="hsql_pu" /> <property name="dataSource" ref="dataSource" /> </bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration(proxyBeanMethods = false) @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
package com.lidihuo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ControllerDemo { @RequestMapping("/") public String home() { return "home.jsp"; } }
package com.lidihuo.model; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="userdata") public class User { @Id private int id; private String username; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return username; } public void setUname(String username) { this.username = username; } @Override public String toString() { return "User [id=" + id + ", uname=" + username + "]"; } }
server.port=8085 spring.h2.console.enabled=true spring.datasource.plateform=h2 spring.datasource.url=jdbc:h2:mem:lidihuo
insert into userdata values(101,'Tom'); insert into userdata values(102,'Andrew'); insert into userdata values(103,'Tony'); insert into userdata values(104,'Bob'); insert into userdata values(105,'Sam');
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="addUser"> ID :<br /> <input type="text" name="t1"><br /> User name :<br /> <input type="text" name="t2"><br /> <input type="submit" value="Add"> </form> </body> </html>
SELECT * FROM USERDATA;
打开调试日志记录
使用Spring Boot执行器
logging.level.org.springframework: DEBUG
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
package com.lidihuo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoRestController { @GetMapping("/hello") public String hello() { return "Hello User, have a nice day."; } }
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-hal-browser</artifactId> </dependency>