玩转spring boot 结合AngularJs和JDBC(4)

参考官方例子:http://spring.io/guides/gs/relational-data-access/

 一、项目准备

在建立mysql数据库后新建表“t_order”

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `t_order`
-- ----------------------------
DROP TABLE IF EXISTS `t_order`;
CREATE TABLE `t_order` (
 `order_id` varchar(36) NOT NULL,
 `order_no` varchar(50) DEFAULT NULL,
 `order_date` datetime DEFAULT NULL,
 `quantity;` int(11) DEFAULT NULL,
 PRIMARY KEY (`order_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_order
-- ----------------------------


修改pom.xml


  4.0.0

  com.github.carter659
  spring04
  0.0.1-SNAPSHOT
  jar

  spring04
  http://maven.apache.org

  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.2.RELEASE
  

  
    UTF-8
    1.8
  

  

    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-devtools
      true
    
    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    
    
      org.springframework.boot
      spring-boot-starter-jdbc
    
    
      mysql
      mysql-connector-java
    
    
      commons-dbcp
      commons-dbcp
    


  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  


二、编写类文件:

修改App.java

package com.github.carter659.spring04;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 博客出处:http://www.cnblogs.com/GoodHelper/
 * 
 * @author 刘冬
 *
 */
@SpringBootApplication
public class App {

  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }
}

新建数据载体类文件“Order.java”

package com.github.carter659.spring04;

import java.util.Date;
/**
 * 博客出处:http://www.cnblogs.com/GoodHelper/
 * @author 刘冬
 *
 */
public class Order {
  public String id;
  public String no;
  public Date date;
  public int quantity;

  /**
   * 省略 get set
   */
}

新建数据持久层类“OrderDao.java”

package com.github.carter659.spring04;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

/**
 * 博客出处:http://www.cnblogs.com/GoodHelper/
 * @author 刘冬
 *
 */
@Repository
public class OrderDao {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List findAll() {
    List list = new ArrayList<>();
    String sql = " select * from t_order ";
    SqlRowSet rows = jdbcTemplate.queryForRowSet(sql, new Object[] {});
    while (rows.next()) {
      Order order = new Order();
      list.add(order);
      order.id = rows.getString("order_id");
      order.no = rows.getString("order_no");
      order.date = rows.getDate("order_date");
      order.quantity = rows.getInt("quantity");
    }
    return list;
  }

  public Order get(String id) {
    Order order = null;
    String sql = " select * from t_order where order_id = ? ";
    SqlRowSet rows = jdbcTemplate.queryForRowSet(sql, new Object[] { id });
    while (rows.next()) {
      order = new Order();
      order.id = rows.getString("order_id");
      order.no = rows.getString("order_no");
      order.date = rows.getDate("order_date");
      order.quantity = rows.getInt("quantity");
    }
    return order;
  }

  public String insert(Order order) {
    String id = UUID.randomUUID().toString();
    String sql = " insert into t_order ( order_id , order_no , order_date , quantity ) values (?,?,?,?) ";
    jdbcTemplate.update(sql,
        new Object[] { id, order.no, new java.sql.Date(order.date.getTime()), order.quantity });
    return id;
  }

  public void update(Order order) {
    String sql = " update t_order set order_no = ? , order_date = ? , quantity = ? where order_id = ? ";
    jdbcTemplate.update(sql,
        new Object[] { order.no, new java.sql.Date(order.date.getTime()), order.quantity, order.id });
  }

  public void delete(String id) {
    String sql = " delete from t_order where order_id = ? ";
    jdbcTemplate.update(sql, new Object[] { id });
  }
}

其中对数据库的操作,顾名思义:

findAll-->查询所有数据

get-->通过id获取数据

insert-->插入数据

update-->修改数据

delete-->删除数据

新建控制器“MainController.java”

package com.github.carter659.spring04;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;

@Controller
public class MainController {

  @Autowired
  private OrderDao orderDao;

  @GetMapping("/")
  public String index() {
    return "index";
  }

  @PostMapping("/save")
  public @ResponseBody Map save(@RequestBody Order order) {
    Map result = new HashMap<>();
    if (StringUtils.isNullOrEmpty(order.id))
      order.id = orderDao.insert(order);
    else {
      orderDao.update(order);
    }
    result.put("id", order.id);
    return result;
  }

  @PostMapping("/get")
  public @ResponseBody Object get(String id) {
    return orderDao.get(id);
  }

  @PostMapping("/findAll")
  public @ResponseBody Object findAll() {
    return orderDao.findAll();
  }

  @PostMapping("/delete")
  public @ResponseBody Map delete(String id) {
    Map result = new HashMap<>();
    orderDao.delete(id);
    result.put("id", id);
    return result;
  }
}

三、新建thymeleaf模板

新建文件“src/main/resources/templates/index.html”





玩转spring boot――结合JDBC




  

玩转spring boot――结合JDBC

from 刘冬的博客



清单信息:

编号: 日期: 数量:

清单列表:

操作 编号 日期 数量
{{row.no}} {{row.date}} {{row.quantity}}

点击访问原版博客

使用angularjs的ajax调用spring boot mv的后台方法。

四、数据库连接

新建“src/main/resources/application.properties”文件

spring.datasource.initialize=false
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

完整的结构为:

玩转spring boot 结合AngularJs和JDBC(4)_第1张图片

五、运行效果

在浏览器输入“http://localhost:8080/”

玩转spring boot 结合AngularJs和JDBC(4)_第2张图片

添加数据:

玩转spring boot 结合AngularJs和JDBC(4)_第3张图片

保存新数据:

玩转spring boot 结合AngularJs和JDBC(4)_第4张图片

玩转spring boot 结合AngularJs和JDBC(4)_第5张图片

编辑数据:

玩转spring boot 结合AngularJs和JDBC(4)_第6张图片

删除数据:

玩转spring boot 结合AngularJs和JDBC(4)_第7张图片

删除完成的效果:

玩转spring boot 结合AngularJs和JDBC(4)_第8张图片

代码:https://github.com/carter659/spring-boot-04.git

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(玩转spring boot 结合AngularJs和JDBC(4))