1.创建表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo练习</title>
</head>
<body>
<h1>用户新增表单</h1>
<form action="http://localhost:8090/addUser" method="post">
名称:<input type="text" name="name" /><br />
年龄:<input type="text" name="age" /><br />
性别:<input type="text" name="sex" /><br />
<input type="submit" value="新增" />
</form>
</body>
</html>
2.在Idea创建项目框架
2.1创建Maven项目
2.2编辑pom.xml,添加以下依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.3</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version>2.5.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.5.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加属性注入依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--引入插件lombok 自动的set/get/构造方法插件 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--支持热部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!--引入数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.48</version> </dependency> <!--springBoot数据库连接 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--spring整合mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> <version>2.5.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2.3配置核心文件application.xml
server: port: 8090 servlet: context-path: / spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true username: root password: 12345678 #mybatis-plush配置 mybatis-plus: type-aliases-package: com.jt.pojo mapper-locations: classpath:/mappers/*.xml configuration: map-underscore-to-camel-case: true #自动开启驼峰规则映射 logging: level: com.jt.mapper: debug
2.4控制器Controller
package com.jt.controller; import com.jt.pojo.User; import com.jt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private UserService userService; @PostMapping("/addUser") public String addUser(User user){ userService.addUser(user); return "增加用户成功"; } @GetMapping("/getUser") public List<User> getUser(){ List<User> userList = userService.getUser(); return userList; } }
2.5 mapper
package com.jt.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jt.pojo.User; public interface UserMapper extends BaseMapper<User> { }
2.6pojo
package com.jt.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.experimental.Accessors; import java.io.Serializable; @Data @Accessors(chain = true) @TableName("demo_user") public class User implements Serializable { @TableId(type = IdType.AUTO) private Integer id; private String name; private Integer age; private String sex; }
2.7service
package com.jt.service; import com.jt.mapper.UserMapper; import com.jt.pojo.User; import java.util.List; public interface UserService { void addUser(User user); List<User> getUser(); }
package com.jt.service; import com.jt.mapper.UserMapper; import com.jt.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServicelmpl implements UserService{ @Autowired private UserMapper userMapper; @Override public void addUser(User user) { userMapper.insert(user); } @Override public List<User> getUser() { return userMapper.selectList(null); } }
2.8启动类SpringBoot
package com.jt; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.jt.mapper") public class SpringBootRun { public static void main(String[] args) { SpringApplication.run(SpringBootRun.class, args); } }