简单快速的用SpringBoot整合myBatis(注解+xml)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36481052/article/details/79154236

跟着上一篇的节奏我们继续,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方式也就这样完事了,是不是很简单。请路过的大神多多指点,如有疑问请留言。。。。。。

猜你喜欢

转载自blog.csdn.net/qq_36481052/article/details/79154236