第二节 使用mybatis+SpringBoot完成第一个查询demo
1.mysql.xml文件配置
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2015-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.course">
<select id="getRecordListCount" resultType="Integer">
select COUNT(*) from userList_record
</select>
</mapper>
2.springBoot启动程序Application
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) {
SpringApplication.run(Application.class, args);
}
@PreDestroy
public void destroy() {
Application.context.close();
}
}
3.demo测试
package com.course.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(value ="v1", description ="这是一个和数据库关联的demo")
@RequestMapping("v1")
public class Demo {
//首先获取一个执行sql语句的对象
@Autowired
private SqlSessionTemplate template;
@RequestMapping(value ="/getRecordCount", method = RequestMethod.GET)
@ApiOperation(value ="获取record表的数量")
public int getRecordCount(){
return template.selectOne("getRecordListCount");
}
}
4.启动springBoot,然后访问:http://localhost:8080/v1/getRecordCount
5.出现的问题---启动springboot报错“Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.fasterxml.jackson.databind” 百度了一下说是jackson的版本与springBoot的版本不兼容,但是我pom里没有引入该包,于是把该包引入后再次启动好啦
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
第三节 使用mybatis实现添加数据和idea的debug操作
1.插入数据
@RequestMapping(value = "/insertUser", method = RequestMethod.POST)
@ApiOperation(value = "插入用户数据")
public int insertUser(@RequestBody User user){
return template.insert("addUser", user);
}
2.mysql mapper文件
<insert id="addUser" parameterType="com.course.module.User">
insert into user (id, name, age, sex)
values (#{id},#{name}, #{age}, #{sex})
</insert>
3.User对象
package com.course.module;
import lombok.Data;
@Data
public class User {
private String id;
private String name;
private String sex;
private String age;
}
4.重启springboot,访问"localhost:8080/vi/insertUser",最终插入成功一条数据
第四节 使用mybatis实现数据的更新和删除操作
1.mysql.xml
<update id="updateUser" parameterType="com.course.module.User">
update user set name = #{name}
where age = #{age} and id = #{id}
</update>
<delete id="deleteUser" parameterType="com,course.module.User">
delete from user where name = #{name}
</delete>
2.demo
@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
@ApiOperation(value = "更新用户数据")
public int updateUser(@RequestBody User user){
return template.update("updateUser", user);
}
@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
@ApiOperation(value = "删除用户数据")
public int deleteUser(@RequestParam String name){
return template.delete("deleteUser", name);
}