码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot中集成Mybaties

时间:2020-03-17 15:38:25      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:map   服务层   不用   framework   factor   actor   art   repos   getname   

1、首先加入依赖

    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2、再application.properties中加入如下内容

spring.datasource.url=jdbc:mysql://localhost:3307/demo?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


mybatis.mapper-locations=classpath:mapper/*.xml

数据库配置请根据自己的情况进行修改,最后一个配置是mapper xml文件的配置路径

3、数据库内容如下

技术图片

 

 

4、bean层,对于数据库中的student

package com.example.demo.bean;

public class Student {

    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

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

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

    public void setPassword(String password) {
        this.password = password;
    }
}

 

5、controller层

package com.example.demo.controller;

import com.example.demo.bean.Student;
import com.example.demo.server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
public class StudentController {

    @Autowired
    StudentServer studentServer;

    @ResponseBody
    @RequestMapping(value = "getName",method = GET,produces = "application/json;charset=utf-8")
    public String getName(){
        Student student = null;
        student = studentServer.findStudentById(1);
        if(student!=null){
            return student.getName();
        }else{
            return "无";
        }
    }
}

 

方法1 、 使用带xml文件的

在Resources新建mapper文件夹,在建立一个student.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.example.demo.dao.StudentDao">
    <select id="findStudentById" parameterType="int" resultType="com.example.demo.bean.Student">
        select * from student where id = #{id}
    </select>
</mapper>

在dao层,可以使用@Respository+@MapperScan 或者直接使用@Mapper

@Repository
public interface StudentDao {

    public Student findStudentById(int id);

}

方法2,不用xml文件,dao层

package com.example.demo.dao;

import com.example.demo.bean.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Repository;

@Mapper
public interface StudentDao2 {

    @Select("select * from student where id = #{id}")
    public Student findStudentById(int id);
}

 

 

服务层:(这里写了三种情况,

              studentDao是使用@Respository+@MapperScan ,

              studentDao1是使用@Mapper进行测试的

              studenDao2是不用xml文件进行测试的   )

package com.example.demo.server.impl;

import com.example.demo.bean.Student;
import com.example.demo.dao.StudentDao;
import com.example.demo.dao.StudentDao1;
import com.example.demo.dao.StudentDao2;
import com.example.demo.server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServerImpl implements StudentServer {

    @Autowired
    //StudentDao studentDao;
    //StudentDao1 studentDao1;  //虽然会提示有错,但是可以正常运行,有时间再看具体原因
    StudentDao2 studentDao2;



    @Override
    public Student findStudentById(int id) {
        Student student = null;
        /*使用@Respository+@MapperScan*/
        //student = studentDao.findStudentById(id);
        //student = studentDao1.findStudentById(id);

        student = studentDao2.findStudentById(id);
        return student;
    }
}

这三个最后都能显示正确的答案

     技术图片

 

 

持续更新中!!!

 

Spring Boot中集成Mybaties

标签:map   服务层   不用   framework   factor   actor   art   repos   getname   

原文地址:https://www.cnblogs.com/minblog/p/12510922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!