spring-boot 引入 JdbcTemplate访问数据库
- spring.datasource.url=jdbc:mysql://192.168.1.112:3306/smartcommondb
- spring.datasource.username=root
- spring.datasource.password=allcam
- 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
- spring.datasource.validation-query=SELECT 1
- spring.datasource.test-on-borrow=false
- spring.datasource.test-while-idle=true
- spring.datasource.time-between-eviction-runs-millis=18800
- spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
复制代码
- <!-- MYSQL -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!-- Spring Boot JDBC -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
复制代码
- package com.allcam.uas.dao;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.RowMapper;
- import org.springframework.stereotype.Repository;
- import com.allcam.uas.dao.pojo.ClientInfo;
- @Repository
- public class ClientDao
- {
- @Autowired
- private JdbcTemplate jdbcTemplate;
-
- public List<ClientInfo> getList()
- {
- String sql = "SELECT * FROM tbl_client_info";
- return (List<ClientInfo>)jdbcTemplate.query(sql, new RowMapper<ClientInfo>()
- {
- public ClientInfo mapRow(ResultSet rs, int rowNum)
- throws SQLException
- {
- ClientInfo stu = new ClientInfo();
- stu.setId(rs.getInt("ID"));
- stu.setClientName(rs.getString("CLIENTNAME"));
- return stu;
- }
- });
- }
-
- public void create( Integer id ,String name) {
- jdbcTemplate.update("insert into tbl_client_info(id, CLIENTNAME) values(?, ?)", id, name);
- }
-
-
- public void deleteByName(String name) {
- jdbcTemplate.update("delete from tbl_client_info where CLIENTNAME = ?", name);
- }
- }
复制代码
- package com.allcam.uas.dao.pojo;
- public class ClientInfo
- {
- private int id;
-
- private String zoneId;
-
- private String clientId;
-
- private String clientName;
-
- private String clientType;
-
- private String postCode;
-
- private String address;
-
- private String email;
-
- private String passWord;
-
- private String phone;
-
- private String fax;
-
- private String status;
-
- private String remark;
-
- private String createDate;
-
- private String lastDate;
-
- public int getId()
- {
- return id;
- }
-
- public void setId(int id)
- {
- this.id = id;
- }
-
- public String getZoneId()
- {
- return zoneId;
- }
-
- public void setZoneId(String zoneId)
- {
- this.zoneId = zoneId;
- }
-
- public String getClientId()
- {
- return clientId;
- }
-
- public void setClientId(String clientId)
- {
- this.clientId = clientId;
- }
-
- public String getClientName()
- {
- return clientName;
- }
-
- public void setClientName(String clientName)
- {
- this.clientName = clientName;
- }
-
- public String getClientType()
- {
- return clientType;
- }
-
- public void setClientType(String clientType)
- {
- this.clientType = clientType;
- }
-
- public String getPostCode()
- {
- return postCode;
- }
-
- public void setPostCode(String postCode)
- {
- this.postCode = postCode;
- }
-
- public String getAddress()
- {
- return address;
- }
-
- public void setAddress(String address)
- {
- this.address = address;
- }
-
- public String getEmail()
- {
- return email;
- }
-
- public void setEmail(String email)
- {
- this.email = email;
- }
-
- public String getPassWord()
- {
- return passWord;
- }
-
- public void setPassWord(String passWord)
- {
- this.passWord = passWord;
- }
-
- public String getPhone()
- {
- return phone;
- }
-
- public void setPhone(String phone)
- {
- this.phone = phone;
- }
-
- public String getFax()
- {
- return fax;
- }
-
- public void setFax(String fax)
- {
- this.fax = fax;
- }
-
- public String getStatus()
- {
- return status;
- }
-
- public void setStatus(String status)
- {
- this.status = status;
- }
-
- public String getRemark()
- {
- return remark;
- }
-
- public void setRemark(String remark)
- {
- this.remark = remark;
- }
-
- public String getCreateDate()
- {
- return createDate;
- }
-
- public void setCreateDate(String createDate)
- {
- this.createDate = createDate;
- }
-
- public String getLastDate()
- {
- return lastDate;
- }
-
- public void setLastDate(String lastDate)
- {
- this.lastDate = lastDate;
- }
-
- @Override
- public String toString()
- {
- return "ClientInfo [id=" + id + ", zoneId=" + zoneId + ", clientId=" + clientId + ", clientName=" + clientName
- + ", clientType=" + clientType + ", postCode=" + postCode + ", address=" + address + ", email=" + email
- + ", passWord=" + passWord + ", phone=" + phone + ", fax=" + fax + ", status=" + status + ", remark="
- + remark + ", createDate=" + createDate + ", lastDate=" + lastDate + "]";
- }
- }
复制代码
- package com.allcam.uas.modules.plat.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.allcam.uas.common.BaseController;
- import com.allcam.uas.dao.ClientDao;
- import com.allcam.uas.dao.pojo.ClientInfo;
- import com.allcam.uas.modules.plat.model.PlatInfo;
- import com.allcam.uas.modules.plat.model.PlatInfoReq;
- import com.allcam.uas.modules.plat.model.PlatInfoResp;
- import com.allcam.uas.sys.SystemSettings;
- import io.swagger.annotations.Api;
- @Controller
- @RequestMapping(value = "/api/plat")
- public class PlatController extends BaseController
- {
-
- @Autowired
- ClientDao clientDao;
- @ResponseBody
- @RequestMapping(value = "/info", method = RequestMethod.POST)
- public PlatInfoResp info(@RequestBody
- final PlatInfoReq platInfo)
-
- {
- List<ClientInfo> list = clientDao.getList();
- System.out.println(list);
- clientDao.deleteByName("2");
- List<ClientInfo> list2 = clientDao.getList();
- System.out.println(list2);
- PlatInfo platBean = new PlatInfo();
- return resp;
- }
- }
复制代码 |