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);
}
}
第一步导入依赖的 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);
}
}