Entity
package com.wuti.Entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* Created by wtrover on 2017/6/23.
*/
@Entity
@Table(name = "city")
public class City implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String codeName;
private String state;
private String country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCodeName() {
return codeName;
}
public void setCodeName(String codeName) {
this.codeName = codeName;
}
}
Dao
package com.wuti.Dao;
import com.wuti.Entity.City;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* Created by wtrover on 2017/6/23.
*/
@Repository
public interface CityRepository extends JpaRepository<City, Integer> {
//通过id查询
City findById(int id);
//通过名称查询
City findByName(String name);
//通过代号查询
City findByCodeName(String codeName);
}
Service
package com.wuti.Service;
import com.wuti.Entity.City;
/**
* Created by wtrover on 2017/6/23.
*/
public interface CityService {
//通过id查询
City findById(int id);
//通过名称查询
City findByName(String name);
//通过代号查询
City findByCodeName(String codeName);
//添加一座城市
City add(String name,String codeName,String state,String country);
//更新一座城市
City update(int id,String name,String codeName,String state,String country);
//删除一座城市
void delete(int id);
//删除一座城市
void delete(City city);
}
________________________________________________________________________
package com.wuti.Service;
import com.wuti.Dao.CityRepository;
import com.wuti.Entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by wtrover on 2017/6/23.
*/
@Service(value = "CityService")
public class CityServiceImpl implements CityService {
@Autowired
private CityRepository cityRepository;
@Override
public City findById(int id) {
return cityRepository.findById(id);
}
@Override
public City findByName(String name) {
return cityRepository.findByName(name);
}
@Override
public City findByCodeName(String codeName) {
return cityRepository.findByCodeName(codeName);
}
@Override
public City add(String name, String codeName, String state, String country) {
City city = new City();
city.setName(name);
city.setCodeName(codeName);
city.setState(state);
city.setCountry(country);
return cityRepository.save(city);
}
@Override
public City update(int id,String name,String codeName, String state,String country) {
City city = new City();
city.setId(id);
city.setName(name);
city.setCodeName(codeName);
city.setState(state);
city.setCountry(country);
return cityRepository.save(city);
}
@Override
public void delete(int id) {
cityRepository.delete(id);
}
@Override
public void delete(City city) {
cityRepository.delete(city);
}
}
Controller
package com.wuti.Controller;
import com.wuti.Entity.City;
import com.wuti.Service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by wtrover on 2017/6/23.
*/
@RestController
@RequestMapping(value = "city/")
public class CityController {
@Autowired
private CityService cityService;
/**
* 添加一座城市
* @param name
* @param codeName
* @param state
* @param country
* @return
*/
@PostMapping(value = "add")
public City cityAdd(@RequestParam("name") String name,
@RequestParam(value = "codeName", required = false, defaultValue = "") String codeName,
@RequestParam(value = "state", required = false, defaultValue = "0") String state,
@RequestParam("country") String country) {
return cityService.add(name,codeName,state,country);
}
/**
* 更新一座城市
* @param id
* @param name
* @param codeName
* @param state
* @param country
* @return
*/
@PostMapping(value = "update")
public City cityUpdate(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam(value = "codeName", required = false, defaultValue = "") String codeName,
@RequestParam(value = "state", required = false, defaultValue = "0") String state,
@RequestParam("country") String country) {
return cityService.update(id,name,codeName,state,country);
}
/**
* 删除一座城市
* @param id
*/
@PostMapping(value = "delete")
public void cityDelete(@RequestParam("id") Integer id) {
cityService.delete(id);
}
}