本人创建的maven项目
首先我们要添加数据库的starter 和mysql数据库依赖:
<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.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!-- springboot每一个框架的集成都是一个starter
spring-boot-starter-web加载javaee 内嵌tomcat
-->
<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>
</project>
添加数据库的四大要素:
spring.datasource.url=jdbc:mysql://localhost/myfood
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
最主要的类:
package cn.et.dao;
import org.springframework.data.repository.CrudRepository;
import cn.et.Food;
public interface FoodRepository extends CrudRepository<Food, Integer> {
}
然后需要创建数据库的实体类:
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;
}
}
最后就是创建项目的Controller层:
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>> queryForList = jdbc.queryForList("select * from food where foodid="+foodid);
return queryForList.get(0);
}
//添加
@RequestMapping("/saveFood")
public String saveFood(){
Food food = new Food();
food.setFoodname("sss");
food.setImgpath("45454");
food.setPrice(15);
er.save(food);
return "1";
}
//查询
@RequestMapping("/queryFood")
public Food queryFood(){
Food food = er.findOne(39);
return food;
}
public static void main(String[] args) {
SpringApplication.run(SbController.class, args);
}
}
最后访问 通过访问 (http://localhost:8080/savefood)进行添加数据库的数据 如果返回值为 "1" 则添加成功 否则失败