springboot整合mybatis

springboot 整合 mybatis


第一步导入依赖的 jar 包

<!-- 引入父类工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>

<!-- springboot 整合 mybytis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

第二步写 bean 就是普通 的  bean
package com.springboot_mybatis.bean;

public class Emp {

private Integer eid;
private String ename;
private String sex;

/get .. set 方法/
}

第三步写 mapper 接口
package com.springboot_mybatis.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.repository.query.Param;

import com.springboot_mybatis.bean.Emp;

public interface Empmapper {
//添加
@Insert(" insert into emp values(null,#{ename},#{sex}) ")
void saveEmp(Emp emp);
//删除
@Delete(" delete from emp where eid = #{eid} ")
void deleteEmp(@Param("eid")Integer eid);
//修改
@Update("update emp set ename = #{ename},sex=#{sex} where eid = #{eid}")
void updateEmp(Emp emp);
//查询
@Select("select * from emp")
List<Emp> selectEmp();
//根据 id 查询 Emp 数据
@Select(" select * from emp where eid = #{eid} ")
Emp getbyid(@Param("eid")Integer eid);


}


第四步 写 controller 控制层
package com.springboot_mybatis.controller;

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 com.springboot_mybatis.bean.Emp;
import com.springboot_mybatis.mapper.Empmapper;

@Controller
public class EmpController {

@Autowired
private Empmapper empmapper;

//添加 Emp
@RequestMapping("/saveEmp")
@ResponseBody
public String saveEmp(Emp emp){
empmapper.saveEmp(emp);
return "success";
}
//修改 Emp
@RequestMapping("/updateEmp")
@ResponseBody
public String updateEmp(Emp emp){
empmapper.updateEmp(emp);
return "success";
}
//删除 Emp
@RequestMapping("/deleteEmp")
@ResponseBody
public String deleteEmp(Integer eid){
empmapper.deleteEmp(eid);
return "success";
}

//根据 eid 查询
@RequestMapping("/getbyid")
@ResponseBody
public Emp getbyid(Integer eid){
System.err.println(eid);
return empmapper.getbyid(eid);
}

//查询所有的用户
@RequestMapping("/EmpList")
@ResponseBody
public List<Emp> EmpList(){
return empmapper.selectEmp();
}

}




第五步启动 springboot 整合 mybatis 工程
package com.springboot_mybatis.RunBoot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


//扫描 controller
@ComponentScan(basePackages={"com.springboot_mybatis.controller"})
//扫描 mapper 接口
@MapperScan(basePackages={"com.springboot_mybatis.mapper"})
@EnableAutoConfiguration
public class RunMybatis {
public static void main(String[] args) {
SpringApplication.run(RunMybatis.class, args);

}
}



























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值