Spring Boot整合MyBatis

目录

 

前言

开始整合

第一步:创建数据库表,插入测试数据

 第二步:创建项目,并创建相关的包、类或者接口(附代码)

1、Student(pojo包)

2、StudentDao(dao包)

3、StudentMapper(mapper包)

4、StudentService(service包)

5、StudentServiceImpl(serviceImpl包)

6、StudentController(controller包)

7、Student.jsp(webapp/jsp目录下)

第三步:修改pom.xml

第四步:修改application.properties文件

第五步:修改启动类xxxApplication

第六步:部署运行


前言

在上一篇博文中,我记录了关于Spring Boot项目搭建的笔记,我们并没有进行数据库操作,但是在真正的环境中这是不可能的,想要持久化就必须要有数据库这个玩意,今天我们就一起在Spring Boot中整合MyBatis!

因为关于项目的搭建,在上一篇文章已经说过了,所以这一篇不再从头进行,请自行搭建Spring Boot工程环境,新手可参考本人的上一篇文章,我们直接从搭建好的目录结构开始!

开始整合

第一步:创建数据库表,插入测试数据

DROP TABLE IF EXISTS `Student`;
CREATE TABLE `Student` (
  `sID` int(11) DEFAULT NULL,
  `sName` varchar(255) DEFAULT NULL,
  `ClassId` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `Student` VALUES ('1', '小张', '1');
INSERT INTO `Student` VALUES ('2', '小明', '1');
INSERT INTO `Student` VALUES ('3', '小李', '2');
INSERT INTO `Student` VALUES ('4', '小强', '2');
INSERT INTO `Student` VALUES ('5', '美女', '2');

 第二步:创建项目,并创建相关的包、类或者接口(附代码)

 项目创建好是这个结构,创建的时候一定不要忘了选上SQL依赖和MyBatis依赖!!!不懂的可参考我的上一篇文章。这个目录结构不是固定的,你可以根据你的习惯创建目录,下面贴出来这些类或接口的代码:

1、Student(pojo包)

package com.books.pojo;

public class Student {
    private int sID;
    private String sName;
    private int classId;

    public int getsID() {
        return sID;
    }

    public void setsID(int sID) {
        this.sID = sID;
    }

    public String getsName() {
        return sName;
    }

    public void setsName(String sName) {
        this.sName = sName;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }
}

2、StudentDao(dao包)

package com.books.dao;



import com.books.pojo.Student;

import java.util.List;


public interface StudentDao {
    public List<Student> getAllStudent();
}

3、StudentMapper(mapper包)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.books.dao.StudentDao">
    <select id="getAllStudent" resultType="Student">
        select * from Student
    </select>
</mapper>

4、StudentService(service包)

package com.books.service;

import com.books.pojo.Student;

import java.util.List;

public interface StudentService {
    public List<Student> getAllStudent();
}

5、StudentServiceImpl(serviceImpl包)

package com.books.serviceImpl;

import com.books.dao.StudentDao;
import com.books.pojo.Student;
import com.books.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

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

@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    @Qualifier("studentDao")
    private StudentDao studentDao;

    @Override
    public List<Student> getAllStudent() {
        return studentDao.getAllStudent();
    }
}

6、StudentController(controller包)

package com.books.controller;

import com.books.pojo.Student;
import com.books.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

@Controller
public class StudentController {
    @Resource(name = "studentService")
    private StudentService studentService;

    @RequestMapping("/student")
    public String goIndex(Model model){
        List<Student> studentList=studentService.getAllStudent();
        model.addAttribute("list",studentList);
        return "Student";
    }
}

7、Student.jsp(webapp/jsp目录下)

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<c:forEach items="${list}" var="student">
    <c:out value="${student.sName}" />
    <br>
</c:forEach>
</body>
</html>

注意不要忘了在IDEA中引入DTD,在settings中设置,这一点可自行搜索资料。

第三步:修改pom.xml

添加MySQL的驱动包依赖:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.13</version>
</dependency>

然后在<build>标签里加入以下内容,否则待会扫描不到我们的SQL映射文件:

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

第四步:修改application.properties文件

server.port=8088
#返回的前缀   目录对应src/main/webapp下
spring.mvc.view.prefix=/jsp/
#返回的后缀
spring.mvc.view.suffix=.jsp

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ShopOrder?useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=accp
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5


mybatis.type-aliases-package=com/books/pojo
mybatis.mapperLocations=classpath*:com/books/mapper/*.xml

第五步:修改启动类xxxApplication

加上@MapperScan这个注解,然后指定你的XML所存放的文件夹,否则不会进行注册:

package com.books;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication() //开启组件扫描和自动配置
@MapperScan("com.books.dao")
public class RunApplication {

    public static void main(String[] args) {
        //负责启动引导应用程序
        SpringApplication.run(RunApplication.class, args);
    }
}

第六步:部署运行

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值