0. mysql 建表代码
DROP TABLE IF EXISTS ` user` ;
CREATE TABLE ` user` (
` id` bigint ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '序号' ,
` account` varchar ( 25 ) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '账号' ,
` password` varchar ( 50 ) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '密码' ,
` phone` varchar ( 30 ) COLLATE utf8mb4_unicode_ci DEFAULT null COMMENT '联系方式' ,
PRIMARY KEY ( ` id` ) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT= DYNAMIC COMMENT = '用户表' ;
INSERT INTO user ( account, password, phone) VALUES
( 'ken' , 'ken123' , '19977778888' ) ,
( 'jianghai' , 'jianghai123' , '19977778888' ) ;
SELECT * FROM user ;
TRUNCATE TABLE user ;
1.项目结构与详细代码
1.1 pom依赖
<?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.5</ version>
< relativePath/>
</ parent>
< groupId> com.wm</ groupId>
< artifactId> stums</ artifactId>
< version> 0.0.1-SNAPSHOT</ version>
< name> stums</ name>
< description> Demo project for Spring Boot</ description>
< properties>
< java.version> 1.8</ java.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-thymeleaf</ artifactId>
</ dependency>
< 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>
< version> 2.1.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< scope> runtime</ scope>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-jdbc</ artifactId>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< configuration>
< excludes>
< exclude>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ exclude>
</ excludes>
</ configuration>
</ plugin>
</ plugins>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.xml</ include>
</ includes>
< filtering> true</ filtering>
</ resource>
</ resources>
</ build>
</ project>
1.2 application.properties
server. port= 8999
#连接数据库
spring. datasource. username= xxxxx
spring. datasource. password= xxxxx
spring. datasource. driver- class - name= com. mysql. cj. jdbc. Driver
spring. datasource. url= jdbc: mysql: / / localhost: 3306 / stums? serverTimezone= UTC & useUnicode= true & characterEncoding= utf- 8
#配置mybatis
mybatis. mapper- locations= classpath: mapper
1.3 UserController
package com. wm. stums. controller ;
import com. wm. stums. entity. User ;
import com. wm. stums. mapper. UserMapper ;
import org. apache. ibatis. annotations. Param ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. util. List ;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping ( "/queryUserList" )
public List queryUserList ( ) {
List < User > users = userMapper. queryUserList ( ) ;
return users;
}
@RequestMapping ( "/queryUserByAccount" )
public User queryUserByAccount ( @Param ( "account" ) String account) {
User user = userMapper. queryUserByAccount ( account) ;
if ( user== null ) {
User userNull = new User ( ) ;
userNull. setAccount ( "该用户不存在!" ) ;
return userNull;
}
return user;
}
@RequestMapping ( "/addUser" )
public boolean addUser ( String account, String password, String phone) {
System . out. println ( account+ "__" + password+ "__" + phone) ;
boolean addUser = userMapper. addUser ( account, password, phone) ;
return addUser;
}
@RequestMapping ( "/updateUser" )
public boolean updateUser ( String id, String account, String password, String phone) {
System . out. println ( id+ "__" + account+ "__" + password+ "__" + phone) ;
boolean updateUser = userMapper. updateUser ( id, account, password, phone) ;
return updateUser;
}
@RequestMapping ( "/deleteUser" )
public boolean deleteUser ( String id) {
System . out. println ( "id:" + id) ;
boolean deleteUser = userMapper. deleteUser ( id) ;
return deleteUser;
}
}
1.4 User 实体类 (用了lombok插件,省略了get/set方法
package com. wm. stums. entity ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import java. io. Serializable ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String id;
private String account;
private String password;
private String phone;
@Override
public String toString ( ) {
return "User{" +
"id='" + id + '\'' +
", account='" + account + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
'}' ;
}
}
1.5 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.wm.stums.mapper.UserMapper" >
< select id = " queryUserList" resultType = " com.wm.stums.entity.User" >
select * from user
</ select>
< select id = " queryUserByAccount" resultType = " com.wm.stums.entity.User" >
select * from user
where account=#{param1}
</ select>
< insert id = " addUser" >
insert into user (account, password, phone)
VALUES (#{param1},#{param2},#{param3})
</ insert>
< update id = " updateUser" >
update user
set account = #{param2},password=#{param3},phone=#{param4}
where id = #{param1}
</ update>
< delete id = " deleteUser" >
delete from user where id = #{param1}
</ delete>
</ mapper>
1.6 UserMapper
package com. wm. stums. mapper ;
import com. wm. stums. entity. User ;
import org. apache. ibatis. annotations. Mapper ;
import org. springframework. stereotype. Repository ;
import java. util. List ;
@Mapper
@Repository
public interface UserMapper {
List < User > queryUserList ( ) ;
User queryUserByAccount ( String account) ;
boolean addUser ( String account, String password, String phone) ;
boolean updateUser ( String id, String account, String password, String phone) ;
boolean deleteUser ( String id) ;
}
参考博客
SpringBoot集成Mybatis这一篇就够了!