一、第一步创建maven项目
file>>>>maven project>>>Filter输入web
1、
2、
3、新建的项目结构为:
4、切换为maven目录(去除勾选的选项)
调整后的目录结构
5、配置pom.xml依赖包
6、resources目录下增加application.yml配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: root
mybatis:
config-location: classpath:mybatis-config.xml
mapper-locations: classpath:mybatis/*Mapper.xml
7、mybatis-config.xml配置文件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
8、src/main/java目录创建如下目录
9、AppleApplication.java服务启动类
package com.neon.apple;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.neon.apple.dao")
public class AppleApplication {
public static void main(String[] args) {
SpringApplication.run(AppleApplication.class, args);
}
}
10、user实体类
package com.neon.apple.model;
public class User {
private String userId;
private String userName;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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;
}
}
11、dao接口
package com.neon.apple.dao;
import java.util.List;
import com.neon.apple.model.User;
public interface IUserDao {
List
User find(String userId);
int insert(User user);
int delete(String userId);
int update(User user);
}
12、service接口
package com.neon.apple.service;
import java.util.List;
import com.neon.apple.model.User;
public interface IUserService {
List
int insert(User user);
int delete(String userId);
int update(User user);
User find(String userId);
}
13、service实现层
package com.neon.apple.service.impl;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.neon.apple.dao.IUserDao;
import com.neon.apple.model.User;
import com.neon.apple.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
@Autowired
private IUserDao userDao;
@Override
public List
return userDao.getAll();
}
@Override
public int insert(User user) {
return userDao.insert(user);
}
@Override
public int delete(String userId) {
return userDao.delete(userId);
}
@Override
public int update(User user) {
User userOutVo=null;
if(user!=null && !StringUtils.isEmpty(user.getUserId())) {
userOutVo=userDao.find(user.getUserId());
if(userOutVo!=null) {
LOGGER.info("用户名:"+user.getUserName());
userOutVo.setUserName(user.getUserName());
userOutVo.setPassword(user.getPassword());
}
}
return userDao.update(userOutVo);
}
@Override
public User find(String userId) {
return userDao.find(userId);
}
}
14、控制层UserController
package com.neon.apple.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.neon.apple.model.User;
import com.neon.apple.service.IUserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
IUserService userService;
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping(value={"/getUserList"}, method=RequestMethod.POST)
public List
System.out.println("查询用户信息。。。。。。。。。");
return userService.getAll();
}
@RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
@ResponseBody
public void addUser(@RequestBody User user){
System.out.println("开始新增。。。。。。。。。。。。。。");
userService.insert(user);
}
@RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
@ResponseBody
public void updateUser(@RequestBody User user){
System.out.println("开始修改信息。。。。。。。。");
userService.update(user);
}
@RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
@ResponseBody
public void deleteUser(@RequestParam String id){
System.out.println("开始删除信息。。。。。。。。");
userService.delete(id);
}
}
15、在src/main/resources下新建目录mybatis,创建UserMapper.xml文件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into tb_user(user_id,user_name,password) values(#{userId},#{userName},#{password})
update tb_user
where user_id=#{userId}
delete from tb_user where user_id=#{userId}
至此,基本的crud操作基本完成。现在进行验证
二、
1、找到AppleApplication.java文件右键Run as >>>>Java Application启动
(控制台无报错信息即说明框架整合成功)
2、用Postman测试一下接口
添加用户接口测试成功
查询数据接口测试成功
附可直接运行源码下载地址:https://download.csdn.net/download/llt1991com/11271866