SpringBoot+SpringData实现SpringMVC简单项目
来源:互联网 发布:ubuntu 12.04 jdk6 编辑:程序博客网 时间:2024/05/18 01:19
一、写在前面
最近一段时间在学习《Spring in action》这本书,在闲着无聊从后往前翻的时候发现了Spring Boot这一章,在书的最后作者有谈到很有可能在下一版中一开始就会介绍Spring Boot,这也就引发了我对于Spring Boot的兴趣。
为什么要使用Spring?因为可以减少Java开发的复杂性。
为什么要使用Spring Boot?为了简化Spring应用的搭建和开发过程。
二、具体实践
开发环境:IntelliJ IDEA 2016.2 (直接使用的较新版本)+MYSQL
项目结构:
项目使用了IDE中的基础Maven模板,因为这个Demo演示的功能比较简单,service类的功能没有用到,就不列出了。Maven配置(pom.xml):
PS:其中有一些是这个简易Demo里没有用到的,我没有删除
<?xml version="1.0" encoding="UTF-8"?><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.wufengqiyun</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>compile</scope> </dependency> </dependencies> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
- 数据库:
这里用到的就是一个很简单的名为springboot的MYSQL数据库,数据表为user,里面有id,name,password三个字段。
CREATE TABLE `springboot`.`user` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, `password` VARCHAR(45) NULL, PRIMARY KEY (`id`));
- 主函数:
主函数非常的简单,就是提供一个入口,和学习JavaSE时候非常类似,其中比较重要的是三个注解,也就是Spring的自动装配。
ps:在新版本的Spring Boot中,使用了一个新的注解@SpringBootApplication来代替这三个的搭配
package com.wufengqiyun;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.Order;import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;@Configuration@ComponentScan@EnableAutoConfigurationpublic class Start { public static void main(String[] args) { SpringApplication.run(Start.class,args); }}
- Controller:
主要功能是根据id返回相关信息
package com.wufengqiyun.controller;import com.wufengqiyun.domain.User;import com.wufengqiyun.repository.UserRepository;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.*;@RestControllerpublic class UserController { private static final Logger LOGGER= LoggerFactory.getLogger(UserController.class); private UserRepository userRepository; @Autowired public UserController(UserRepository userRepository){ this.userRepository=userRepository; } @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) @ResponseBody public User findByUserId(@PathVariable long id){ User user=userRepository.findOne(id); return user; }}
- 仓库类UserRepository:
简单的数据库CRUD操作通过继承CrudRepository即可完成
package com.wufengqiyun.repository;import com.wufengqiyun.domain.User;import org.springframework.data.repository.CrudRepository;import org.springframework.data.rest.core.annotation.RestResource;import java.util.Collection;@RestResource(exported = false)public interface UserRepository extends CrudRepository<User, Long>{ Collection<User> findAll(); User findByName(String name);}
- User实体类:
一个简单的JavaBean,注意和数据库相对应
package com.wufengqiyun.domain;import javax.persistence.*;@Entity@Table(name = "user")public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
- 配置文件application.properties:
这里的数据库相关信息,需要按照自己实际情况修改
#数据库配置信息spring.datasource.url=jdbc:mysql://localhost/springboot?useUnicode=true&characterEncoding=UTF-8&autoReconnect=truespring.datasource.username=自行添加spring.datasource.password=自行添加
运行结果:
如果运行成功,是可以看到内置的tomcat的运行成功信息的,无需另行配置,非常方便在浏览器中输入链接:http://localhost:8080/user/2 结果为:
0 0
- SpringBoot+SpringData实现SpringMVC简单项目
- springboot+mybatis+springmvc+mysql简单项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- SpringBoot + Mybatis实现SpringMVC Web项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- 构建spring+springdata +jpa+springmvc项目
- IDEA进行springmvc项目开发三(springboot搭建简单springmvc)
- springdata redis实现的简单demo
- springdata redis实现的简单demo
- SpringBoot-SpringData-JPA集成
- SpringBoot-SpringData-oneToOne
- SpringBoot-SpringData-ManyToMany
- SpringBoot-SpringData-ManyToOne
- SpringBoot-SpringData-懒加载
- Springboot+SpringData+jpa
- SpringBoot-SpringData-懒加载
- Bluedroid GKI源码分析
- 虚拟桌面现状
- JNI官方规范中文版——如何在JNI中抛出Java异常
- Protocol buffer序列化及其在微信蓝牙协议中的应用
- python生成html表格
- SpringBoot+SpringData实现SpringMVC简单项目
- NOI2003 矩阵游戏
- ZYNQ平台学习--(2)生成FSBL
- Querying CRM data with LINQ
- 深入hibernate的三种状态
- PHP中的命名空间(namespace)及其使用详解
- leetcode 20. Valid Parentheses
- c++大数模板类
- Android ImageView圆形头像 图片完全解析