01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(下)
SSM入门实战(登录功能的实现)
(1)数据库文件--->user.sql
/*Navicat Premium Data TransferSource Server : MySQLSource Server Type : MySQLSource Server Version : 50719Source Host : localhost:3306Source Schema : ssm_mavenTarget Server Type : MySQLTarget Server Version : 50719File Encoding : 65001Date: 16/05/2020 18:33:03
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (`userID` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户id',`userName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',`gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '性别',`avatar` varchar(255) CHARACTER SET swe7 COLLATE swe7_swedish_ci NULL DEFAULT NULL COMMENT '头像',`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',`telephone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',`userType` int(5) NOT NULL COMMENT '用户类型',`regTime` datetime(0) NOT NULL COMMENT '注册时间',`regIP` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '注册IP',PRIMARY KEY (`userID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('201601', '沐晴つ', '女', '', '123456', '13578519307', 1, '2020-05-12 12:10:06', '127.0.0.0.1');
INSERT INTO `user` VALUES ('201602', '墨夕つ', '男', '', '123456', '13578519307', 1, '2020-05-12 12:10:06', '127.0.0.0.1');SET FOREIGN_KEY_CHECKS = 1;
(2)逆向工程generator生成Mapper和Entity
(2-a)Generator.java
package com.edu.util;import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;import java.io.File;
import java.util.ArrayList;
import java.util.List;//逆向工程,生成Mapper和Entity
public class Generator {public void generator() throws Exception{List<String> warnings = new ArrayList<String>();boolean overwrite = true;/**指向逆向工程配置文件 idea中右击文件copyPath*/File configFile = new File("D:\\IdeaProjects\\SSM_Maven\\src\\main\\resources\\generatorConfig.xml");ConfigurationParser parser = new ConfigurationParser(warnings);Configuration config = parser.parseConfiguration(configFile);DefaultShellCallback callback = new DefaultShellCallback(true);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);myBatisGenerator.generate(null);}public static void main(String[] args) throws Exception {try {Generator generator = new Generator();generator.generator();} catch (Exception e) {e.printStackTrace();}}
}
(2-b)GeneratorConfig.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><context id="mybatisGenerator" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <!--<*********************--><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/ssm_maven?useUnicode=true&characterEncoding=utf8"userId="root"password="root"></jdbcConnection><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- targetProject:生成PO/Entity类的位置 --> <!--<**********************************--><javaModelGenerator targetPackage="com.edu.entity" targetProject=".\src\main\java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><!-- targetProject:mapper映射文件生成的位置 --> <!--<***********************--><sqlMapGenerator targetPackage="com.edu.mapper" targetProject=".\src\main\java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></sqlMapGenerator><!-- targetPackage:mapper接口生成的位置 --> <!--<***************--><javaClientGenerator type="XMLMAPPER" targetPackage="com.edu.mapper" targetProject=".\src\main\java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="false" /></javaClientGenerator><!-- 指定数据库表 --><table tableName="user"></table> <!--<***************--><!-- 当需要指定特定字段的java类型时<table schema="" tableName=""><columnOverride column="" javaType="" /></table> --></context>
</generatorConfiguration>
(3)Service---业务层
(3-a)UserService.java
package com.edu.service;import com.edu.entity.User;
import com.edu.entity.UserExample;import java.util.List;public interface UserService {long countByExample(UserExample example);int deleteByExample(UserExample example);int deleteByPrimaryKey(String userID);int insert(User record);int insertSelective(User record);List<User> selectByExample(UserExample example);User selectByPrimaryKey(String userID);User findUser(String userID, String password);int updateByExampleSelective(User record, UserExample example);int updateByExample(User record, UserExample example);int updateByPrimaryKeySelective(User record);int updateByPrimaryKey(User record);
}
(3-b)UserServiceImpl.java
package com.edu.service.impl;import com.edu.entity.User;
import com.edu.entity.UserExample;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;//@Service用于标注业务层组件
@Service("UserService")
public class UserServiceImpl implements UserService {//set方法注入private UserMapper userMapper;@Autowiredpublic void setUserMapper (UserMapper userMapper) {this.userMapper = userMapper;}@Overridepublic long countByExample(UserExample example) {return userMapper.countByExample(example);}@Overridepublic int deleteByExample(UserExample example) {return userMapper.deleteByExample(example);}@Overridepublic int deleteByPrimaryKey(String userID) {return userMapper.deleteByPrimaryKey(userID);}@Overridepublic int insert(User record) {return userMapper.insert(record);}@Overridepublic int insertSelective(User record) {return userMapper.insertSelective(record);}@Overridepublic List<User> selectByExample(UserExample example) {return userMapper.selectByExample(example);}@Overridepublic User selectByPrimaryKey(String userID) {return userMapper.selectByPrimaryKey(userID);}@Overridepublic User findUser(String userID, String password) {return userMapper.findUser(userID, password);}@Overridepublic int updateByExampleSelective(User record, UserExample example) {return userMapper.updateByExampleSelective(record, example);}@Overridepublic int updateByExample(User record, UserExample example) {return userMapper.updateByExample(record, example);}@Overridepublic int updateByPrimaryKeySelective(User record) {return userMapper.updateByPrimaryKeySelective(record);}@Overridepublic int updateByPrimaryKey(User record) {return userMapper.updateByPrimaryKey(record);}
}
(4)Controller---控制层--->LoginController.java
package com.edu.controller;import com.edu.entity.User;
import com.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class LoginController {private UserService userService;@Autowiredpublic void setUserService(UserService userService) {this.userService = userService;}//登录处理@RequestMapping("/login")public String login(String userID, String password) {User user=userService.findUser(userID, password);if(user != null) {return "success";}else {return "fail";}}}
(5)登录页面--->index.jsp
<%--Created by IntelliJ IDEA.User: MuQingDate: 2020/5/16Time: 9:57To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<html>
<head><meta charset="UTF-8"><title>登录</title>
</head>
<body><form action="${pageContext.request.contextPath}/login.action" method="post">用户名:<input type="text" name="userID">密码:<input type="password" name="password"><input type="submit" value="登录"></form>
</body>
</html>
(6)浏览器网址: http://localhost:8080/SSM_Maven/
01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(下)相关推荐
- 01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(上)
1.环境搭建 (1)JDK 1.8下载安装 https://www.oracle.com/cn/java/technologies/javase-jdk8-downloads.html (2)Apac ...
- 手把手教你整合 SpringMvc+Spring+MyBatis+Maven
注:该教程是参考孙宇老师的<SpringMvc+Spring+Mybatis+Maven整合视频教程1>整理的,花了我六个多小时,边复习视频边调代码边写教程,保证该教程每一步都能正确执行, ...
- 孙宇java_[JAVA] 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程...
资源介绍 孙宇老师Struts2+Hibernate4+Maven+EasyUI+SpringMvc+Spring+Mybatis+Maven整合课程 ===============课程目录===== ...
- 利用SSM(springmvc+spring+mybatis)实现多表联合查询
最近在做在eclipse + maven搭建SSM框架下做一个简单的后台管理系统,因为是第一次搭建SSM项目,在mybatis进行多表连接查询的时候遇到问题,不知道如何进行处理?在网上搜了一下解决方法 ...
- 1 (SSM) springMVC + spring + Mybatis(MySQL)学习笔记 ------ 阶段成果笔记
学习了一段时间spring,springMVC和Mybatis,从开始学到现在熬了好多夜晚,好几个深夜和bug作战,真是难受. 打算写系列学习笔记,第一篇从一个小的成果说起吧,刚刚学的看这篇可能有点吃 ...
- idea+springmvc+spring+mybatis+maven整合返回json数据web api-
本人大三,第一次写博客,还有许多不懂得地方,如果有不当的地方 欢迎各位指教 项目地址:https://github.com/qq571831158/Springmvc-spring-mybatisDe ...
- SSM框架整合一(springmvc+spring+mybatis+maven+tomcat)
一,环境说明 jdk1.7.0_07(cmd命令行输入java -version查看),点击下载 eclipse Kepler Service Release 2 apache-maven-3.3.9 ...
- SSH(Struts,Spring,Hibernate )和SSM(SpringMVC,Spring,MyBatis )的区别,抽丝剥茧的给你讲清楚
SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...
- ssm(springMVC + spring+MyBatis) 小例
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 整体环境参见本人另一文:http://blog.csdn.net/jiangyu1013/artic ...
最新文章
- 曝iOS存在缺陷 黑客可强制iPhone拨打收费电话
- FTP同步的另类解决办法——NetDrive
- docker 开启remote api
- mysql创建反弹函数,MySql创建函数
- java从外部得到数据_java – 如何实现Observer以从侦听器获取数据?
- 特斯拉全球超级充电站已超过25000座 国内超过870座
- 用matlab解拉格朗日,用MATLAB实现拉格朗日插值
- java虚拟机执行过程
- mysql英文怎么发音_英文字母和汉语拼音先学哪个?科学答案在这里
- 草图大师SketchUp 2022 安装教程
- 阿里云大学>【Python学习路线】Python语言基础自测考试 - 初级难度 | 包过关系列
- 蹩脚的Access分页语句
- 433 490 868 915Mhz lora频段贴片天线方案 CA-S01 CrossAir贴片天线
- SFTP传输文件工具FileZilla下载安装教程
- 【互联网大厂机试真题 - 华为】九宫格
- 学习大数据需要什么语言基础
- python中geometry用法_Python geometry.Point方法代码示例
- 基于FPGA的啸叫检测与抑制系统设计与实现verilog
- DeepLearning-500-questions
- 2017年如何实现1个亿的小目标?