1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(下)

01-Intellij IDEA搭建SSM(SpringMVC+Spring+Mybatis+Maven)框架(下)

时间:2022-12-12 08:37:22

相关推荐

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/ 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 ('01', '沐晴つ', '女', '', '123456', '13578519307', 1, '-05-12 12:10:06', '127.0.0.0.1');INSERT INTO `user` VALUES ('02', '墨夕つ', '男', '', '123456', '13578519307', 1, '-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和Entitypublic 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 "-////DTD MyBatis Generator Configuration 1.0//EN""/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&amp;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;@Controllerpublic 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: /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/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。