springboot整合mybatis

Posted wn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合mybatis相关的知识,希望对你有一定的参考价值。

创建一个工程

    

导入依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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-jdbc</artifactId>
        </dependency>

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

创建entity实体层

package com.wn.springbootproject04.entity;

import java.io.Serializable;

public class Petbreed implements Serializable {

    private Integer petId;
    private String petBreed;

    public Petbreed(Integer petId, String petBreed) {
        this.petId = petId;
        this.petBreed = petBreed;
    }

    public Petbreed(String petBreed) {
        this.petBreed = petBreed;
    }

    public Petbreed() {
    }

    public Integer getPetId() {
        return petId;
    }

    public void setPetId(Integer petId) {
        this.petId = petId;
    }

    public String getPetBreed() {
        return petBreed;
    }

    public void setPetBreed(String petBreed) {
        this.petBreed = petBreed;
    }
}

编写application.properties文件

#配置数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///petmanage
spring.datasource.username=root
spring.datasource.password=123

#配置端口 server.port
=8004
#映射xml文件 mybatis.mapper
-locations=classpath:/mapper/*.xml

创建dao接口层

@Repository
public interface PetbreedDao {

    //查询
    public List<Petbreed> getAll();

    //新增
    public Integer getinsert(Petbreed petbreed);

    //修改
    public Integer getupdate(Petbreed petbreed);

    //删除
    public Integer getdel(@Param("id") Integer id);

}

创建dao.xml层

<?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指定Dao接口的完整类名
mybatis会依据这个接口动态创建一个实现类去实现这个接口,
而这个实现类是一个Mapper对象-->

<mapper namespace="com.wn.springbootproject04.dao.PetbreedDao">
    <resultMap id="MapList" type="com.wn.springbootproject04.entity.Petbreed">
        <id property="perId" column="petId"></id>
        <result property="perBreed" column="petBreed"></result>
    </resultMap>

    <!--查询全部-->
    <select id="getAll" resultType="com.wn.springbootproject04.entity.Petbreed">
        SELECT * FROM petbreed
    </select>

    <!--新增-->
    <insert id="getinsert">
        INSERT INTO petbreed(petBreed) VALUE(#{petBreed})
    </insert>

    <!--修改-->
    <update id="getupdate">
        UPDATE petbreed SET petBreed=#{petBreed} WHERE petId=#{petId}
    </update>

    <!--删除-->
    <delete id="getdel">
        DELETE FROM petbreed WHERE petId=#{id}
    </delete>

</mapper>

创建service接口层

public interface PetbreedService {

    //查询
    public List<Petbreed> getAll();

    //新增
    public Integer getinsert(Petbreed petbreed);

    //修改
    public Integer getupdate(Petbreed petbreed);

    //删除
    public Integer getdel(@Param("id") Integer id);

}

创建service接口实现层

@Service("petService")
public class PetbreedServiceImpl implements PetbreedService {

    //植入dao层对象
    @Resource
    private PetbreedDao dao;

    @Override
    public List<Petbreed> getAll() {
        return dao.getAll();
    }

    @Override
    public Integer getinsert(Petbreed petbreed) {
        return dao.getinsert(petbreed);
    }

    @Override
    public Integer getupdate(Petbreed petbreed) {
        return dao.getupdate(petbreed);
    }

    @Override
    public Integer getdel(Integer id) {
        return dao.getdel(id);
    }
}

创建controller层

@Controller
@RequestMapping("/breed")
public class PetbreedController {

    //植入service层对象
    @Resource(name = "petService")
    private PetbreedService service;

    //查询
    @RequestMapping("/getAll")
    @ResponseBody
    public List<Petbreed> getAll(){
        List<Petbreed> all = service.getAll();
        return all;
    }

    //新增
    @RequestMapping("/getinsert")
    @ResponseBody
    public Integer getinsert(){
        Integer getinster = service.getinsert(new Petbreed("hhh"));
        return getinster;
    }

    //修改
    @RequestMapping("/getupdate")
    @ResponseBody
    public Integer getupdate(){
        Integer getupdate = service.getupdate(new Petbreed(9, "小猫"));
        return getupdate;
    }

    //删除
    @RequestMapping("/getdel")
    @ResponseBody
    public Integer getdel(){
        Integer getdel = service.getdel(9);
        return getdel;
    }

}

实现结果

  查询:

    

  新增:

    

 

     新增后的数据库,新增了id为11的数据;

 

 

 

     

  修改:

    

 

      修改后的数据库,id为9的数据修改为小猫

 

 

     

  删除:

    

 

      删除后的数据库,id为9的数据没有了

 

 

     

以上是关于springboot整合mybatis的主要内容,如果未能解决你的问题,请参考以下文章

springboot整合mybatis,redis,代码

SpringBoot系列Mybatis整合

SpringBoot系列Mybatis整合

springboot整合mybatis开发

18.springboot整合mybatis

SpringBoot:4.SpringBoot整合Mybatis实现数据库访问