1、maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>MybatisDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MybatisDemo</name>
<description>MybatisDemo</description>
<properties>
<java.version>1.8</java.version>
<mybatis.springboot.version>1.3.2</mybatis.springboot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
2、架构代码
3、mapper.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.example.mybatisdemo.service.UserMapper">
<select id="queryAllUser" resultType="User">
select * from User
</select>
<select id="getUser" resultType="User">
SELECT id,name,password,token FROM User WHERE id=#{id}
</select>
</mapper>
4、application.yml
spring:
application:
name: mybatis-server
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8
username: root
password: root
server:
port: 8081
mybatis:
type-aliases-package: com.example.mybatisdemo.entity
mapper-locations:
- classpath:mapper/*.xml
5、user
package com.example.mybatisdemo.entity;
public class User {
private String id;
private String name;
private String password;
private String token;
public String getId() {
return id;
}
public void setId(String 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 getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
6、UserController
package com.example.mybatisdemo.controller;
import com.example.mybatisdemo.entity.User;
import com.example.mybatisdemo.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService service;
@GetMapping("demo")
public List<User> queryAll(){
List<User> list = service.queryAllUser();
return list;
}
@GetMapping("getid")
public List<User> getUser(int id){
return service.getUser(id);
}
@PostMapping("addUser")
public User addUser(User user){
service.addUser(user);
return user;
}
@GetMapping("deleteUser")
public String deleteUser(String userName){
service.deleteUser(userName);
return userName;
}
@PutMapping("/updateUser")
public User UserName(User user){
User user1 = service.updateUser(user);
return user;
}
}
7、interface UserMapper
package com.example.mybatisdemo.service;
import com.example.mybatisdemo.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> queryAllUser();
List<User> getUser(int id);
@Insert("insert into user(name,password,token) values (#{name},#{password},#{token})")
@Options(useGeneratedKeys = true,keyProperty = "id")
void addUser(User user);
@Delete("delete from user where name = #{userName}")
void deleteUser(String userName);
@Update("update user set password = #{password} where name = #{userName} ")
User updateUser(User user);
}
8、UserService implements
package com.example.mybatisdemo.service.impl;
import com.example.mybatisdemo.entity.User;
import com.example.mybatisdemo.service.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements UserMapper{
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryAllUser() {
return userMapper.queryAllUser();
}
@Override
public List<User> getUser(int id) {
return userMapper.getUser(id);
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void deleteUser(String userName) {
userMapper.deleteUser(userName);
}
@Override
public User updateUser(User user) {
userMapper.updateUser(user);
return (User) userMapper.getUser(Integer.parseInt(user.getId()));
}
}
9、待补充新增和更新接口。。。