# 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
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 + "]";}}
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() + "]";}}
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")List
// 反射:某个类型的父类,实现了那些接口,字段,方法@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);}
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
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 List
}
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 {// 依赖关系:控制器需要数据访问或业务逻辑// 控制器的方法需要用到 mapper 成为依赖// Spring 容器完成依赖注入:为 mapper 赋值,不再为 null@AutowiredStaffMapper mapper;// GET "/api/staff"@GetMappingpublic List
出现这个,表示成功
部署的要求: