Springboot整合mybatis实现数据的增删查改
一、工具:
IDEA
Mysql
二、具体实现
新建一个springboot工程
File->new->project
点击springinitial->Next
填写Group和Artifact点击下一步
添加对应的技术,选中之后会自动添加对应的依赖,点击Next
点击fish
项目结构
工程创建成功之后开始创建依赖,在pom.xml下添加依赖,修改依赖
修改配置文件resources->application.properties为application.yml,并且添加如下内容。.properties与.yml的区别参考: https://blog.csdn.net/u013551585/article/details/80196052
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mydb?serverTimezone=GMT%2B8
username: root
password: qwer1234
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath*:mapper/*.xml
mydb为数据库的名称,username为用户名,classpath*:mapper为resources下的mapper文件夹,在新版本中需要添加serverTimezone=GMT%2B8,否则会报错
创建相应的包和文件
创建User实体类
创建User.java实体类
package com.sml.mybatisdemo.domin;
public class User {
public String id;
public String userName;
public String age;
public String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age)
{
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建service层
创建UserService.java接口,包含增删查改的接口
package com.sml.mybatisdemo.service;
import com.sml.mybatisdemo.domin.User;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface UserService {
List<User> getAllUser();
void insert(User user);
void update(User user);
void delete(String id);
}
创建dao层接口
创建UserDao.java接口
package com.sml.mybatisdemo.dao;
import com.sml.mybatisdemo.domin.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("customBlackListDao")
public interface UserDao {
List<User> getAllUser();
void insert(User user);
void update(User user);
void delete(String id);
}
创建控制类UserController.java
package com.sml.mybatisdemo.controller;
import com.sml.mybatisdemo.domin.User;
import com.sml.mybatisdemo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class UserController {
@Resource
UserService userService;
@GetMapping(value = "/getAllUser")
public List<User> getAllUser(){
return userService.getAllUser();
}
@PostMapping(value = "/insert")
public void insert(@Param("id") String id,
@Param("userName") String userName,
@Param("age") String age,
@Param("sex") String sex){
User user = new User();
user.setId(id);
user.setUserName(userName);
user.setAge(age);
user.setSex(sex);
userService.insert(user);
}
@GetMapping(value = "/delete")
public void delete(String id) {
userService.delete(id);
}
@PostMapping(value = "/update")
public void update(User user) {
userService.update(user);
}
}
创建mapper层
创建实现类UserServicelmpl.java
package com.sml.mybatisdemo.mapper;
import com.sml.mybatisdemo.dao.UserDao;
import com.sml.mybatisdemo.domin.User;
import com.sml.mybatisdemo.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("customBlackListService")
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public List<User> getAllUser() {
return userDao.getAllUser();
}
@Override
public void insert(User user) {
userDao.insert(user);
}
@Override
public void delete(String id) {
userDao.delete(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
在启动类添加注解
Resources下创建UserMapper.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.sml.mybatisdemo.dao.UserDao">
<select id="getAllUser" resultType="com.sml.mybatisdemo.domin.User" >
SELECT * FROM userinfo;
</select>
<insert id="insert" parameterType="com.sml.mybatisdemo.domin.User" useGeneratedKeys="true" keyProperty="userId">
insert into userinfo(id,userName,age,sex) values (#{id},#{userName},#{age},#{sex})
</insert>
<delete id="delete" parameterType="com.sml.mybatisdemo.domin.User">
delete from userinfo where id=#{id}
</delete>
<update id="update" parameterType="com.sml.mybatisdemo.domin.User">
update userinfo set userinfo.userName=#{userName},userinfo.age=#{age},userinfo.sex=#{sex} where userinfo.id=#{id}
</update>
</mapper>
数据表如下
接下来我们用微信小程序来验证一下是否成功
三、验证
新建一个微信小程序
Index.js如下
// pages/identify/identify.js
var app = getApp();
// var myUtils = require("…/…/utils/myUtils.js");
Page({
/**
},
/**
},
/**
},
/**
},
/**
},
/**
},
/**
},
/**
},
/**
},
formSubmit: function (res) {
wx.request({
url: ‘http://localhost:8080/insert’,
method: ‘POST’,
data: {
id:‘2’,
userName:‘小王’,
age:‘12’,
sex:‘男’
},
header: {
‘content-type’: ’ application/x-www-form-urlencoded’
},
success: function (res) {
console.log(res)
},
fail:function(res){
console.log(‘res’)
}
})
}
})
Index.wxm如下
Index.wxml只是一个装饰,只有按钮起到作用,数据被写死在了程序里面
把insert位置分别更换为getAllUser、uatate和delete来验证增删改查功能是否实现
若出现上图所示,则表示已经成功了
注:小程序端若请求方式为POST,需要改变请求头为:
header: {
‘content-type’: ’ application/x-www-form-urlencoded’
},
否则请求的数据为空
如上,我们的增删改查功能就实现啦