<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
package com.example.demo.entity;
public class User {
private Integer id;
private String name;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.example.demo.mapper;
import com.example.demo.entity.Admin;
import org.apache.ibatis.annotations.Options;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
//@Mapper或者@MapperScan
public interface AdminMapper {
public List<Admin> getAdminList();
public Admin getAdminById(Integer id);
public int insertAdmin(Admin admin);
public int updateAdmin(Admin admin);
public Boolean deleteAdmin(Integer id);
}
mybatis全局配置文件 放在resource.mybatis,根据需要进行修改
具体配置查看https://mybatis.org/mybatis-3/zh/configuration.html#settings
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 开启驼峰命名自动映射 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 允许 JDBC 支持自动生成主键 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- MyBatis会返回一个空实例 -->
<setting name="returnInstanceForEmptyRow" value="true"/>
</settings>
</configuration>
<!-- SQL映射文件 放在resource.mybatis.mapper -->
<?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.example.demo.mapper.AdminMapper">
<!-- id对应mapper中的方法名 resultType只有在有返回值的时候才写,路径是实体类路径 -->
<select id="getAdminList" resultType="com.example.demo.entity.Admin">
select * from admin
</select>
<select id="getAdminById" resultType="com.example.demo.entity.Admin">
select * from admin where id = #{id}
</select>
<insert id="insertAdmin">
insert into admin(name,password) values (#{name},#{password})
</insert>
<update id="updateAdmin">
update admin set name = #{name},password = #{password} where id = #{id}
</update>
<delete id="deleteAdmin">
delete from admin where id = #{id}
</delete>
</mapper>
mybatis:
#指定mybatis全局配置文件路径
config-location: classpath:mybatis/mybatis-config.xml
#指定SQL映射文件路径
mapper-locations: classpath:mybatis/mapper/*.xml
package com.example.demo.controller;
import com.example.demo.entity.Admin;
import com.example.demo.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class AdminController {
@Autowired
AdminMapper adminMapper;
@GetMapping("/admin")
public List<Admin> getAdminList(){
return adminMapper.getAdminList();
}
@GetMapping("/admin/{id}")
public Admin getAdminById(@PathVariable("id") Integer id){
return adminMapper.getAdminById(id);
}
@GetMapping("/insert")
public Admin insertAdmin(Admin admin){
adminMapper.insertAdmin(admin);
return admin;
}
@GetMapping("/edit")
public Admin updateAdmin(Admin admin){
adminMapper.updateAdmin(admin);
return admin;
}
@GetMapping("/delete/{id}")
public Boolean deleteAdmin(@PathVariable("id") Integer id){
try {
adminMapper.deleteAdmin(id);
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
}