在上一篇博客中我们已经学会了利用springboot+MyBits构建一个基础的项目,实现查询信息的操作;本篇博客将继续带领大家实现剩下的增,删,改三个基础的操作。
背景:对一个叫做gate_test的表(id,name)做增删改查,其中id是自增主键。
1.首先我们需要创建一个springboot项目,在pom.xml文件中引入相关依赖,并配置扫描实体类和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.GateTestExame"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
基础知识,仅做记录便于回忆,方便初学者