IDE:idea、DB:mysql
-
新建一个Spring Initializr项目
- 创建项目的文件结构以及jdk的版本
-
- 选择项目所需要的依赖
- 修改项目名,finish完成
-
完成后的目录结构(
DemoApplication:启动入口函数;application.properties系统配置;pox.xml系统依赖)
- 来看下建好后的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.6.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <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.2.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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- pom.xml中的mysql 驱动版本需要与你的MySQL数据版本一致。<不然会报数据库连接错误>
- 修改配置文件(修改application.properties为application.yaml,并配置)
配置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); List<User> getAll(); }
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.example1.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example1.demo.entity.User"> <id column="id" javaType="INTEGER" property="id" /> <result column="userName" javaType="String" property="userName" /> <result column="passWord" javaType="String" property="passWord" /> <result column="realName" javaType="String" property="realName" /> </resultMap> <select id="Sel" parameterType="com.example1.demo.entity.User" resultMap="BaseResultMap"> select * from userTable where id = #{id} </select> <update id="Update" parameterType="com.example1.demo.entity.User"> update userTable set passWord = '123qwe' where id = #{id} </update> <insert id="Insert" parameterType="com.example1.demo.entity.User"> insert into userTable(userName,passWord,realName) value('admin12','56494126',#{realName}) </insert> <delete id="Delete" parameterType="com.example1.demo.entity.User"> delete from userTable where id = #{id} </delete> <select id="getAll" parameterType="com.example1.demo.entity.User" resultMap="BaseResultMap"> select * from userTable </select> </mapper>
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 List<User> getAll(){ 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 List<User> getAll(){ return userService.getAll(); } }
- 最终框架结构
完成以上,下面在启动类里加上注解用于给出需要扫描的mapper文件路径
- 最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getUser/1
- 测试成功,就这样基本框架就搭建成功了。