准备工作:
1.首先我们在/src/main/java创建一个包名为bean 底下放入4个类(pay(缴费记录),pay(家长),stu(学生),tables(报名表))
2.创建一个包controller 放1个类tableController
3./src/main/resources data.sql, schema.sql , application.properties
public class par {
private int par_id; //家长ID
private String par_name; //家长姓名
private String par_phone; //家长联系方式
public int getPar_id() {
return par_id;
}
public void setPar_id(int par_id) {
this.par_id = par_id;
}
public String getPar_name() {
return par_name;
}
public void setPar_name(String par_name) {
this.par_name = par_name;
}
public String getPar_phone() {
return par_phone;
}
public void setPar_phone(String par_phone) {
this.par_phone = par_phone;
}
}
pay
public class pay {
private int pay_id; //缴费ID
private int pay_price; //金额
private String pay_time; //时间
public int getPay_id() {
return pay_id;
}
public void setPay_id(int pay_id) {
this.pay_id = pay_id;
}
public int getPay_price() {
return pay_price;
}
public void setPay_price(int pay_price) {
this.pay_price = pay_price;
}
public String getPay_time() {
return pay_time;
}
public void setPay_time(String pay_time) {
this.pay_time = pay_time;
}
}
stu
public class stu {
private int stu_id; //学生ID
private String stu_name; //学生姓名
private int stu_age; //学生年龄
private String stu_sex; //学生性别
public int getStu_id() {
return stu_id;
}
public void setStu_id(int stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public int getStu_age() {
return stu_age;
}
public void setStu_age(int stu_age) {
this.stu_age = stu_age;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
}
tables
public class tables {
private int tables_id;
private String tables_time;
private par parents;
private pay pay;
private stu student;
public int getTables_id() {
return tables_id;
}
public void setTables_id(int tables_id) {
this.tables_id = tables_id;
}
public String getTables_time() {
return tables_time;
}
public void setTables_time(String tables_time) {
this.tables_time = tables_time;
}
public par getPr() {
return parents;
}
public void setPr(par par) {
this.parents = par;
}
public pay getPy() {
return pay;
}
public void setPy(pay py) {
this.pay = py;
}
public stu getSu() {
return student;
}
public void setSu(stu su) {
this.student = su;
}
}
tableController
@RestController
public class tableController {
@Autowired
JdbcTemplate jdbc;
@GetMapping("/tables")
public Object getTab()
{ String sqls="select * from tables";
List<Map<String, Object>> listss = jdbc.queryForList(sqls);
List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>();
for(Map<String,Object> map6:listss)
{Map<String,Object> maps=new LinkedHashMap<String,Object>();
Object a=map6.get("tables_id");
int id=Integer.parseInt(String.valueOf(a));
String sql = "select * from tables "
+ "where tables_id = " + id;
List<Map<String, Object>> list1 = jdbc.queryForList(sql);
for(Map<String, Object> map1:list1)
{
for(String key:map1.keySet())
{
maps.put(key, map1.get(key));
}
}
String sql1 = "select * from pay "
+ "where pay_id = " + id;
List<Map<String, Object>> list2 = jdbc.queryForList(sql1);
for(Map<String, Object> map2:list2)
{
maps.put("pay", map2);
}
String sql2 = "select * from par "
+ "where par_id = " + id;
List<Map<String, Object>> list3 = jdbc.queryForList(sql2);
for(Map<String, Object> map3:list3)
{
maps.put("parents", map3);
}
String sql3 = "select * from stu "
+ "where stu_id = " + id;
List<Map<String, Object>> list4 = jdbc.queryForList(sql3);
for(Map<String, Object> map4:list4)
{
maps.put("student", map4);
}
lists.add(maps);
}
return lists;
}
@GetMapping("/tables/{id}")
public Object getTabById(@PathVariable("id") String id)
{
List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>();
Map<String,Object> maps=new LinkedHashMap<String,Object>();
String sql = "select * from tables "
+ "where tables_id = " + id;
List<Map<String, Object>> list1 = jdbc.queryForList(sql);
for(Map<String, Object> map1:list1)
{
for(String key:map1.keySet())
{
maps.put(key, map1.get(key));
}
}
String sql1 = "select * from pay "
+ "where pay_id = " + id;
List<Map<String, Object>> list2 = jdbc.queryForList(sql1);
for(Map<String, Object> map2:list2)
{
maps.put("pay", map2);
}
String sql2 = "select * from par "
+ "where par_id = " + id;
List<Map<String, Object>> list3 = jdbc.queryForList(sql2);
for(Map<String, Object> map3:list3)
{
maps.put("parents", map3);
}
String sql3 = "select * from stu "
+ "where stu_id = " + id;
List<Map<String, Object>> list4 = jdbc.queryForList(sql3);
for(Map<String, Object> map4:list4)
{
maps.put("student", map4);
}
lists.add(maps);
return lists.get(0);
}
@DeleteMapping("tables/{id}")
public void delTabByID(@PathVariable("id") int id)
{
String sql = "delete tables,par,pay,stu " +
" from tables,par,pay,stu " +
" where tables.tables_id = " + id +
" and par.par_id = " + id +
" and pay.pay_id = " + id +
" and stu.stu_id = " + id;
jdbc.execute(sql);
}
@PostMapping("tables")
public Object postTables(@RequestBody tables tab)
{
/*
{
"tables_id": 4,
"tables_time": "2018.6.6",
"py":{
"pay_price": 600,
"pay_time": "2018.6.6"
},
"pr":{
"par_name": "mom6",
"par_phone": "156"
},
"su":{
"stu_name": "stu6",
"stu_sex": "male",
"stu_age": 66
}
}
*/
//tables
String sql1= "insert into tables (tables_time) value(?)";
Object[] args1 = {tab.getTables_time()};
int temp = jdbc.update(sql1,args1);
//pay
String sql2 = "insert into pay (pay_price,pay_time) value(?,?)";
Object[] args2 = {tab.getPy().getPay_price(),tab.getPy().getPay_time()};
int temp2 = jdbc.update(sql2,args2);
//par
String sql3 = "insert into par (par_name,par_phone) value(?,?)";
Object[] args3 = {tab.getPr().getPar_name(),tab.getPr().getPar_phone()};
int temp3 = jdbc.update(sql3,args3);
//stu
String sql4 = "insert into stu (stu_name,stu_sex,stu_age) value(?,?,?)";
Object[] args4 = {tab.getSu().getStu_name(),tab.getSu().getStu_sex(),tab.getSu().getStu_age()};
int temp4 = jdbc.update(sql4,args4);
if(temp == 1 && temp2 == 1 && temp3 ==1 && temp4 == 1 )
{int tempId = tab.getTables_id() ;
List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>();
Map<String,Object> maps=new LinkedHashMap<String,Object>();
String sql = "select * from tables "
+ "where tables_id = " + tempId;
List<Map<String, Object>> list1 = jdbc.queryForList(sql);
for(Map<String, Object> map1:list1)
{
for(String key:map1.keySet())
{
maps.put(key, map1.get(key));
}
}
String sql6 = "select * from pay "
+ "where pay_id = " + tempId;
List<Map<String, Object>> list2 = jdbc.queryForList(sql6);
for(Map<String, Object> map2:list2)
{
maps.put("pay", map2);
}
String sql7 = "select * from par "
+ "where par_id = " + tempId;
List<Map<String, Object>> list3 = jdbc.queryForList(sql7);
for(Map<String, Object> map3:list3)
{
maps.put("parents", map3);
}
String sql8 = "select * from stu "
+ "where stu_id = " + tempId;
List<Map<String, Object>> list4 = jdbc.queryForList(sql8);
for(Map<String, Object> map4:list4)
{
maps.put("student", map4);
}
lists.add(maps);
return lists.get(0);
}
return null;
}
@PutMapping("tables")
public Object putTables(@RequestBody tables tab)
{
/*
*
*
{
"tables_id": 1,
"tables_time": "2018.666666",
"py":{
"pay_id": 1,
"pay_price": 600,
"pay_time": "2018.6.6"
},
"pr":{
"par_id": 1,
"par_name": "mom6",
"par_phone": "156"
},
"su":{
"stu_id": 1,
"stu_name": "stu6",
"stu_sex": "male",
"stu_age": 66
}
}
*/
//sql1
String sql1 = "update tables set tables_time=? where tables_id=?";
Object[] args1 = {tab.getTables_time(),tab.getTables_id()};
int temp1 = jdbc.update(sql1,args1);
//pay
String sql2 = "update pay set pay_price=?,pay_time=? where pay_id=?";
Object[] args2 = {tab.getPy().getPay_price(),tab.getPy().getPay_time(),tab.getPy().getPay_id()};
int temp2 = jdbc.update(sql2,args2);
//par
String sql3 = "update par set par_name=?,par_phone=? where par_id=?";
Object[] args3 = {tab.getPr().getPar_name(),tab.getPr().getPar_phone(),tab.getPr().getPar_id()};
int temp3 = jdbc.update(sql3,args3);
//stu
String sql4 = "update stu set stu_name=?,stu_sex=?,stu_age=? where stu_id=?";
Object[] args4 = {tab.getSu().getStu_name(),tab.getSu().getStu_sex(),tab.getSu().getStu_age(),tab.getSu().getStu_id()};
int temp4 = jdbc.update(sql4,args4);
if(temp1 == 1 && temp2 ==1 && temp3 ==1 && temp4 ==1 )
{int tempId = tab.getTables_id() ;
List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>();
Map<String,Object> maps=new LinkedHashMap<String,Object>();
String sql = "select * from tables "
+ "where tables_id = " + tempId;
List<Map<String, Object>> list1 = jdbc.queryForList(sql);
for(Map<String, Object> map1:list1)
{
for(String key:map1.keySet())
{
maps.put(key, map1.get(key));
}
}
String sql6 = "select * from pay "
+ "where pay_id = " + tempId;
List<Map<String, Object>> list2 = jdbc.queryForList(sql6);
for(Map<String, Object> map2:list2)
{
maps.put("pay", map2);
}
String sql7 = "select * from par "
+ "where par_id = " + tempId;
List<Map<String, Object>> list3 = jdbc.queryForList(sql7);
for(Map<String, Object> map3:list3)
{
maps.put("parents", map3);
}
String sql8 = "select * from stu "
+ "where stu_id = " + tempId;
List<Map<String, Object>> list4 = jdbc.queryForList(sql8);
for(Map<String, Object> map4:list4)
{
maps.put("student", map4);
}
lists.add(maps);
return lists.get(0);
}
return tab;
}
}
data.sql
insert into tables values (1,‘2018.1.1’);
insert into tables values (2,‘2018.2.2’);
insert into tables values (3,‘2018.3.3’);
insert into stu values (1,‘stu1’,‘male’,11);
insert into stu values (2,‘stu2’,‘male’,12);
insert into stu values (3,‘stu3’,‘male’,13);
insert into par values (1,‘mom1’,‘151’);
insert into par values (2,‘mom2’,‘152’);
insert into par values (3,‘mom3’,‘153’);
insert into pay values (1,100,‘2018.1.1’);
insert into pay values (2,200,‘2018.2.2’);
insert into pay values (3,300,‘2018.3.3’);
schema.sql
insert into tables values (1,‘2018.1.1’);
insert into tables values (2,‘2018.2.2’);
insert into tables values (3,‘2018.3.3’);
insert into stu values (1,‘stu1’,‘male’,11);
insert into stu values (2,‘stu2’,‘male’,12);
insert into stu values (3,‘stu3’,‘male’,13);
insert into par values (1,‘mom1’,‘151’);
insert into par values (2,‘mom2’,‘152’);
insert into par values (3,‘mom3’,‘153’);
insert into pay values (1,100,‘2018.1.1’);
insert into pay values (2,200,‘2018.2.2’);
insert into pay values (3,300,‘2018.3.3’);
application.properties
注意 是8666 数据库的密码为无 创建一个数据库为baoming
server.port=8666
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/baoming?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver