DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `detail` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.max-idle=10 spring.datasource.max-wait=10000 spring.datasource.min-idle=5 spring.datasource.initial-size=5 server.port=8080 server.session.timeout=10 server.tomcat.uri-encoding=UTF-8
package org.amuxia.start;
public class Items {
private Integer id;
private String title;
private String name;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Items() {
super();
// TODO Auto-generated constructor stub
}
public Items(Integer id, String title, String name, String detail) {
super();
this.id = id;
this.title = title;
this.name = name;
this.detail = detail;
}
@Override
public String toString() {
return "Items [id=" + id + ", title=" + title + ", name=" + name + ", detail=" + detail + "]";
}
}
/**
* 新增数据
* @param items
* @return
*/
@RequestMapping("/add")
public @ResponseBody String addItems(Items items) {
String sql = "insert into items (id,title,name,detail) value (?,?,?,?)";
Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章新增成功";
}
return "新增出现错误";
}
/**
* @return
* 查询全部信息
*/
@RequestMapping("/list")
public List<Map<String, Object>> itemsList() {
String sql = "select * from items";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
package org.amuxia.start;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@ComponentScan
@RestController
@RequestMapping("/items")
public class ItemsController {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* @return
* 查询全部信息
*/
@RequestMapping("/list")
public List<Map<String, Object>> itemsList() {
String sql = "select * from items";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
/**
* @param id
* @return
* 根据ID查询单条信息
*/
@RequestMapping("/detail/{id}")
public Map<String, Object> detail(@PathVariable int id) {
Map<String, Object> map = null;
List<Map<String, Object>> list = itemsList();
map = list.get(id);
return map;
}
/**
* 新增数据
* @param items
* @return
*/
@RequestMapping("/add")
public @ResponseBody String addItems(Items items) {
String sql = "insert into items (id,title,name,detail) value (?,?,?,?)";
Object args[] = {items.getId(),items.getTitle(),items.getName(),items.getDetail()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章新增成功";
}
return "新增出现错误";
}
/**
* @param items
* @return
* 删除数据
*/
@RequestMapping("/del")
public @ResponseBody String delItems(Items items) {
String sql = "delete from items where id = ?";
Object args[] = {items.getId()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章删除成功";
}
return "删除出现错误";
}
/**
* @param items
* @return
* 更新操作
*/
@RequestMapping("/upd")
public @ResponseBody String updItems(Items items) {
String sql = "update items set title = ?,detail = ? where id = ?";
Object args[] = {items.getTitle(),items.getDetail(),items.getId()};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章修改成功";
}
return "修改出现错误";
}
}
package org.amuxia.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class App
{
public static void main( String[] args )
{
System.out.println( "start....." );
SpringApplication.run(ItemsController.class, args);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有