1、本次教程是与springboot结合使用的,故此需要先搭建好springboot项目环境,本章教如何搭建springboot项目环境开始
然后再次点击next和finsh创建即可
2、创建mysql所需的表结构
CREATE DATABASE IF NOT EXISTS `shirodb` CHARACTER SET utf8mb4;
USE `shirodb`;
CREATE TABLE `user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` VARCHAR(30) DEFAULT NULL COMMENT '用户名',
`pwd` VARCHAR(50) DEFAULT NULL COMMENT '密码',
`rid` BIGINT(20) DEFAULT NULL COMMENT '角色编号',
PRIMARY KEY (`id`)) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='用户表';
3、添加pom文件中所需jar包依赖
<!-- lombok所需JAR包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
<!--mybatis-plus所需JAR包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- spring-boot-starter-web所需JAR包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<!-- mysql所需JAR包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
4、创建User实体
package com.wxg.springbootshiro.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
Integer id;
String name;
String pwd;
Integer rid;
}
5、创建User的mapper接口文件
package com.wxg.springbootshiro.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wxg.springbootshiro.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
6、启动类加入@MapperScan扫描位置包
7、创建service层
package com.wxg.springbootshiro.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wxg.springbootshiro.entity.User;
public interface UserService extends IService<User> {
}
8、创建service实现层impl
package com.wxg.springbootshiro.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxg.springbootshiro.entity.User;
import com.wxg.springbootshiro.mapper.UserMapper;
import com.wxg.springbootshiro.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServerImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
9、resources目录下新建application.yml文件
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shirodb?characterEncoding=utf8&useSSL=false
username: root
password: 123456
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
10、创建UserController
package com.wxg.springbootshiro.controller;
import com.wxg.springbootshiro.entity.User;
import com.wxg.springbootshiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("list")
public List<User> list(){
List<User> list = userService.list();
return list;
}
}
11、整个目录如下
12、启动项目工程,默认端口为8080,在浏览器打开http://localhost:8080/list