IDE:idea、DB:mysql
新建一个Spring Initializr项目
创建项目的文件结构以及jdk的版本选择项目所需要的依赖 修改项目名,finish完成
完成后的目录结构(
DemoApplication:启动入口函数;application.properties系统配置;pox.xml系统依赖)来看下建好后的pom
pom.xml中的mysql 驱动版本需要与你的MySQL数据版本一致。<不然会报数据库连接错误>修改配置文件(修改application.properties为application.yaml,并配置)4.0.0 org.springframework.boot spring-boot-starter-parent2.6.3 com.example demo0.0.1-SNAPSHOT demo Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.2.1 mysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
配置application.yaml
然后开始创建实体类实现业务流程
创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。
然后浏览器中出现以下:http://localhost:8080/test/index
表示spring boot 基本配置成功。可以开始业务的编写了。
数据库表结构
CREATE TABLE `usertable` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',
`userName` varchar(32) NOT NULL COMMENT '姓名',
`passWord` varchar(100) NOT NULL COMMENT '密码',
`realName` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb3
建好表,插入2条数据。
项目的目录结构:
User.java
package com.example1.demo.entity; import java.io.Serializable; public class User implements Serializable { private Integer id; private String userName; private String passWord; private String realName; public User() { } public User(Integer id, String userName, String passWord, String realName) { this.id = id; this.userName = userName; this.passWord = passWord; this.realName = realName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + ''' + ", passWord='" + passWord + ''' + ", realName='" + realName + ''' + '}'; } }
UserMapper.java
package com.example1.demo.mapper; import com.example1.demo.entity.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserMapper { User Sel(int id); int Update(int id); int Insert(String realName); int Delete(int id); ListgetAll(); }
UserMapper.xml
UserService.java
import com.example1.demo.entity.User; import com.example1.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public User Sel(int id){ return userMapper.Sel(id); } public int Update(int id){ return userMapper.Update(id); } public int Insert(String realName){ return userMapper.Insert(realName); } public int Delete(int id){ return userMapper.Delete(id); } public ListgetAll(){ return userMapper.getAll(); } }
UserController.java
package com.example1.demo.controller; import com.example1.demo.entity.User; import com.example1.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/testBoot") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser/{id}") public String GetUser(@PathVariable int id){ return userService.Sel(id).toString(); } @RequestMapping("/update/{id}") public int Update(@PathVariable int id){ return userService.Update(id); } @RequestMapping("/insert/{realName}") public int Insert(@PathVariable String realName){ return userService.Insert(realName); } @RequestMapping("/delete/{id}") public int Delete(@PathVariable int id){ return userService.Delete(id); } @RequestMapping("/getAll") public ListgetAll(){ return userService.getAll(); } }
最终框架结构
完成以上,下面在启动类里加上注解用于给出需要扫描的mapper文件路径
最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getUser/1测试成功,就这样基本框架就搭建成功了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)