整合springboot+mysql+mybatis之新增(一)


結構

 

 

 

1使用starter,訪問maven倉庫地址

http://mvnrepository.com

或者直接在pom.xml中添加依賴,注意,如果包不能被引用,把<scope>給注釋掉

 

<!-- 引入mybatis的starter的包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<!-- <scope>runtime</scope> -->
</dependency>

<!-- mysql的驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>

<!-- 引入第三方驅動源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>

 

 2在application.properties中新增mysql配置文件

#整合mysql的配置文件
#mysql加載驅動
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jdbc數據庫連接
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
#mysql賬號
spring.datasource.username=root
#mysql密碼
spring.datasource.password=456789
#數據連接源,如果注釋掉,數據源使用默認的(com.zaxxer.hikari.HikariDataSource)
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

 

 

 

 

 3在數據庫mysql中創建對應的表student

CREATE TABLE `student` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(0) NULL DEFAULT NULL,
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`money` int(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

4創建student類

package springboot_mysql.bean;

public class Student {
/**
* 自增id
*/
private int id;

/**
* 學生姓名
*/
private String name;
/**
* 學生年齡
*/
private int age;
/**
* 學生性別
*/
private String sex;
/**
* 學生電話
*/
private String phone;
/**
* 學生存款
*/
private int money;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Student(int id, String name, int age, String sex, String phone, int money) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
this.money = money;
}
public Student() {
super();
}

}

 

 

 

 2創建mapper接口StudentMapper

package springboot_mysql.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;

import springboot_mysql.bean.Student;

/**
* 訪問數據庫的接口
* @author Administrator
*
*/
public interface StudentMapper {
/**
* sql語句中推薦使用#{},而不是${},因為存在sql注入的危險,#{}返回的值是?
* @Insert("insert into student(name,age,sex,phone,money)values(#{name},#{age},#{sex},#{phone},#{money})")是新增的sql的方法
* @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")是獲取自增的主鍵id
* useGeneratedKeys是是否返回值,true返回,flase不返回
* keyColumn對應的是Student類中的屬性id
* keyProperty對應的是數據庫mysql中的表Student中的字段id
* @param student
* @return
*/
@Insert("insert into student(name,age,sex,phone,money)values(#{name},#{age},#{sex},#{phone},#{money})")
@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
int insert(Student student);

}

 

 3創建service接口,

 

 

package springboot_mysql.service;

import springboot_mysql.bean.Student;

public interface StudentService {

public int add(Student student);
}

4創建service的實現類

 

 

package springboot_mysql.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import springboot_mysql.bean.Student;
import springboot_mysql.mapper.StudentMapper;
import springboot_mysql.service.StudentService;
/**
* @Service 這個注解能夠讓controller掃描StudentServiceImpl
* @author Administrator
*
*/
@Service
public class StudentServiceImpl implements StudentService{

@Autowired
private StudentMapper studentmapper;

@Override
public int add(Student student) {
studentmapper.insert(student);
int id=student.getId();
return id;
}

}

5創建controller

 

 

package springboot_mysql.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springboot_mysql.bean.JsonData;
import springboot_mysql.bean.Student;
import springboot_mysql.service.StudentService;

@RestController
@RequestMapping("/api/v1/student")
public class StudentController {

@Autowired
private StudentService studentService;

@GetMapping("add")
public Object add() {
Student student =new Student();
student.setName("李明");
student.setAge(20);
student.setSex("男");
student.setPhone("13662626356");
student.setMoney(1000);
int id=studentService.add(student);
return JsonData.buildSuccess(id);
}
}

6創建JsonData類

package springboot_mysql.bean;

import java.io.Serializable;

public class JsonData implements Serializable{

private static final long serialVersionUID = 1L;

//狀態碼,0表示成功,-1表示失敗
private int code;

//結果
private Object data;

//返回錯誤消息
private String msg;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public JsonData(int code, Object data, String msg) {
super();
this.code = code;
this.data = data;
this.msg = msg;
}

public static Object buildSuccess(int id) {

return id;
}
}

7run application  ,然后訪問http://localhost:8080//api/v1/student/add

返回數據庫表中新建數據的id,數據庫中的數據會新增

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM