SpringBoot的出現減輕了開發者的配置工作量,而mybatis的出現又大大減少了我們隊數據庫操作的代碼量。
看一下MyBatis 的官方定義:
MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。
下面我們就來做一個小DEMO,實現CRUD:
背景:對一個叫做gate_test的表(id,name)做增刪改查,其中id是自增主鍵。
1.引用mybatis包,並配置掃描實體類和mapper.xml的位置。
我們在application.properties中配置:

2.創建gateTest實體:

3.創建相應的接口和接口實現類:

紅框中就是增刪改查的方法,具體實現在GateTestServiceImpl.java文件里面:
@Override
public void insertGateTestService() {
GateTest gateTest = new GateTest();
int val = (int)(Math.random()*100+1);
String name = "zhangsan"+val;
gateTest.setName(name);
gateTestMapper.insert(gateTest);
}
@Override
public void deleteGateTestService(int id) {
gateTestMapper.deleteByPrimaryKey(id);
}
@Override
public void updateGateTestService(int id,String name) {
GateTest gateTest = new GateTest();
gateTest.setId(id);
gateTest.setName(name);
gateTestMapper.updateByPrimaryKey(gateTest);
}
@Override
public GateTest getTestDateService(int id) {
GateTest gateTest = gateTestMapper.selectByPrimaryKey(id);
return gateTest;
}
4,增加GateTestMapper接口類:(這里的方法就和GateTestMapper.xml中的id保持一致)
package com.test.base.dao;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import com.test.base.entity.BaseExample;
import com.test.base.entity.GateTest;
import com.test.base.entity.GateTestExample;
@Mapper
public interface GateTestMapper extends BaseDao<GateTest, BaseExample>{
List<GateTest> directQuery(@Param("sql")String sql);
int deleteByPrimaryKey(Integer id);
int insert(GateTest record);
int insertSelective(GateTest record);
GateTest selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(GateTest record);
int updateByPrimaryKey(GateTest record);
}
5,增加GateTestMapper.xml,注意id和上面GateTestMapper.java的方法名保持一致
<?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.test.base.dao.GateTestMapper" >
<resultMap id="BaseResultMap" type="com.test.base.entity.GateTest" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from gate_test
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from gate_test
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.test.base.entity.GateTest" >
insert into gate_test values (#{id}, #{name,jdbcType=VARCHAR})
</insert>
<select id="countByExample" parameterType="com.test.base.entity.GateTestExample" resultType="java.lang.Integer" >
select count(*) from gate_test
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByPrimaryKey" parameterType="com.test.base.entity.GateTest" >
update gate_test
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
注意:一定要將**mapper.xml放在指定配置的目錄中,否則掃描不到。
6.我們在數據庫中創建gate_test表,包括兩個字段int id,varchar name,關於數據庫的配置也在application.properties中:

7.寫控制器測試代碼GateTestController.java:
package com.test.base.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.test.base.entity.GateTest;
import com.test.base.service.GateTestService;
@Controller
public class GateTestController {
@Autowired
private GateTestService gateTestService;
@RequestMapping("/insertGateTest")
@ResponseBody
public String insertGateTest(Model model, HttpServletRequest request, String typeCode) {
gateTestService.insertGateTestService();
return "ok";
}
@RequestMapping(value="/getGateTest/{id}",method= RequestMethod.GET)
@ResponseBody
public Object getGateTest(@PathVariable("id") int id) {
GateTest gateTest = new GateTest();
gateTest = gateTestService.getTestDateService(id);
return JSON.toJSON(gateTest);
}
@RequestMapping(value="/updateGateTest/{id}/{name}",method= RequestMethod.GET)
@ResponseBody
public Object updateGateTest(@PathVariable("id") int id,@PathVariable("name") String name) {
gateTestService.updateGateTestService(id,name);
return "update id= "+id+" successful";
}
@RequestMapping(value="/deleteGateTest/{id}",method= RequestMethod.GET)
@ResponseBody
public Object deleteGateTest(@PathVariable("id") int id) {
gateTestService.deleteGateTestService(id);
return "delete id= "+id+" successful";
}
}
8.到此為止,完成了所有配置代碼,可以測試一下:
增加:http://127.0.0.1:8082/insertGateTest
刪除id為6的數據:http://127.0.0.1:8082/deleteGateTest/6
修改id為3的name為“ceshi”: http://127.0.0.1:8082/updateGateTest/3/ceshi
查看id為4的數據:http://127.0.0.1:8082/getGateTest/4
基礎知識,僅做記錄便於回憶,方便初學者
