Springboot Link Database and curd

Source: Internet
Author: User
Tags xmlns mysql database

The MAVEN project I created

first of all we want to add database starter and MySQL database dependencies:

<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>cn.et</groupId> <artifactid>sblesson</ artifactid> <version>0.0.1-SNAPSHOT</version> <parent> <groupid>org.springframework.bo Ot</groupid> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.release&
	Lt;/version> </parent> <dependencies> <!--springboot The integration of each framework is a starter Spring-boot-starter-web loading Java EE inline tomcat--<dependency> <groupid>org.springframework.boot </groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <depen Dency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>mysql</
groupid> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> </project>

four main elements of adding a database:

Spring.datasource.url=jdbc:mysql://localhost/myfood
Spring.datasource.username=root
spring.datasource.password=123456
Spring.datasource.driver-class-name=com.mysql.jdbc.driver

The most important classes are:

Package Cn.et.dao;


Import org.springframework.data.repository.CrudRepository;


Import Cn.et.Food;


Public interface Foodrepository extends Crudrepository<food, integer> {


}


then you need to create the entity class for the database:

Package cn.et;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;

@Entity public
class Food {
	@Id
	@GeneratedValue (strategy=generationtype.auto)
	private int foodid;
	@Column
	private String foodname;
	@Column
	private int price;
	@Column
	private String imgpath;
	public int Getfoodid () {
		return foodid;
	}
	public void Setfoodid (int foodid) {
		this.foodid = Foodid;
	}
	Public String Getfoodname () {
		return foodname;
	}
	public void Setfoodname (String foodname) {
		this.foodname = foodname;
	}
	public int GetPrice () {
		return price;
	}
	public void Setprice (int. price) {
		This.price = Price;
	}
	Public String Getimgpath () {
		return imgpath;
	}
	public void Setimgpath (String imgpath) {
		this.imgpath = Imgpath;
	}
	
}

The final thing is to create the controller layer for the project:

Package cn.et;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;
Import Cn.et.dao.FoodRepository;
	@RestController @SpringBootApplication public class Sbcontroller {@Autowired jdbctemplate jdbc;
	
	@Autowired foodrepository er;
		@RequestMapping ("/hello") public Map Hello () {map map = new HashMap ();
		Map.put ("id", 1);
		
		Map.put ("name", "Zs");
	return map; } @RequestMapping ("/food/{foodid}") Public Map Getfood (@PathVariable String foodid) {list<map<string, Object&gt ;> queryforlist = jdbc.queryforlist ("SELECT * FROM food wheRe foodid= "+foodid);
	Return Queryforlist.get (0);
	}//Add @RequestMapping ("/savefood") public String Savefood () {Food food = new food ();
	Food.setfoodname ("SSS");
	Food.setimgpath ("45454");
	Food.setprice (15);
	Er.save (food);
return "1";
	}//Query @RequestMapping ("/queryfood") Public food Queryfood () {Food food = Er.findone (39);
return food;
	} public static void Main (string[] args) {Springapplication.run (sbcontroller.class, args); }
	
} 



last access to add data via access (Http://localhost:8080/savefood) if the return value is "1" then add success otherwise fail


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.