热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot+Mybatis+Vue整合

创建springboot项目添加配置spring.datasource.urljdbc:mysql:demo?serverTimezoneUTC&characterEncodin

创建springboot项目


添加配置

spring.datasource.url = jdbc:mysql:///demo?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#指定映射xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
#端口
server.port=8888

mybatis反向生成

db.sql

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`email` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`username` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`password` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,`createDate` timestamp NULL DEFAULT NULL,`address` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '王者归来', '287664409@qq.com', 'admin', '123456', '2020-09-21 00:00:00', '北京市通州区商通大道1号');
INSERT INTO `user` VALUES ('2', '王小虎', '', 'tiger', '123456', '2020-09-22 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('3', '王小虎', '', 'tiger', '123456', '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('4', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('5', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('6', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('7', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('8', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('9', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('10', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('11', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');
INSERT INTO `user` VALUES ('12', '王小虎', null, 'tiger', null, '2020-09-23 00:00:00', '上海市普陀区金沙江路 1518');

generatorConfig.xml





jdbc.properties

jdbc.driverLocation=D:\\Software\\mysql-connector-java-8.0.11.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
#数据库地址
jdbc.jdbcUrl=jdbc:mysql:///demo?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
#用户名
jdbc.user=root
#密码
jdbc.password=123456
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2

运行

LoginController

/*********************************************** Copyright (C) 2019 IBM All rights reserved.********** K*I*N*G ********** B*A*C*K *******/
package com.example.demo.controller;
/*** @author Moses ** @Date 2020/9/22 12:00*/import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;@CrossOrigin
@Controller
public class LoginController {@Autowiredprivate IUserService userService;@ResponseBody@PostMapping("/login")public boolean login(User user) {return userService.findUser(user) == null;}
}

UserController

/*********************************************** Copyright (C) 2019 IBM All rights reserved.********** K*I*N*G ********** B*A*C*K *******/
package com.example.demo.controller;
/*** @author Moses ** @Date 2020/9/22 12:00*/import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller
@RequestMapping(value = "/user")
public class UserController {@Autowiredprivate IUserService userService;@ResponseBody@GetMapping("/findById/{id}")public User findById(@PathVariable(name = "id") Integer id) {return userService.findById(id);}@ResponseBody@GetMapping("/findUserList")public List findUserList() {List list = userService.findUserList();return list;}
}

IUserService

package com.example.demo.service;import com.example.demo.entity.User;import java.util.List;public interface IUserService {User findById(Integer id);List findUserList();User findUser(User user);
}

UserServiceImpl

package com.example.demo.service.impl;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;@Service("userService")
public class UserServiceImpl implements IUserService {@Resourceprivate UserMapper userMapper;@Overridepublic User findById(Integer id) {return userMapper.selectByPrimaryKey(id);}@Overridepublic List findUserList() {return userMapper.findUserList();}@Overridepublic User findUser(User user) {return userMapper.findUser(user);}
}

 

DemoApplication

package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

 

UserMapper.xml添加


启动

localhost:8888/user/findById/1


创建vue项目


安装node


安装vue

npm install --global vue-cli

npm install -g webpack

vue init webpack vue


安装淘宝npm

npm install -g cnpm --registry=https://registry.npm.taobao.org


安装element-ui

cnpm install element-ui --save


main.js,引入element-ui依赖

import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(Element)

安装axios

cnpm install axios --save

main.js中全局引入axios

import axios from 'axios'
Vue.prototype.$axios = axios //

配置自动格式化和代码纠正

{"editor.formatOnSave": true,"editor.codeActionsOnSave": {"source.fixAll.eslint": true},"eslint.format.enable": true,"[vue]": {"editor.defaultFormatter": "dbaeumer.vscode-eslint"}}


config跨域配置

index.js添加

proxyTable: {'/api': {target: 'http://127.0.0.1:8888',changeOrigin: true,pathRewrite: {'^/api': ''}}},

dev.env.js添加

BASE_API: '"/api/"',

Login.vue




UserIndex.vue




UserDetail.vue




router配置

index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Login from '@/view/Login'
import UserIndex from '@/view/UserIndex'
import UserDetail from '@/view/UserDetail'
Vue.use(Router)export default new Router({routes: [{path: '/',name: 'HelloWorld',component: HelloWorld},{path: '/login',name: 'Login',component: Login},{path: '/user',name: 'UserIndex',component: UserIndex},{path: '/userDetail',name: 'UserDetail',component: UserDetail}]
})

 

启动

http://localhost:8080/#/login

 

登录跳转

点击查看

 

 


推荐阅读
  • 本文探讨了在使用MyBatis Generator过程中遇到的'Communication Link Failure'错误,并提供了多种有效的解决方案。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 本文深入探讨了如何通过调整InnoDB的关键配置参数来优化MySQL的随机IO性能,涵盖了缓存、日志文件、预读机制等多个方面,帮助读者全面提升数据库系统的性能。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本文详细介绍了如何在ECharts中使用线性渐变色,通过echarts.graphic.LinearGradient方法实现。文章不仅提供了完整的代码示例,还解释了各个参数的具体含义及其应用场景。 ... [详细]
  • 在尝试从数据库获取设置的过程中,遇到了一个致命错误:Fatal error: Call to a member function bind_param() on boolean。本文将详细分析该错误的原因,并提供解决方案。 ... [详细]
  • CentOS 7.6环境下Prometheus与Grafana的集成部署指南
    本文旨在提供一套详细的步骤,指导读者如何在CentOS 7.6操作系统上成功安装和配置Prometheus 2.17.1及Grafana 6.7.2-1,实现高效的数据监控与可视化。 ... [详细]
  • 本文探讨了2019年前端技术的发展趋势,包括工具化、配置化和泛前端化等方面,并提供了详细的学习路线和职业规划建议。 ... [详细]
  • 本文探讨了在使用阿里云RDS实例时遇到的一个时区问题。该问题导致系统时间与预期时间相差13小时。通过深入分析,发现问题是由于名为CST的时区存在多种解释,特别是在MySQL和Java之间进行时区协商时出现的误解。 ... [详细]
  • 解读MySQL查询执行计划的详细指南
    本文旨在帮助开发者和数据库管理员深入了解如何解读MySQL查询执行计划。通过详细的解析,您将掌握优化查询性能的关键技巧,了解各种访问类型和额外信息的含义。 ... [详细]
  • Symfony是一个功能强大的PHP框架,以其依赖注入(DI)特性著称。许多流行的PHP框架如Drupal和Laravel的核心组件都基于Symfony构建。本文将详细介绍Symfony的安装方法及其基本使用。 ... [详细]
author-avatar
帕格迪奥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有