SpringBoot整合Mybatis完成增刪改查功能
阿新 • • 發佈:2019-01-27
2.建立一個專案:
① new--->Spring Stater project
②
③:搜尋需要的依賴包
④:我們發現上一步選擇的依賴,在pom.xml檔案中 已經自動新增到我們的檔案中了。
2. 開始編寫程式碼和一些配置
先看一下我的專案的最後目錄結構:
①:自己建立一個數據表 欄位為id , name, password ,number 四個欄位 id為int型別,其他的都為String型別。
在src/main/java目錄下新增controller層,entity層,mapper層,service層
entity層的實體類User:
package com.example.demo.entity; public class User { private int id; private String name; private String password; private String number; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + ", number=" + number + "]"; } }
mapper層的UserMapper類:
package com.example.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.example.demo.entity.User; @Mapper public interface UserMapper { List<User> findUserByName(String name); public List<User> ListUser(); public int insertUser(User user); public int delete(int id); public int Update(User user); }
service層的實現類Userservice:
controller層 的訪問類CRUD:package com.example.demo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> findByName(String name) { return userMapper.findUserByName(name); } public User insertUser(User user) { userMapper.insertUser(user); return user; } public List<User> ListUser(){ return userMapper.ListUser(); } public int Update(User user){ return userMapper.Update(user); } public int delete(int id){ return userMapper.delete(id); } }
package com.example.demo.controller;
import java.util.List;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
@RestController
@RequestMapping(value = "/CRUD", method = { RequestMethod.GET, RequestMethod.POST })
public class CRUD {
@Autowired
private UserService userservice;
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String delete(int id) {
int result = userservice.delete(id);
if (result >= 1) {
return "刪除成功";
} else {
return "刪除失敗";
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(User user) {
int result = userservice.Update(user);
if (result >= 1) {
return "修改成功";
} else {
return "修改失敗";
}
}
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public User insert(User user) {
return userservice.insertUser(user);
}
@RequestMapping("/ListUser")
@ResponseBody
public List<User> ListUser(){
return userservice.ListUser();
}
@RequestMapping("/ListUserByname")
@ResponseBody
public List<User> ListUserByname(String name){
return userservice.findByName(name);
}
}
接著:
在src/main/resources下寫UserMapper的對映檔案xml.
UserMapper.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD com.example.Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="result" type="com.example.demo.entity.User">
<result property="name" column="name" />
<result property="password" column="password" />
<result property="number" column="number"/>
</resultMap>
<select id="ListUser" resultMap="result">
SELECT * FROM user
</select>
<select id="findUserByName" resultMap="result">
SELECT * FROM user where name=#{name}
</select>
<insert id="insertUser" parameterType="com.example.demo.entity.User"
keyProperty="id" useGeneratedKeys="true">
INSERT INTO user
(
id,name,password,number
)
VALUES (
#{id},
#{name, jdbcType=VARCHAR},
#{password, jdbcType=VARCHAR},
#{number}
)
</insert>
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
<update id="Update" parameterType="com.example.demo.entity.User">
update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
</update>
</mapper>
最後配置application.properties配置檔案:
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 11111111
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml
啟動程式的入口類:SpringbootMybatisApplication.java
我們在postmain這個工具中測試:
測試根據name查詢資料:
測試根據Id刪除資料:
結束!原始碼地址-->碼雲:原始碼地址