Spring Boot与Spring Data JPA、MySql环境搭建

来源:互联网 发布:阿里云平台架构 编辑:程序博客网 时间:2024/05/17 03:23

想做一个微信小程序后台,选用了spring Boot,因为有新鲜感,



pom.xml

<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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.0.RELEASE</version></parent>  <groupId>com.hufan</groupId>  <artifactId>mysql</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>mysql</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </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-data-jpa</artifactId>  </dependency>  <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  </dependency>  </dependencies>    <!-- Package as an executable JAR --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>


application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost/springspring.datasource.username=rootspring.datasource.password=# Keep the connection alive if idle for a long time (needed in production)spring.datasource.testWhileIdle = truespring.datasource.validationQuery = SELECT 1# Show or not log for each sql queryspring.jpa.show-sql = true# Hibernate ddl auto (create, create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategyspring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy# Use spring.jpa.properties.* for Hibernate native properties (the prefix is# stripped before adding them to the entity manager)# The SQL dialect makes Hibernate generate better SQL for the chosen databasespring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

package com.hufan.mysql;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.validator.constraints.NotBlank;@Entity@Table(name="todos")public class Todo {@Id@GeneratedValue(strategy = GenerationType.AUTO)private long id;@NotBlankprivate String title;private Date createdOn = new Date();private Boolean completed = false;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Date getCreateOn() {return createdOn;}public void setCreateOn(Date createOn) {this.createdOn = createOn;}public Boolean getCompleted() {return completed;}public void setCompleted(Boolean completed) {this.completed = completed;}public Todo(String title) {super();this.title = title;}public Todo() {super();}}


package com.hufan.mysql;import java.util.List;import org.springframework.data.repository.CrudRepository;import org.springframework.stereotype.Repository;@Repositorypublic interface TodoRepository extends CrudRepository<Todo, Long>{public List<Todo> findAll();public Todo findOne(Long id);public Todo save(Todo todo);public void delete(Long id);}


package com.hufan.mysql;import java.util.List;import javax.validation.Valid;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/todos")public class TodoController {@Autowiredprivate TodoRepository todoRepository;@RequestMapping(value="/get",method = RequestMethod.GET)public List<Todo>getAllTodos(){return todoRepository.findAll();}@RequestMapping(method = RequestMethod.POST)public Todo createTodo(@Valid @RequestBody Todo todo){return todoRepository.save(todo);}@RequestMapping(value="{id}", method = RequestMethod.GET)public ResponseEntity<Todo> findOne(@PathVariable("id") Long id){Todo todo = todoRepository.findOne(id);if(todo == null){return new ResponseEntity<Todo>(HttpStatus.NOT_FOUND);}return new ResponseEntity<Todo>(todo,HttpStatus.OK);}@RequestMapping(value="{id}", method = RequestMethod.PUT)public ResponseEntity<Todo> updateTodo(@Valid @RequestBody Todo todo, @PathVariable("id") Long id){Todo todoDb = todoRepository.findOne(id);if(todoDb == null){return new ResponseEntity<Todo>(HttpStatus.NOT_FOUND);}else{todoDb.setTitle(todo.getTitle());todoDb.setCompleted(todo.getCompleted());todoDb = todoRepository.save(todoDb);return new ResponseEntity<Todo>(todoDb,HttpStatus.OK);}}@RequestMapping(value="{id}", method = RequestMethod.DELETE)public void deleteTodo(@PathVariable("id") Long id) {todoRepository.delete(id);}}

package com.hufan.mysql;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class TodoApplication {public static void main(String[] args){SpringApplication.run(TodoApplication.class, args);}}
CREATE TABLE todos (  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,  title VARCHAR(50) NOT NULL,  todos VARCHAR(50) NOT NULL,   createdOn DATE NOT NULL,   completed BOOLEAN NOT NULL)


                                             
0 0
原创粉丝点击