参考资料
IDEA搭建Springboot+SpringMVC+Mybatis+Mysql(详细、易懂)
创建项目
创建多个目录
把application.properties改成yml格式并补充配置
mysql中创建数据库和表
create database db1;
use db1;
create table `user` (
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.yml内容
spring:
datasource:
url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:/resources/mappers/*.xml
type-aliases-package: com.example.demo.dao
server:
port: 8080
servlet:
context-path: /demo
补充多个文件内容
pom.xml里面添加依赖
在dependencies中添加web依赖
<!--提供web服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.4</version>
</dependency>
在build中添加mybatis的xml文件位置
<resources>
<resource>
<directory>src/main/</directory>
<!--识别mybatis的Mapper.xml-->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--指定资源的位置-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
src/main/resources/mappers/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.example.demo.dao.UserMapper">
<insert id="insertUserInfo" parameterType="com.example.demo.entity.User">
INSERT INTO user
(name)
VALUES
(#{name,jdbcType=VARCHAR})
</insert>
</mapper>
src/main/java/com/example/demo/service/UserService.java
package com.example.demo.service;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int addUserInfo(String name) {
User user = new User(name);
int res = userMapper.insertUserInfo(user);
if (res > 0) {
return 1;
}
return 0;
}
}
src/main/java/com/example/demo/entity/User.java
package com.example.demo.entity;
public class User {
private String name;
public User(String name) {
this.name = name;
}
}
src/main/java/com/example/demo/dao/UserMapper.java
package com.example.demo.dao;
import com.example.demo.entity.User;
public interface UserMapper {
int insertUserInfo(User user);
}
src/main/java/com/example/demo/controller/UserController.java
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/user")
@ResponseBody
public int addUserInfo(@RequestParam("name") String name){
int res = userService.addUserInfo(name);
return res;
}
}
Postman测试
POST http://localhost:8080/demo/user?name=wjq
插入成功返回1。
标签:java,name,Spring,Boot,MVC,demo,import,com,example From: https://www.cnblogs.com/WJQ2017/p/17556273.html