SpringBoot呼び出しmysqlアクション
27347 ワード
IDEA構成SpringBootプロジェクト
SpringBootは添削を実現
記録機能は簡単で、Controllerを作成して、それからサービスの中のSQL文を呼び出して添削して調べます
使用するjarパッケージ
1.エンティティークラスの作成
2.サービスを作成し、データベースへの接続を行うための追加削除・変更操作(ここにリストされています)
3.コントローラの作成
SpringBootは添削を実現
記録機能は簡単で、Controllerを作成して、それからサービスの中のSQL文を呼び出して添削して調べます
使用するjarパッケージ
//Gson
implementation 'com.google.code.gson:gson:2.8.5'
//json
implementation 'org.json:json:20160810'
1.エンティティークラスの作成
public class TestBean {
private long id;
private String userName;
private String password;
private int age;
private String phoneNumber;
private String headPicture;
private Date addDate;
private Date updateDate;
private int state;
@Override
public String toString() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id",id);
jsonObject.put("userName",userName);
jsonObject.put("password",password);
jsonObject.put("age",age);
jsonObject.put("phoneNumber",phoneNumber);
jsonObject.put("headPicture",headPicture);
jsonObject.put("addDate",addDate);
jsonObject.put("updateDate",updateDate);
jsonObject.put("state",state);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
public long getId() {
return id;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getHeadPicture() {
return headPicture;
}
public Date getAddDate() {
return addDate;
}
public Date getUpdateDate() {
return updateDate;
}
public int getState() {
return state;
}
public void setId(long id) {
this.id = id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(int age) {
this.age = age;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setHeadPicture(String headPicture) {
this.headPicture = headPicture;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public void setState(int state) {
this.state = state;
}
}
2.サービスを作成し、データベースへの接続を行うための追加削除・変更操作(ここにリストされています)
/**
*
* **/
@Mapper
public interface TestService {
//
@Select("SELECT table_name FROM information_schema.TABLES WHERE table_name ='AppComment'")
String isHaveTable();
//
@Insert("CREATE TABLE IF NOT EXISTS `runoob_bbb`(" +
"`runoob_id` INT UNSIGNED AUTO_INCREMENT," +//
"`runoob_title` VARCHAR(100) NOT NULL," +//
"PRIMARY KEY(`runoob_id`)" +//
")ENGINE=InnoDB DEFAULT CHARSET=utf8")
int createComment(TestBean bean);// 0
//
@Insert("INSERT INTO `runoob_bbb`(" +
"`runoob_title`" +
")VALUES(" +
"' '" +//
")")
int addComment(TestBean bean);
//
@Delete("DELETE FROM `bingo_comment` WHERE `app_id`=-1")
int deleteComment(TestBean bean);
//
@Update("UPDATE `runoob_bbb` SET `runoob_title`=' ' WHERE `runoob_id`=1")
int updateComment(TestBean bean);
// #{userName}
@Select("SELECT * FROM `runoob_bbb` where `runoob_id`=5")
TestBean selectComment(TestBean bean);
//
@Update("ALTER TABLE `runoob_bbb` ADD COLUMN `runoob_test` INT NOT NULL DEFAULT 0 AFTER `runoob_title`")
int updateTableColumn();
//
@Delete("ALTER TABLE `runoob_bbb` DROP COLUMN `GATEWAYID`")
int deleteTableColumn();
// ( after)
@Update("ALTER TABLE `runoob_bbb` CHANGE `runoob_title` `runoob_title` VARCHAR(100) NOT NULL AFTER `runoob_test`")
int updateTableColumnOrder();
// ( , , )
@Update("Alter table `runoob_bbb` change `runoob_test` `runoob_test` int(10)")
int updateTableDeleteKeyAuto();
// ( , , )
@Delete("alter table `runoob_bbb` drop primary key")
int updateTableDeleteKey();
// ( , , )
@Update("ALTER TABLE `runoob_bbb` add primary key (runoob_id)")
int updateTableUpdateKey();
//
// alter table t1 change b b bigint not null;
// alter table infos change list list tinyint not null default ‘0′;
@Update("alter table `bingo_reply` change `reply_time` `reply_time` VARCHAR(100) NOT NULL")
int updateTableType();
// -- , 、 、 、
@Update("alter table bingo_comment change conment_time comment_time int(10) DEFAULT 0 COMMENT ' '")
int renameComment();
//
@Delete("DROP TABLE `runoob_aaa`")
int deleteTable();
//
@Delete("DROP TABLE `runoob_bbb`")
int deleteTable1();
//
@Delete("DROP TABLE `runoob_tbl`")
int deleteTable2();
}
3.コントローラの作成
/**
*
* **/
@RestController
@RequestMapping("/admin")
public class TestController {
@Autowired
private TestService service;
//https://store-api.bingo.fun/files/public/14/icon/x50
@RequestMapping("addComment")
public int setComment(TestBean bean){
try {
int a = service.updateTableType();
System.out.print(a);
return a;
}catch (Exception e){
System.out.print(e.toString());
return -123;
}
}
}