springboot加速你的web开发

本文介绍如何使用SpringBoot快速搭建RESTful风格的Web应用,并通过Freemarker实现页面展示,同时利用Spring Data JPA进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

为了有一个直观的感受,我们来搭建基于springboot的resful风格的web项目,页面使用freemarker模板

工程结构:可以war包启动,也可以使用jar启动


pom文件的依赖:<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.springboot</groupId>
<artifactId>springboot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.20</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

配置文件只有一个application.properties:

#freemarker
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates

 #DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#DataSource
datasource.pool.initialSize=10
datasource.pool.minIdle=5
datasource.pool.maxActive=1000
datasource.pool.maxWait=60000
datasource.pool.validationQuery=SELECT 'x'
datasource.pool.testWhileIdle=true
datasource.pool.testOnBorrow=false
datasource.pool.testOnReturn=false
datasource.pool.timeBetweenEvictionRunsMillis=50000
datasource.pool.minEvictableIdleTimeMillis=50000
datasource.pool.numTestsPerEvictionRun=10
datasource.pool.poolPreparedStatements=true
 
 #JPA
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.open-in-view=true
spring.jpa.database=mysql
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.fetch_size=50
spring.jpa.properties.hibernate.jdbc.batch_size=20
spring.jpa.properties.javax.persistence.validation.mode=none
spring.jpa.properties.hibernate.connection.isolation=2

项目用main方法启动:使用war必须继承SpringBootServletInitializer

@Configuration
@ComponentScan("com.springboot")
@EnableAutoConfiguration
public class StartUp extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication app = new SpringApplication(StartUp.class);
app.setWebEnvironment(true);
app.setShowBanner(true);
Set<Object> set = new HashSet<Object>();
//set.add("classpath:applicationContext.xml");  
        app.setSources(set);  
        app.run(args); 
}
@Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(StartUp.class);  
    } 
}

springmvc的controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping(value="/login")
@ResponseBody
public String login(String userName, String passWord){
User u = userService.selectUser(userName,passWord);
if(null==u){
return "error";
}else{
return "welcome";
}
}
}

spring-data-jpa的实体类:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="da_user")
public class User {
@Column(name="user_name")
private String userName;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@Column(name="pass_word")
private String passWord;

public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}

dao层:

import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import com.springboot.entity.User;

public interface UserRepository extends Repository<User, Long>{
User findByUserNameAndPassWord(String userName, String passWord);
@Query("from User")
List<User> findAll();

}

项目启动:


springboot内置tomcat,main方法启动后就可以直接访问了:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值