首先在mapper下面新建一个mysql.xml
mysql.xml
<?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.course"> <!--id必须为唯一,因为执行的时候是匹配的id--> <select id="getUserCount" resultType="Integer"> <!--resultType是执行结果返回的数据类型,返回一个用户数--> select count(1) from user; </select> <insert id="addUser" parameterType="com.course.model.User"> <!--parameterType是传入参数的数据类型 这是插入操作--> insert into user (name,age,sex) values (#{name},#{age},#{sex}) </insert> <update id="updateUser" parameterType="com.course.model.User"> <!--这是更新语句--> update user set name = #{name}, age=#{age} where id = #{id} </update> <delete id="deleteUser" parameterType="Integer"> <--这是删除语句--> delete from user where id = #{id} </delete> </mapper>
要准备好一个数据库,准备一个user 表
新建一个controller的文件夹,写入一个demo类,然后还要写一个启动类application。以下是目录结构。
Application.java
package com.course; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.scheduling.annotation.EnableScheduling; import javax.annotation.PreDestroy; @EnableScheduling @SpringBootApplication public class Application { private static ConfigurableApplicationContext context; public static void main(String[] args){ Application.context = SpringApplication.run(Application.class,args); } @PreDestroy public void close(){ Application.context.close(); } }
Demo.java
package com.course.controller; import com.course.model.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.log4j.Log4j; import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @Slf4j @Log4j @RestController @Api(value ="v1",description = "这是我的第一个版本的demo") @RequestMapping("v1") public class Demo { //获取一个执行sql语句的对象,里面会有增删改查的4个模版 @Autowired //下面的对象会被加载 private SqlSessionTemplate template; @RequestMapping(value = "/getUserCount",method = RequestMethod.GET) @ApiOperation(value = "可以获取到用户数") public int getUserCount(){ return template.selectOne("getUserCount"); } @RequestMapping(value = "/addUser",method = RequestMethod.POST) @ApiOperation(value = "插入用户") public int addUser(@RequestBody User user){ int result= template.insert("addUser",user); return result; } @RequestMapping(value = "/updateUser",method = RequestMethod.POST) @ApiOperation(value = "更新用户") public int updateUser(@RequestBody User user){ return template.update("updateUser",user); } @RequestMapping(value = "/deleteUser",method = RequestMethod.POST) @ApiOperation(value = "删除用户") public int deleteUser(@RequestParam int id){ return template.delete("deleteUser",id); } }
然后,运行Application启动类来启动服务,在浏览器输入地址看查询的结果 http://localhost:8888/v1/getUserCount.结果显示8,上面的表里面确实是8条数据。
插入的话,需要用到另一个类model下新建User.java model要和数据库中的字段相对应
package com.course.model; import lombok.Data; @Data public class User { private int age; private int id; private String name; private String sex; }
在jmeter中查看插入的结果:
运行以后查看结果树的结果:响应数据是1,证明添加了一条成功了
打开数据库表刷新看到多了一条:
更新:
删除: