转载

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

工程目录:


 


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=true

server.port=9000

Dept.java

package com.newer.mybatis2.pojo;
/**
 * 部门
 * @author Admin
 *
 */
public class Dept {

	/**
	 * 编号
	 */
	int id;
	
	/**
	 * 名称
	 */
	
	String title;
	
	
	/**
	 * 所在地
	 */
	String loc;
	
//	构造器
	public Dept() {
		
	}

//	getters,setters
	public 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;
	}

	
	@Override
	public 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;
	}

	@Override
	public 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")
	List<Dept>findALL();
	
	@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<Staff> 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 {

	/**
	 * 自动装配(注入当前组件依赖的数据操作——)
	 */
	@Autowired
	DeptMapper deptMapper;
	
	@GetMapping()
	public List<Dept>list(){
		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) {
		
//		设置更新数据的id
		dept.setId(id);
		deptMapper.update(dept);
	}
	
	@DeleteMapping("/{id}")
	public ResponseEntity<String> delete(@PathVariable int id) {
		deptMapper.remove(id);
		
//		推荐
		return new ResponseEntity<String>(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 {

	// 依赖关系:控制器需要数据访问或业务逻辑

	// 控制器的方法需要用到 mapper 成为依赖

	// Spring 容器完成依赖注入:为 mapper 赋值,不再为 null
	@Autowired
	StaffMapper mapper;

	// GET "/api/staff"
	@GetMapping
	public List<Staff> findAll() {
		return mapper.findAll();
	}

	// GET "/api/staff/{id}"
	@GetMapping("/{id}")
	public Staff findById(@PathVariable int id) {
		return mapper.findById(id);
	}

	// POST "/api/staff"
	/**
	 * 
	 * @param staff HTTP 请求头中负载 JSON 格式
	 * @return
	 */
	@PostMapping
	public ResponseEntity<String> create(@RequestBody Staff staff) {
		mapper.save(staff);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

	// PUT "/api/staff/{id}"
	@PutMapping("/{id}")
	public ResponseEntity<String> update(@PathVariable int id, @RequestBody Staff staff) {

		staff.setId(id);
		mapper.update(staff);

		return new ResponseEntity<>(HttpStatus.OK);
	}

	// DELETE "/api/staff/{id}
	@DeleteMapping("/{id}")
	public void remove(@PathVariable int id) {
		mapper.remove(id);

		// TODO ResponseEntity 可选
	}

}

打包部署到云主机


出现这个,表示成功

 


部署的要求:

 


扩展: 

正文到此结束
本文目录