1.开发环境

JDK:1.8.0

Service:tomcat v9.0

数据库:mysql 8.0.19

spring springmvc: 5.2.8.RELEASE

mybatis:3.5.1

mybatis-spring:2.0.5

数据源:0.9.5.4

IDE:eclipse

2.导入jar包

spring核心包:spring-beans,spring-core,spring-context,spring-express,依赖commons-logging

springAOP和事务:spring-tx,spring-jdbc,spring-aop,spring-aspect,aspectjweaver

数据源:c3p0,依赖mchange-commons-java

数据库驱动:mysql-connector-java

spring-mvc包:spring-web,spring-webmvc

整合mybatis:mybatis,mybatis-spring

image-20201123203412794

2.新建数据库和表

1
2
3
4
5
6
7
8
9
10
/*创建数据库*/
create database ssm;
/*创建数据表*/
create table userstable(
username varchar(20) ,
passwd varchar(20) ,
email varchar(20)
);
/*插入一条数据*/
insert into userstable values('cjq','123','cjq@qq.com');

3.编写配置文件

3.1建立包结构

image-20201123203651072

3.2编写web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>lab4_spring_springmvc_mybatis</display-name>

<!-- 1.配置初始化启动applicationContext -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- 2.配置一个前端控制器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 3.配置一个字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 4.配置一个rest过滤器 -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

3.3编写spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

<!-- 组件扫描 -->
<context:component-scan base-package="com.cjq.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 前端控制器的静态资源问题 -->
<mvc:default-servlet-handler/><!-- 处理一些静态资源,非jsp,jsp是交由jspServlet进行处理 -->
<mvc:annotation-driven/> <!-- 配置校验器注解驱动 -->

<!-- 视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
</bean>
</beans>

3.4编写applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
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">
<!-- 组件扫描,注意:此处不加use-default-filters="false",当是include-filter才加,吐血,找了半天 -->
<context:component-scan base-package="com.cjq.ssm" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- context:component-scan里面的use-default-filters默认值为true,使用默认的过滤器,
会自动扫描带有@Component、@Repository、@Service和@Controller的类。可以搭配exclude-filer使用
若配置use-default-filters="false",则需要自己定义include-filter,否则不会扫描任何注解
-->

<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- 事务 -->
<!-- id不可更改 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven/> <!-- 事务注解驱动 -->

<!-- spring整合mybatis -->
<!-- 1. -->
<bean id="" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 配置,mybatis使用的数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置mybatis使用的mapper映射文件,注意name中是Locations,value中是目录结构 -->
<property name="mapperLocations" value="classpath:com/cjq/ssm/mapper/*.xml"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="com.cjq.ssm.bean"></property>
</bean>
<!-- 2. -->
<!-- 配置扫描包,使得spring容器可以管理Dao接口的动态代理实现类 ,也可使用@Mapper注解-->
<mybatis-spring:scan base-package="com.cjq.ssm.dao"/>

</beans>

3.5编写mybatis-config.xml全局配置文件

db.properties

1
2
3
4
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=Hongkong
jdbc.username=root
jdbc.password=mysqlpasswd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 已经在spring的配置文件中进行了配置 -->
<!-- <properties resource="db.properties"></properties> -->

<settings>
<!--设置启用数据库字段下划线映射到java对象的驼峰式命名属性,默认为false -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!--
<typeAliases>
<package name="lab4.cjq.bean" />
</typeAliases>
-->

<!-- 已经在spring的配置文件中进行了配置 -->
<!-- <environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> -->

<!-- 已经在spring的配置文件中进行了配置 -->
<!-- <mappers>
<package name="lab4.cjq.dao" />
</mappers> -->
</configuration>

4.Demo代码示例

Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.cjq.ssm.bean;
/**
*类定义:
*@author Tsai E-mail:2726868668@qq.com
*@version 2020年11月23日 下午6:34:18
*/
public class User {
private String username;
private String password;
private String email;
//constructor
//getter and setter
//toString
}

Dao

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.cjq.ssm.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;

import com.cjq.ssm.bean.User;

/**
*类定义:
*@author Tsai E-mail:2726868668@qq.com
*@version 2020年11月23日 下午6:35:14
*/
public interface UserDao {
User selByNameAndPasswd(@Param("name") String userName,@Param("passwd") String password);
//也可使用注解编写
@Delete("delete * from userstable where username=#{0}")
int delByUsername(String username);
}

Mapper

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!--编写mapper对应的xml文件-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cjq.ssm.dao.UserDao"><!-- 这里写的是 包名+类名 -->
<select id="selByNameAndPasswd" resultType="User"> <!-- 接口绑定可省略paramType -->
select * from userstable where userName = #{name} and password= #{passwd}
</select>
</mapper>

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.cjq.ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cjq.ssm.bean.User;
import com.cjq.ssm.dao.UserDao;

/**
*类定义:
*@author Tsai E-mail:2726868668@qq.com
*@version 2020年11月23日 下午6:37:57
*/
@Service
public class UserService {
@Autowired
public UserDao userDao;
public boolean isLegal(User obj) {
User res=null;
res=userDao.selByNameAndPasswd(obj.getUsername(), obj.getPassword());
if(res!=null)return true;
return false;
};
public boolean delByName(User obj) {
userDao.delByUsername(obj.getUsername());
return true;
};
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.cjq.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cjq.ssm.bean.User;
import com.cjq.ssm.service.UserService;

/**
*类定义:
*@author Tsai E-mail:2726868668@qq.com
*@version 2020年11月23日 下午6:43:00
*/
@Controller
public class UserController {

@Autowired
public UserService service;
/*以判断用户名密码是否正确为例*/
@RequestMapping("/login/{userName}/{password}")
public String func(@PathVariable("userName") String name,@PathVariable("password") String passwd) {
//··········进行参数校验
//封装参数
User obj = new User();
obj.setUsername(name);
obj.setPassword(passwd);
//调用service,简单的跳转到两个html文件表示登录状态
if(service.isLegal(obj))return "loginSuccess";
else return "loginFail";
}
}

jsp页面

1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
密码错误,登录失败!(密码正确,登录成功!)
</body>
</html>

测试结果

image-20200823224816134

image-20200823224925903