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

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建立包结构

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>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <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> <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/> <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"> <context:component-scan base-package="com.cjq.ssm" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
<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> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven/> <bean id="" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:com/cjq/ssm/mapper/*.xml"></property> <property name="typeAliasesPackage" value="com.cjq.ssm.bean"></property> </bean> <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> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> <setting name="lazyLoadingEnabled" value="true" /> <setting name="aggressiveLazyLoading" value="false"/> </settings>
</configuration>
|
4.Demo代码示例
Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.cjq.ssm.bean;
public class User { private String username; private String password; private String email; }
|
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;
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"?>
<!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"> 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;
@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;
@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); 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>
|
测试结果

