基本概念
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令
开发环境搭建结构图
创建User数据表
CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `sex` varchar(50) NOT NULL, `phone` varchar(50) NOT NULL, `address` varchar(100) DEFAULT NULL, PRIMARY KEY (`uid`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
创建项目Maven引入需要的JAR包(pom.xml)
UTF-81.81.85.0.2.RELEASE1.6.61.2.125.1.63.4.5org.aspectj aspectjweaver 1.6.8org.springframework spring-aop ${spring.version}org.springframework spring-context ${spring.version}org.springframework spring-web ${spring.version}org.springframework spring-webmvc ${spring.version}org.springframework spring-test ${spring.version}org.springframework spring-tx ${spring.version}org.springframework spring-jdbc ${spring.version}junit junit 4.12compilemysql mysql-connector-java ${mysql.version}javax.servlet servlet-api 2.5providedjavax.servlet.jsp jsp-api 2.0providedjstl jstl 1.2log4j log4j ${log4j.version}org.slf4j slf4j-api ${slf4j.version}org.slf4j slf4j-log4j12 ${slf4j.version}org.mybatis mybatis ${mybatis.version}org.mybatis mybatis-spring 1.3.0c3p0 c3p0 0.9.1.2jarcompile
配置web.xml文件(在WEB-INF)目录下
Archetype Created Web ApplicationcontextConfigLocationclasspath:applicationContext.xmlcharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8characterEncodingFilter/*org.springframework.web.context.ContextLoaderListenerdispatcherServletorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:springmvc.xml1dispatcherServlet/
创建applicationContext.xml(整合Spring和Mybatis)
<?xml version="1.0" encoding="UTF-8"?>
创建Mybatis-config.xml(Mybatis全局配置文件)
<?xml version="1.0" encoding="UTF-8"?>
springmvc.xml(SpringMVC配置文件)
<?xml version="1.0" encoding="UTF-8"?>
创建log4j.properties(日志文件)
# Set root category priority to INFO and its only appender to CONSOLE.#log4j.rootCategory=INFO, CONSOLE debug info warn error fatallog4j.rootCategory=info, CONSOLE, LOGFILE# Set the enterprise logger category to FATAL and its only appender to CONSOLE.log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE# CONSOLE is set to be a ConsoleAppender using a PatternLayout.log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m# LOGFILE is set to be a File appender using a PatternLayout.log4j.appender.LOGFILE=org.apache.log4j.FileAppenderlog4j.appender.LOGFILE.File=d:logsaxis.loglog4j.appender.LOGFILE.Append=truelog4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m
创建User实体类
package com.bigjava18.web.domain;/** * 用户实体类 */public class User { private Integer uid; //用户uid private String username; //用户名称 private String password; //用户密码 private String sex; //用户性别 private String phone; //用户号码 private String address; //用户地址 public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "uid=" + uid + ", username='" + username + ''' + ", password='" + password + ''' + ", sex='" + sex + ''' + ", phone='" + phone + ''' + ", address='" + address + ''' + '}'; }}
创建用户Controller
package com.bigjava18.web.controller;import com.bigjava18.web.domain.User;import com.bigjava18.web.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * 用户Controller */@Controller@RequestMapping(path = "/user")public class UserController { @Autowired private UserService userService; /** * 测试方法 * @return */ @RequestMapping(path = "/testProject") public String testProject(){ System.out.println("Welcome Come To SSM"); return "success"; } /** * 测试添加用户 * @return */ @RequestMapping(path = "/addUserDate") public String addUserDate(User user){ userService.addUserDate(user); System.out.println("添加用户为:"+user); return "success"; }}
创建用户Service接口
package com.bigjava18.web.service;import com.bigjava18.web.domain.User;/** * 用户业务逻辑层接口 */public interface UserService { /** * 测试添加用户 * @param user */ public void addUserDate(User user);}
创建用户Service接口实现类
package com.bigjava18.web.service.impl;import com.bigjava18.web.dao.UserDao;import com.bigjava18.web.domain.User;import com.bigjava18.web.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * 用户业务逻辑层实现类 */@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserDao userDao; /** * 测试添加用户 * @param user */ @Override public void addUserDate(User user) { userDao.addUserDate(user); }}
创建用户数据库访问接口
package com.bigjava18.web.dao;import com.bigjava18.web.domain.User;/** * 用户数据库访问接口 */public interface UserDao { /** * 测试添加用户 * @param user */ public void addUserDate(User user);}
创建UserDaoMapper.xml代理
insert into user (username,password,sex,phone,address) values (#{username},#{password},#{sex},#{phone},#{address})
测试结果
测试添加用户
测试结果