1.1 Create New Project
1.2 選擇 Spring Initializr 建立器和選擇SDK版本
1.3 填寫組織、項目名稱和Java版本信息等
1.4 選擇須要的依賴
1.5 填寫項目、選擇存儲路徑,而後點擊finish
至此,項目已建立好。java
2.1 新建 controller.HelloController 控制器文件mysql
@RestController public class HelloController { @RequestMapping(value = "/hi", method = RequestMethod.GET) public String sayHello() { return "Hi buddy, How're you today ? "; } }
2.2 修改應用主入口文件MessageSystemApplication,註解添加(exclude = DataSourceAutoConfiguration.class)使其不須要配置數據庫。最後以下web
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class MessageSystemApplication { public static void main(String[] args) { SpringApplication.run(MessageSystemApplication.class, args); } }
2.3 啓動應用,而後在瀏覽器訪問:http://127.0.0.1:8080/hi,服務器相應並返回字符串,以下圖:spring
<repositories> <repository> <id>alimaven</id> <name>Maven Aliyun Mirror</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
4.1 pom.xml 添加依賴和配置sql
<?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.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.longmaster</groupId> <artifactId>message-system</artifactId> <version>0.0.1-SNAPSHOT</version> <name>message-system</name> <description>MessageSystemproject for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </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> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>cn/longmaster/messagesystem/mapper/xml/*.xml</include> </includes> </resource> </resources> </build> <!-- 配置阿里倉庫--> <repositories> <repository> <id>alimaven</id> <name>Maven Aliyun Mirror</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
4.2 配置數據庫鏈接
在resources目錄下新建 application.yml 配置文件,並進行數據庫配置以下數據庫
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/db_ms?useUnicode=true&&characterEncoding=utf-8&serverTimezone=UTC #注意設置編碼格式 #url username: root #用戶名 password: longmaster #密碼 driver-class-name: com.mysql.cj.jdbc.Driver #數據庫連接驅動 mybatis: mapper-locations: classpath:cn/longmaster/messagesystem/mapper/xml/*.xml # 注意:必定要對應mapper映射xml文件的所在路徑 type-aliases-package: cn.longmaster.messagesystem.entity # 注意:對應實體類的路徑
4.3 新建實體類 entity.Userapache
@Data public class User { private int user_id; private String job_number; private String name; private String password; private Date birthday; private int use_state; }
4.4 新建 mapper.UserMappersegmentfault
@Repository public interface UserMapper { /** * 新增職工信息 * @param user * @return */ int addUser(@Param("user") User user); }
4.5 新建 xml, mapper.xml.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="cn.longmaster.messagesystem.mapper.UserMapper"> <insert id="addUser" > INSERT INTO t_user(job_number, name, password, birthday, use_state) VALUES (#{user.job_number}, #{user.name}, #{user.password}, #{user.birthday}, 1) </insert> </mapper>
4.6 新建服務類 service.UserService服務器
@Service public class UserService { @Autowired UserMapper userMapper; public String addUser(User user) { if (userMapper.addUser(user) > 0) { return "Add Success"; } else { return "Add Fail"; } } }
4.7 控制器添加新增方法addUser:
@RestController public class HelloController { @Autowired UserService userService; @RequestMapping(value = "/hi", method = RequestMethod.GET) public String sayHello() { return "Hi buddy, How're you today ? "; } @RequestMapping(value = "/addUser", method = RequestMethod.POST) public String addUser(@RequestBody User user) { return userService.addUser(user); } }
4.8 應用入口類添加Mapper
@SpringBootApplication @MapperScan("cn.longmaster.messagesystem.mapper") public class MessageSystemApplication { public static void main(String[] args) { SpringApplication.run(MessageSystemApplication.class, args); } }
至此,全部的配置完成。啓動程序,經過POST發送添加請求,添加成功。以下圖
數據庫