1. 程式人生 > >簡單快速的用SpringBoot整合myBatis(註解+xml)

簡單快速的用SpringBoot整合myBatis(註解+xml)

跟著上一篇的節奏我們繼續,SpringBoot整合myBatis的兩種方式:1)註解,2)xml。

首先,我們先來看第一種方式:註解…….

第一步,先引入Springboot整合mybatis,JDBC,mysql的Jar檔案..
    <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId
>
<version>1.3.1</version> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
<!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> 第二步,配置application.properties的資料訪問路徑:

spring.datasource.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    第三步,新建StudentEntity實體類 ,StudentDao介面...

StudentEntity:
public class StudentEntity {
//id
private Integer id;
//姓名
private String name;
//年齡
private Integer age;
//性別
private String sex;
//地址
private String address;
//是否刪除(0:未刪除,1:已刪除)
private Integer isDelete;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public Integer getIsDelete() {
    return isDelete;
}

public void setIsDelete(Integer isDelete) {
    this.isDelete = isDelete;
}

}

//StudentDao介面

@Mapper//生成一個類,作用類似於xml
public interface StudentDao {
//查詢
@Select(“SELECT * FROM student”)
List queryAllStudent();

//新增
@Insert("INSERT INTO student(name,sex,age,address)VALUES(#{name},#{sex},#{age},#{address})")
int insertStudent(StudentEntity student);

//更新
@Update("UPDATE student SET name=#{name},sex=#{sex},age=#{age},address=#{address} WHERE id=#{id} AND is_delete=0")
int updateStudent(StudentEntity entity);

//刪除
@Delete("UPDATE student SET is_delete=1 WHERE id=#{id}")
int deleteStudent(Integer id);

}

    第五步,建立StudentService介面,和StidentServiceImpl類...

StudentService介面:

/**
* SpringBoot 整合 myBatis(註解)
* */
public interface StudentService {

//寫入資料(mybatis)
int insertStudent(StudentEntity studentEntity);

//查詢資料(mybatis)
List queryAllStudent();

//更新資料(mybatis)
int updateStudent(StudentEntity studentEntity);

//刪除資料(mybatis)
int deleteStudent(Integer id);

}


 StudentServiceImpl類:

@Service(“studentServices”)//別名
public class StudentServiceImpl implements StudentService {

@Resource
private StudentDao studentDao;


//myBatis 查詢資料
@Override
public List queryAllStudent() {
    //執行查詢
    List list = studentDao.queryAllStudent();
    //組合資料
    List newDate = dataUtil.getData(list);
    //返回結果
    return newDate;
}

//myBatis 寫入資料
@Override
public int insertStudent(StudentEntity studentEntity) {
    //執行新增
    int row = studentDao.insertStudent(studentEntity);
    //返回結果
    return row;
}

//myBatis 更新資料
@Override
public int updateStudent(StudentEntity studentEntity) {
    //執行更新
    int row = studentDao.updateStudent(studentEntity);
    //返回結果
    return row;
}

//myBatis 刪除資料
@Override
public int deleteStudent(Integer id) {
    //執行刪除
    int row = studentDao.deleteStudent(id);
    //返回接果
    return 0;
}

}

    第七步,StudentController介面,和StudentControllerImpl類

StudentController介面:

public interface StudentController {

//寫入資料(mybatis註解)
String insertStudent();

//查詢資料(mybatis註解)
String queryAllStudent();

//更新資料(mybatis註解)
String updateStudent();

//刪除資料(mybatis註解)
String deleteStudent();

}

StudentControllerImpl類:

@RestController
public class StudentControllerImpl implements StudentController {

@Resource
private StudentService studentServices;

//myBatis 查詢資料(註解)
@RequestMapping("/query")
public String queryAllStudent() {
    //執行查詢
    List list = studentServices.queryAllStudent();
    //組裝資料
    List resList = dataUtil.getData(list);
    //返回結果
    return resList.toString();
}

//myBatis 新增資料(註解)
@RequestMapping("/insert")
public String insertStudent() {
    //新建學生物件並賦值
    StudentEntity stu = new StudentEntity();
    stu.setName("趙四");
    stu.setSex("男");
    stu.setAge(12);
    stu.setAddress("遼寧");
    //執行寫入
    int row = studentServices.insertStudent(stu);
    //判斷結果
    if (row == -1) {
        return "新增失敗";
    } else {
        return "新增成功";
    }
}


//myBatis 更新資料(註解)
@RequestMapping("/update")
public String updateStudent() {
    //新建學生物件並賦值
    StudentEntity stu = new StudentEntity();
    stu.setName("王老七");
    stu.setSex("男");
    stu.setAge(42);
    stu.setAddress("遼寧");
    stu.setId(3);
    //執行寫入
    int row = studentServices.updateStudent(stu);
    //判斷結果
    if (row == -1) {
        return "更新失敗";
    } else {
        return "更新成功";
    }
}

//myBatis 刪除資料(註解)
@RequestMapping("/delete")
public String deleteStudent() {
    //定義初始化
    Integer id = 1;
    //執行寫入
    int row = studentServices.deleteStudent(id);
    //判斷結果
    if (row == -1) {
        return "刪除失敗";
    } else {
        return "刪除成功";
    }
}

}

 剩下最後一個dataUtil類,

import com.demo.entity.StudentEntity;
import java.util.ArrayList;
import java.util.List;

