热门标签 | 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

 

登录跳转

点击查看

 

 


推荐阅读
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社区 版权所有