热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot中的mybatis以及打包部署到云主机

工程目录:application.properties#MySQL数据源spring.datasource.urljdbc:mysql:111.229.66.19

工程目录:



 



application.properties

# MySQL 数据源
spring.datasource.url=jdbc:mysql://111.229.66.196:3306/test
#windows系统
#spring.datasource.url=jdbc:mysql://111.229.66.196:3306/test?serverTimezone=UTC
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#日志信息
logging.level.web=debug#详细信息
spring.http.log-request-details=trueserver.port=9000



Dept.java

package com.newer.mybatis2.pojo;
/*** 部门* @author Admin**/
public class Dept {/*** 编号*/int id;/*** 名称*/String title;/*** 所在地*/String loc;// 构造器public Dept() {}// getters,setterspublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}@Overridepublic String toString() {return "Dept [id=" + id + ", title=" + title + ", loc=" + loc + "]";}}



Staff.java

package com.newer.mybits.pojo;/*** 员工(POJO)实体类* * @author wtao**/
public class Staff {/*** 编号(PK)*/private int id;/*** 姓名*/private String name;/*** 职位*/private String job;/*** 手机号*/private String phone;public Staff() {}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 getJob() {return job;}public void setJob(String job) {this.job = job;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "Staff [id=" + id + ", name=" + name + ", job=" + job + ", phone=" + phone + ", getId()=" + getId()+ ", getName()=" + getName() + ", getJob()=" + getJob() + ", getPhone()=" + getPhone() + ", getClass()="+ getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";}}



DeptMapper.java

package com.newer.mybatis2.mapper;import java.util.List;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import com.newer.mybatis2.pojo.Dept;
/*** * 封装了Dept实体类的数据操作*(CRUD)* Mapper:映射,把对象的字段(或者状态)投影到关系数据中的一条记录* @author Admin**/
@Mapper
public interface DeptMapper {@Select("select * from dept_9")ListfindALL();@Select("select * fom dept_9 where id=#{id}")Dept find(int id);// 基于反射:根据类名知道类的属性,方法等
// 反射:某个类型的父类,实现了那些接口,字段,方法@Insert("insert into dept_9(title,loc) values(#{title},#{loc})")void create (Dept d);@Update("update dept_9 set title=#{title},loc=#{loc} where id=#{id}")void update(Dept dept);@Delete("delete from dept_9 where id=#{id}")void remove(int id);}



StaffMapper.java

package com.newer.mybits.mapper;import java.util.List;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import com.newer.mybits.pojo.Staff;/*** 数据持久化(存储)* * MyBatis 是 SQL 映射框架(ORM 对象关系映射 / hibernate JPA )* * @author wtao**/
// 标签,扮演特定的角色/具备功能
@Mapper
public interface StaffMapper {/*** SQL 语句映射成一个抽象方法(MyBatis 框架实现了抽象方法)* * @param staff*/@Insert("insert into staff(name,job,phone) values(#{name},#{job},#{phone})")void save(Staff staff);@Select("select * from staff")List findAll();// #{field} 参数@Select("select * from staff where id=#{id}")Staff findById(int id);/*** 删除* * @param id 是记录主键,#{id}*/@Delete("delete from staff where id=#{id}")void remove(int id);/*** 更新:SQL --> 抽象方法* * @param staff 封装了新数据的参数对象*/@Update("update staff set name=#{name},job=#{job},phone=#{phone} where id=#{id}")void update(Staff staff);}



DeptController.java

package com.newer.mybatis2.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.newer.mybatis2.mapper.DeptMapper;
import com.newer.mybatis2.pojo.Dept;//启用跨域访问
@CrossOrigin@RestController
@RequestMapping("/api/dept")
public class DeptController {/*** 自动装配(注入当前组件依赖的数据操作——)*/@AutowiredDeptMapper deptMapper;@GetMapping()public Listlist(){return deptMapper.findALL();}@GetMapping("/{id}")public Dept load(@PathVariable int id) {return deptMapper.find(id);}// Post JSON@PostMapping()public void create(@RequestBody Dept d) {deptMapper.create(d);}@PutMapping("/{id}")public void update(@PathVariable int id,@RequestBody Dept dept) {// 设置更新数据的iddept.setId(id);deptMapper.update(dept);}@DeleteMapping("/{id}")public ResponseEntity delete(@PathVariable int id) {deptMapper.remove(id);// 推荐return new ResponseEntity(HttpStatus.OK);}
}



StaffController.java

package com.newer.mybits.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.newer.mybits.mapper.StaffMapper;
import com.newer.mybits.pojo.Staff;/*** 控制器:定义 RESTful API 接口* * @author wtao**/
@RestController
//@RequestMapping("/api/v1/staff")
@RequestMapping("/api/staff")
public class StaffController {// 依赖关系&#xff1a;控制器需要数据访问或业务逻辑// 控制器的方法需要用到 mapper 成为依赖// Spring 容器完成依赖注入&#xff1a;为 mapper 赋值&#xff0c;不再为 null&#64;AutowiredStaffMapper mapper;// GET "/api/staff"&#64;GetMappingpublic List findAll() {return mapper.findAll();}// GET "/api/staff/{id}"&#64;GetMapping("/{id}")public Staff findById(&#64;PathVariable int id) {return mapper.findById(id);}// POST "/api/staff"/*** * &#64;param staff HTTP 请求头中负载 JSON 格式* &#64;return*/&#64;PostMappingpublic ResponseEntity create(&#64;RequestBody Staff staff) {mapper.save(staff);return new ResponseEntity(HttpStatus.OK);}// PUT "/api/staff/{id}"&#64;PutMapping("/{id}")public ResponseEntity update(&#64;PathVariable int id, &#64;RequestBody Staff staff) {staff.setId(id);mapper.update(staff);return new ResponseEntity<>(HttpStatus.OK);}// DELETE "/api/staff/{id}&#64;DeleteMapping("/{id}")public void remove(&#64;PathVariable int id) {mapper.remove(id);// TODO ResponseEntity 可选}}



打包部署到云主机


出现这个&#xff0c;表示成功

 


部署的要求&#xff1a;

 



扩展&#xff1a; 


推荐阅读
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有