public class dataUtil {

//返回格式資料
public static List getData(List list){
    //新建集合儲存資料
    List newlist = new ArrayList();
    //迴圈取出結果
    for (int i = 0; i <list.size() ; i++) {
        //新建學生物件
        StudentEntity stu = (StudentEntity) list.get(i);
        //填充資料
        newlist.add(stu.getId());
        newlist.add(stu.getName());
        newlist.add(stu.getAge());
        newlist.add(stu.getSex());
        newlist.add(stu.getAddress());
    }
    //返回結果
    return  newlist;
}

}


    寫完了,執行一下我的URL:localhost:8080/query 是不是註解方式很方便,如果有疑問請留言.........




下來看第二種方式:XML.....
        ![這裡寫圖片描述](http://img.blog.csdn.net/20180125175900608?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzY0ODEwNTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)


   首先,第一步:在resources檔案下新建mapper檔案,在mapper檔案中建立Student.xml
    第二步,在application.properties 加上 "mybatis.mapper-locations=classpath:mapper/*.xml"
    第三步,Student.xml程式碼:  
   第四步,StudentDao介面程式碼:

StudentDao介面:

import com.demo.entity.StudentEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper//生成一個類,作用類似於xml
public interface StudentDao {

//查詢所有學生資訊
List findStudent();

//新增學生資訊
int insertStudent(StudentEntity entity);

//更新學生資訊
int updateStudent(StudentEntity entity);

//刪除學生資訊
int deleteStudent(Integer id);

}

    第五步,StudentService和StudentServiceImpl 程式碼:

StudentService介面:

import com.demo.entity.StudentEntity;
import java.util.List;

/**
* SpringBoot 整合 myBatis(XML)
* */
public interface StudentService {

//xml 查詢所有學生資訊
List findStudent();

//xml 新增學生資訊
int insertStudent(StudentEntity entity);

//xml 更新學生資訊
int updateStudent(StudentEntity entity);

//xml 刪除學生資訊
int deleteStudent(Integer id);

}

StudentServiceImpl類:

import com.demo.dao.StudentDao;
import com.demo.entity.StudentEntity;
import com.demo.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(“studentServices”)
public class StudentServiceImpl implements StudentService {

@Resource
private StudentDao studentDao;


//xml 查詢
@Override
public List findStudent() {
    //執行查詢
    List list = studentDao.findStudent();
    //返回結果
    return list;
}

//xml 新增
@Override
public int insertStudent(StudentEntity entity) {
    //執行新增
    int row = studentDao.insertStudent(entity);
    //返回結果
    return row;
}

//xml 更新
@Override
public int updateStudent(StudentEntity entity) {
    //執行更新
    int row = studentDao.updateStudent(entity);
    //返回結果
    return 0;
}

//xml 刪除
@Override
public int deleteStudent(Integer id) {
    //執行刪除
    int row = studentDao.deleteStudent(id);
    //返回結果
    return 0;
}

}

  第六步,StudentController介面和StudentControllerImpl類程式碼:

StudentController介面:

public interface StudentController {

//寫入資料(XML)
String insertStudent();


//更新資料(XML)
String updateStudent();

//刪除資料(XML)
String deleteStudent();

//查詢資料(XML)
String findStudent();

}

StudentControllerImpl類:

import com.demo.controller.StudentController;
import com.demo.entity.StudentEntity;
import com.demo.service.StudentService;
import com.demo.util.dataUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StudentControllerImpl implements StudentController {

@Resource
private StudentService studentServices;


//myBatis 新增資料(XML)
@RequestMapping("/insert")
public String insertStudent() {
    //新建學生物件並賦值
    StudentEntity stu = new StudentEntity();
    stu.setName("熊大");
    stu.setSex("男");
    stu.setAge(8);
    stu.setAddress("遼寧");
    //執行寫入
    int row = studentServices.insertStudent(stu);
    //判斷結果
    if (row == -1) {
        return "新增失敗";
    } else {
        return "新增成功";
    }
}


//myBatis 更新資料(XML)
@RequestMapping("/update")
public String updateStudent() {
    //新建學生物件並賦值
    StudentEntity stu = new StudentEntity();
    stu.setName("光頭強");
    stu.setSex("男");
    stu.setAge(42);
    stu.setAddress("遼寧");
    stu.setId(5);
    //執行寫入
    int row = studentServices.updateStudent(stu);
    //判斷結果
    if (row == -1) {
        return "更新失敗";
    } else {
        return "更新成功";
    }
}

//myBatis 刪除資料(XML)
@RequestMapping("/delete")
public String deleteStudent() {
    //定義初始化
    Integer id = 5;
    //執行寫入
    int row = studentServices.deleteStudent(id);
    //判斷結果
    if (row == -1) {
        return "刪除失敗";
    } else {
        return "刪除成功";
    }
}

//myBatis 查詢資料(XML)
@RequestMapping("/find")
@Override
public String findStudent() {
    //執行查詢
    List list = studentServices.findStudent();
    //組裝返回資料
    List newList = dataUtil.getData(list);
    //System.out.println("jjjj:"+newList);
    //返回結果
    return newList.toString();
}

}

 第七步,StudentEntity類:

package com.demo.entity;

public class StudentEntity {

//id
private Integer id;
//姓名
private String name;
//年齡
private Integer age;
//性別
private String sex;
//地址
private String address;
//是否刪除(0:未刪除,1:已刪除)
private Integer isDelete;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public Integer getIsDelete() {
    return isDelete;
}

public void setIsDelete(Integer isDelete) {
    this.isDelete = isDelete;
}

}

“`
XML方式也就這樣完事了,是不是很簡單。請路過的大神多多指點,如有疑問請留言。。。。。。