springboot整合mybatis

本文详细介绍了如何在Spring Boot项目中整合MyBatis,包括配置依赖、创建DAO层、Service层、Impl层和Controller层,以及使用Postman进行测试的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入依赖

在pom文件引入依赖:

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

		<!-- JDBC依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<!-- 连接类及连接池 -->	
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.29</version>
		</dependency>
		<!-- mybatis依赖包 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

 

dao层

package com.xiami.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xiami.entity.MyLove;

@Mapper
public interface MyLoveMapper
{
	@Insert("insert into myLove(name,age,sex) values (#{name},#{age},#{sex})")
	int add(@Param("name") String name,@Param("age") String age,
			@Param("sex") String sex);
	@Update("update myLove set name = #{name}, age = #{age}, sex= #{sex} where id = #{id}")
	int update(@Param("name") String name,@Param("age") String age,
			@Param("sex") String sex, @Param("id") int id);
	

    @Delete("delete from myLove where id = #{id}")
    int delete(int id);

    @Select("select name, sex, age from myLove where id = #{id}")
    MyLove findMlove(@Param("id") int id);

    @Select("select name, sex, age from myLove")
    List<MyLove> findMyLoveList();

}

 

service层

package com.xiami.service;


import java.util.List;
import com.xiami.entity.MyLove;

public interface MyLoveMapperService {
    public int add(String name, String sex, String age);
    
    public int update(String name, String sex, String age, int id);
    
    public int delete(int id);
    
    public MyLove findMyLove(int id);
    
    public List<MyLove> findMyLoveList();
}

 

Impl层

package com.xiami.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiami.dao.MyLoveMapper;
import com.xiami.entity.MyLove;

@Service
public class MyLoveMapperServiceImpl implements MyLoveMapperService {

	@Autowired
	private MyLoveMapper myLoveMapper;
	@Override
	public int add(String name, String sex, String age) {
		return myLoveMapper.add(name, age, sex);
	}

	@Override
	public int update(String name, String sex, String age, int id) {
		return myLoveMapper.update(name, age, sex, id);
	}

	@Override
	public int delete(int id) {
		return myLoveMapper.delete(id);
	}

	@Override
	public MyLove findMyLove(int id) {
		return myLoveMapper.findMlove(id);
	}

	@Override
	public List<MyLove> findMyLoveList() {
		return myLoveMapper.findMyLoveList();
	}

}

 

controller层

package com.xiami.myLoveController;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xiami.entity.MyLove;
import com.xiami.service.MyLoveMapperService;

public class MyLoveMapperController {

  	@Autowired
    MyLoveMapperService myLoveMapperService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public List<MyLove> POSTAccounts() {
        return myLoveMapperService.findMyLoveList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public MyLove POSTAccountById(@PathVariable("id") int id) {
        return myLoveMapperService.findMyLove(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name") String name,
    @RequestParam(value = "age") String age,@RequestParam(value = "sex") String sex) 
    {
        int t= myLoveMapperService.update(name, sex, age, id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public String delete(@PathVariable(value = "id")int id) {
        int t= myLoveMapperService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
	@RequestParam(value = "age") String age,@RequestParam(value = "sex") String sex) {

       int t= myLoveMapperService.add(name, sex, age);
       if(t==1) {
           return "success";
       }else {
           return "fail";
       }

    }
}

 

通过postman完成测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值