<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
<groupId>com.codervibegroupId>
<artifactId>springbootmultimoduleartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springbootmultimodulename>
<description>springbootmultimoduledescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<packaging>pompackaging>
<modules>
<module>daomodule>
<module>webmodule>
<module>servermodule>
<module>modelmodule>
modules>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
@RestController
@RequestMapping("/user")
public class UserController {
Logger logger = LoggerFactory.getLogger(UserController.class);
@GetMapping("/list")
public R list() {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "孙悟空", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(2, "金蝉子", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(3, "鲁班七号", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(4, "鲁班大师", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(5, "安其拉", "111111", "[email protected]", new Date(), "1"));
logger.info("用户列表 :" + userList.toString());
return R.ok().data("userItems", userList).message("用户列表");
}
}
spring:
application:
name: springbootmultimodule
jackson:
date-format:yyyy-MM-dd HH:mm:ss
time-zone=GMT+8
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/vue_login?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: root
password: admin
server:
port: 5767
undertow:
io-threads: 16
worker-threads: 256
buffer-size: 1024
direct-buffers: true
servlet:
context-path: /springbootmultimodule
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.codervibe.springbootmultimodule.model
logging:
level:
root: info
com.codervibe.springbootmultimodule: info
com.codervibe.springbootmultimodule.mapper: debug
file:
path: ./log/
package com.codervibe.server.service;
import com.codervibe.model.User;
import java.util.List;
public interface UserService {
/**
* 查询所有用户信息
*
* @return boolean
*/
List<User> selectUserAll();
/**
* 添加用户信息列表
* @return boolean
*/
void addList(List<User> userList);
}
package com.codervibe.server.Impl;
import com.codervibe.mapper.UserMapper;
import com.codervibe.model.User;
import com.codervibe.server.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {
Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
@Resource
private UserMapper userMapper;
/**
* 查询所有用户信息
*
* @return userList
*/
@Override
public List<User> selectUserAll() {
logger.info(userMapper.selectUserAll().toString());
logger.info("即将返回数据");
return userMapper.selectUserAll();
}
/**
* 添加用户信息列表
*
* @param userList
* @return boolean
*/
@Override
public void addList(List<User> userList) {
for (User user : userList) {
userMapper.addList(user);
}
}
}
package com.codervibe.web.controller;
import com.codervibe.model.User;
import com.codervibe.server.service.UserService;
import com.codervibe.web.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
@RestController
@RequestMapping("/user")
public class UserController {
Logger logger = LoggerFactory.getLogger(UserController.class);
@Resource
private UserService userService;
@GetMapping("/list")
public R list() {
logger.info("用户列表 :" + userService.selectUserAll().toString());
return R.ok().data("userItems", userService.selectUserAll()).message("用户列表");
}
@GetMapping("/addList")
public R addList() {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "孙悟空", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(2, "金蝉子", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(3, "鲁班七号", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(4, "鲁班大师", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(5, "安其拉", "111111", "[email protected]", new Date(), "1"));
Map<String, Object> map = new HashMap<>();
try {
userService.addList(userList);
} catch (Exception e) {
e.printStackTrace();
map.put("fali", false);
map.put("message", "用户注册失败" + e.getMessage());
return R.error().data(map);
}
map.put("success", true);
map.put("message", "用户注册成功");
return R.ok().data(map);
}
}
package com.codervibe.mapper;
import com.codervibe.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface UserMapper {
/**
* 查询所有用户信息
* @return
*/
List<User> selectUserAll();
/**
* 添加用户信息列表
* @return boolean
*/
boolean addList(User user);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.codervibe.mapper.UserMapper">
<insert id="addList" parameterType="com.codervibe.model.User" useGeneratedKeys="true" keyProperty="id">
insert into user(username,password,email,createTime,role) values (#{username}, #{password},#{email}, #{createTime}, #{role})
insert>
<select id="selectUserAll" resultType="com.codervibe.model.User" >
select * from user
select>
mapper>
package com.codervibe.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.codervibe"})
@MapperScan({"com.codervibe.mapper"})
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
package com.codervibe.web.common.response;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R {
private Boolean success;
private Integer code;
private String message;
private Map<String, Object> data = new HashMap<>();
// 构造器私有
private R() {
}
// 通用返回成功
public static R ok() {
R r = new R();
r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
r.setCode(ResultCodeEnum.SUCCESS.getCode());
r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
return r;
}
// 通用返回失败,未知错误
public static R error() {
R r = new R();
r.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
r.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
return r;
}
// 设置结果,形参为结果枚举
public static R setResult(ResultCodeEnum result) {
R r = new R();
r.setSuccess(result.getSuccess());
r.setCode(result.getCode());
r.setMessage(result.getMessage());
return r;
}
/**
* ------------使用链式编程,返回类本身-----------
**/
// 自定义返回数据
public R data(Map<String, Object> map) {
this.setData(map);
return this;
}
// 通用设置data
public R data(String key, Object value) {
this.data.put(key, value);
return this;
}
// 自定义状态信息
public R message(String message) {
this.setMessage(message);
return this;
}
// 自定义状态码
public R code(Integer code) {
this.setCode(code);
return this;
}
// 自定义返回结果
public R success(Boolean success) {
this.setSuccess(success);
return this;
}
}
package com.codervibe.web.common.response;
import lombok.Getter;
@Getter
public enum ResultCodeEnum {
SUCCESS(true,20000,"成功"),
UNKNOWN_ERROR(false,20001,"未知错误"),
PARAM_ERROR(false,20002,"参数错误"),
NULL_POINT(false,20003,"空指针错误"),
HTTP_CLIENT_ERROR(false,20004,"客户端错误")
;
private Boolean success;
private Integer code;
private String message;
ResultCodeEnum(boolean success,Integer code,String message){
this.success =success;
this.code = code;
this.message =message;
}
}
项目地址
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
parent>
<groupId>com.codervibegroupId>
<artifactId>springbootmultimoduleartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springbootmultimodulename>
<description>springbootmultimoduledescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<packaging>pompackaging>
<modules>
<module>daomodule>
<module>webmodule>
<module>servermodule>
<module>modelmodule>
modules>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
@RestController
@RequestMapping("/user")
public class UserController {
Logger logger = LoggerFactory.getLogger(UserController.class);
@GetMapping("/list")
public R list() {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "孙悟空", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(2, "金蝉子", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(3, "鲁班七号", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(4, "鲁班大师", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(5, "安其拉", "111111", "[email protected]", new Date(), "1"));
logger.info("用户列表 :" + userList.toString());
return R.ok().data("userItems", userList).message("用户列表");
}
}
spring:
application:
name: springbootmultimodule
jackson:
date-format:yyyy-MM-dd HH:mm:ss
time-zone=GMT+8
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3307/vue_login?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: root
password: admin
server:
port: 5767
undertow:
io-threads: 16
worker-threads: 256
buffer-size: 1024
direct-buffers: true
servlet:
context-path: /springbootmultimodule
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.codervibe.springbootmultimodule.model
logging:
level:
root: info
com.codervibe.springbootmultimodule: info
com.codervibe.springbootmultimodule.mapper: debug
file:
path: ./log/
package com.codervibe.server.service;
import com.codervibe.model.User;
import java.util.List;
public interface UserService {
/**
* 查询所有用户信息
*
* @return boolean
*/
List<User> selectUserAll();
/**
* 添加用户信息列表
* @return boolean
*/
void addList(List<User> userList);
}
package com.codervibe.server.Impl;
import com.codervibe.mapper.UserMapper;
import com.codervibe.model.User;
import com.codervibe.server.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("UserService")
public class UserServiceImpl implements UserService {
Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
@Resource
private UserMapper userMapper;
/**
* 查询所有用户信息
*
* @return userList
*/
@Override
public List<User> selectUserAll() {
logger.info(userMapper.selectUserAll().toString());
logger.info("即将返回数据");
return userMapper.selectUserAll();
}
/**
* 添加用户信息列表
*
* @param userList
* @return boolean
*/
@Override
public void addList(List<User> userList) {
for (User user : userList) {
userMapper.addList(user);
}
}
}
package com.codervibe.web.controller;
import com.codervibe.model.User;
import com.codervibe.server.service.UserService;
import com.codervibe.web.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
@RestController
@RequestMapping("/user")
public class UserController {
Logger logger = LoggerFactory.getLogger(UserController.class);
@Resource
private UserService userService;
@GetMapping("/list")
public R list() {
logger.info("用户列表 :" + userService.selectUserAll().toString());
return R.ok().data("userItems", userService.selectUserAll()).message("用户列表");
}
@GetMapping("/addList")
public R addList() {
List<User> userList = new ArrayList<>();
userList.add(new User(1, "孙悟空", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(2, "金蝉子", "1111", "[email protected]", new Date(), "1"));
userList.add(new User(3, "鲁班七号", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(4, "鲁班大师", "11111", "[email protected]", new Date(), "1"));
userList.add(new User(5, "安其拉", "111111", "[email protected]", new Date(), "1"));
Map<String, Object> map = new HashMap<>();
try {
userService.addList(userList);
} catch (Exception e) {
e.printStackTrace();
map.put("fali", false);
map.put("message", "用户注册失败" + e.getMessage());
return R.error().data(map);
}
map.put("success", true);
map.put("message", "用户注册成功");
return R.ok().data(map);
}
}
package com.codervibe.mapper;
import com.codervibe.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface UserMapper {
/**
* 查询所有用户信息
* @return
*/
List<User> selectUserAll();
/**
* 添加用户信息列表
* @return boolean
*/
boolean addList(User user);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.codervibe.mapper.UserMapper">
<insert id="addList" parameterType="com.codervibe.model.User" useGeneratedKeys="true" keyProperty="id">
insert into user(username,password,email,createTime,role) values (#{username}, #{password},#{email}, #{createTime}, #{role})
insert>
<select id="selectUserAll" resultType="com.codervibe.model.User" >
select * from user
select>
mapper>
package com.codervibe.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = {"com.codervibe"})
@MapperScan({"com.codervibe.mapper"})
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
package com.codervibe.web.common.response;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R {
private Boolean success;
private Integer code;
private String message;
private Map<String, Object> data = new HashMap<>();
// 构造器私有
private R() {
}
// 通用返回成功
public static R ok() {
R r = new R();
r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
r.setCode(ResultCodeEnum.SUCCESS.getCode());
r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
return r;
}
// 通用返回失败,未知错误
public static R error() {
R r = new R();
r.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
r.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
return r;
}
// 设置结果,形参为结果枚举
public static R setResult(ResultCodeEnum result) {
R r = new R();
r.setSuccess(result.getSuccess());
r.setCode(result.getCode());
r.setMessage(result.getMessage());
return r;
}
/**
* ------------使用链式编程,返回类本身-----------
**/
// 自定义返回数据
public R data(Map<String, Object> map) {
this.setData(map);
return this;
}
// 通用设置data
public R data(String key, Object value) {
this.data.put(key, value);
return this;
}
// 自定义状态信息
public R message(String message) {
this.setMessage(message);
return this;
}
// 自定义状态码
public R code(Integer code) {
this.setCode(code);
return this;
}
// 自定义返回结果
public R success(Boolean success) {
this.setSuccess(success);
return this;
}
}
package com.codervibe.web.common.response;
import lombok.Getter;
@Getter
public enum ResultCodeEnum {
SUCCESS(true,20000,"成功"),
UNKNOWN_ERROR(false,20001,"未知错误"),
PARAM_ERROR(false,20002,"参数错误"),
NULL_POINT(false,20003,"空指针错误"),
HTTP_CLIENT_ERROR(false,20004,"客户端错误")
;
private Boolean success;
private Integer code;
private String message;
ResultCodeEnum(boolean success,Integer code,String message){
this.success =success;
this.code = code;
this.message =message;
}
}
项目地址