包 | 所属层 | 作用 |
---|---|---|
java | 无 | 里面保存着 java 源代码 |
resources | 无 | 存放各种资源文件,SSM配置文件等 |
webapp | 无 | 存放静态资源 |
com.vue.controller | controller | 主要负责业务模块的控制,存放各种的 控制器 |
com.vue.service | service | 负责业务的主要逻辑,在这里才会调用 dao层 |
com.vue.entity | dao/在SSM中也叫 mapper | 存放实体类的包 |
com.vue.mapper | dao/在SSM中也叫 mapper | 存放 mapper接口 和 mapper.xml 的包 |
com.vue.mapper包的问题:mapper.xml 放在 java 包里可能导致运行时不能正确编译,需要在 Maven 中手动添加一些属性,在下面会写出。
vuespring:
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80017
Source Host : localhost:3306
Source Schema : vuespring
Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001
Date: 31/03/2020 20:51:30
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`upwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`uid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'aaa', '111');
INSERT INTO `user` VALUES (2, '老王', '222');
SET FOREIGN_KEY_CHECKS = 1;
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>VueSpringartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>VueSpring Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<spring.version>5.2.3.RELEASEspring.version>
<mybatis.version>3.5.2mybatis.version>
<mysql.version>5.1.47mysql.version>
<slf4j.version>1.6.6slf4j.version>
<log4j.version>1.2.17log4j.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.5version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.8version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>com/vue/mapper/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
<finalName>VueSpringfinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
解决 mapper.xml 文件没有编译的 配置:
src/main/java
com/vue/mapper/*.xml
true
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/vuespring?ucharacterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=这里的密码看自己的数据库密码是什么就写什么
log4j.properties:
# rootLogger 第一参数为日志级别, 其后参数为日志载体
log4j.rootLogger=DEBUG,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
#设定输出格式
log4j.appender.Console.layout.ConversionPattern=[%d][%p][%c] %m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<context:component-scan base-package="com.vue">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置通知类 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 设置传播行为 -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<!-- select/find开头的所有方法为只读 -->
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop配置 -->
<aop:config>
<!-- 切面配置 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.vue.service.*.*(..))"/>
</aop:config>
<!-- 引入peroperties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 配置mybatis中的SqlSessionFactory工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.vue.entity"/>
</bean>
<!-- 配置mapper包扫描,创建mapper代理对象,如果mapper.xml和mapper.java在同一个文件夹下这样写就行了 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.vue.mapper"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 扫描controller包下的类,注册进spring容器中 -->
<context:component-scan base-package="com.vue.controller"/>
<!-- 开启springmvc注解支持,自动创建处理器映射器和处理器适配器 -->
<mvc:annotation-driven/>
<!-- 过滤静态资源 -->
<mvc:default-servlet-handler />
<!-- 配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 因为在applicationContext.xml中的SqlSessionFactory配置中可以配置大部分属性,
并且在这个教程中不涉及其Mybatis设置,因此不需要在此配置属性-->
</configuration>
package com.vue.mapper;
import com.vue.entity.User;
/**
* @author wsw16
*/
public interface UserMapper {
User selectByAccount(User record);
}
<mapper namespace="com.vue.mapper.UserMapper">
<resultMap id="baseResultMap" type="com.vue.entity.User">
<id property="uid" jdbcType="INTEGER" column="uid"/>
<result property="uname" jdbcType="VARCHAR" column="uname"/>
<result property="upwd" jdbcType="VARCHAR" column="upwd"/>
resultMap>
<select id="selectByAccount" parameterType="com.vue.entity.User" resultMap="baseResultMap">
select * from user where uname = #{uname,jdbcType=VARCHAR} and upwd = #{upwd,jdbcType=VARCHAR}
select>
mapper>
package com.vue.service;
import com.vue.entity.User;
import com.vue.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author wsw16
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User selectByAccount(User record) {
return userMapper.selectByAccount(record);
}
}
package com.vue.controller;
import com.vue.entity.User;
import com.vue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
@SessionAttributes("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String tologin(){
return "login";
}
@PostMapping("/login")
public String login(ModelMap modelMap, String uname, String upwd) {
// 将要登录的账号密码封装进bean里面
User userLogin = new User();
userLogin.setUname(uname);
userLogin.setUpwd(upwd);
// 连接数据库判断账号密码是否正确,如果userSuccess==null,就代表登录失败
User userSuccess = userService.selectByAccount(userLogin);
if (userSuccess != null) {
// 将用户信息保存到request域中,用于页面获取数据
modelMap.addAttribute("user", userSuccess);
// 正确则跳转成功界面
return "redirect: success.jsp";
} else {
// 如果userSuccess为空,代表登录失败,输出错误信息
modelMap.addAttribute("errMsg", "账号或密码错误");
return "login";
}
}
}
@SessionAttributes属性:
value:字符串数组,用于将模型中的同名的数据存储到session中,如果只有单个属性,可以省略 value = “user” 中的前缀:value =。
name:与value功能相同。
type:可以指定多个参数类型,将模型中相同类型的数据存储到session中。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="login" method="post">
<c:if test="${errMsg != null}">
<h1>${errMsg}</h1>
</c:if>
<label><input type="text" name="uname" placeholder="请输入账户名"></label>
<label><input type="text" name="upwd" placeholder="请输入密码"></label>
<button type="submit">登录</button>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>欢迎,${user.uname}</h1>
</body>
</html>
总结一下难点:编写配置文件时要注意;编写class类时不要忘记加上对应的注解;mapper.xml 容易写错。
测试模块就不搞太复杂,CRUD操作感兴趣的可以自己编写一下。
如果该文章有帮到你的忙,我会很开心。
知识点引用:
@SessionAttributes属性: https://www.cnblogs.com/caoyc/p/5635914.html