SSM(Spring+SpringMVC+MyBatis)框架集由 **Spring ** 、**MyBatis **两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
Spring就像是整个项目中 装配bean的大工厂 ,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。
Spring的核心思想是 IoC(控制反转),即不再需要程序员去显式地new
一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC在项目中拦截用户请求,它的核心Servlet即 DispatcherServlet 承担中介或是前台这样的职责,将用户请求通过 HandlerMapping 去匹配 Controller ,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis是对 jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个 sqlSessionFactory 实例展开的。mybatis通过配置文件关联到各实体类的 Mapper 文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
在第一章maven项目的基础上,继续配置spring和mybatis的相关内容,完成SSM的搭建
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<mvc:annotation-driven>mvc:annotation-driven>
<context:component-scan
base-package="com.gonna.controller">
context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="view/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:default-servlet-handler />
为了提高代码的可维护性可以将数据库信息单独写在“字典文件”1中,在applicationContext.xml中引用就好
<context:component-scan
base-package="com.gonna.dao">context:component-scan>
<context:component-scan
base-package="com.gonna.service">context:component-scan>
<context:property-placeholder
location="classpath:db.properties" />
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}">property>
<property name="jdbcUrl" value="${url}">property>
<property name="user" value="${user}">property>
<property name="password" value="${password}">property>
<property name="maxPoolSize" value="100">property>
bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config.xml">property>
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations">
<list>
<value>classpath:mapper/*.xmlvalue>
list>
property>
bean>
<bean id="scannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gonna.dao">property>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven
transaction-manager="transactionManager" />
<configuration>
configuration>
mybatis的逆向工程工具为我们自动生成了许多常用的sql语句,在这里我们自己写一个容易测试的
<mapper namespace="com.gonna.dao.StudentMapper">
<resultMap id="BaseResultMap" type="com.gonna.domain.Student">
<id column="sno" property="sno" jdbcType="INTEGER" />
<result column="sname" property="sname" jdbcType="VARCHAR" />
<result column="ssex" property="ssex" jdbcType="VARCHAR" />
<result column="sage" property="sage" jdbcType="INTEGER" />
<result column="stel" property="stel" jdbcType="VARCHAR" />
<result column="shome" property="shome" jdbcType="VARCHAR" />
<result column="pid" property="pid" jdbcType="INTEGER" />
<result column="cid" property="cid" jdbcType="INTEGER" />
resultMap>
<sql id="Base_Column_List">
sno, sname, ssex, sage, stel, shome, pid, cid
sql>
<select id="selectStudents" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
select>
mapper>
至此SSM的基本配置也就完成了,我们需要写对应的测试类和页面
实体类MyBatis generator已为我们建好,我们可以给它加一个toString()方法便于测试
dao也已自动建好,在这里做一点小改动
package com.gonna.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.gonna.domain.Student;
@Repository
public interface StudentMapper {
// @Autowired
List<Student> selectStudents();//该方法必须和mapper中的select语句id一致!
int deleteByPrimaryKey(Integer sno);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer sno);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
package com.gonna.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gonna.domain.Student;
@Service
public interface StudentService {
// @Autowired
List<Student> selectStudents();
}
package com.gonna.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gonna.dao.StudentMapper;
import com.gonna.domain.Student;
import com.gonna.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
// @Resource ----?有的博文里是这么写的,回头再尝试下
StudentMapper studentMapper;
//generator可以自动生成,自然在这里也可以autowired
@Override
public List<Student> selectStudents() {
return studentMapper.selectStudents();
}
}
package com.gonna.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gonna.domain.Student;
import com.gonna.service.StudentService;
@Controller
public class StudentController {
@Autowired
// @Resource
private StudentService studentService;
@RequestMapping("getStudents")
public String getStudents(ModelMap model) {
List<Student> list = studentService.selectStudents();
model.addAttribute("students", list);
for (Student s : list) {
System.out.println(s);
}
//控制台看看Student对象是否拿到
return "showstudents";
}
}
最后写一个页面显示效果,为了方便以后加东西写多了点
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
学号
姓名
性别
年龄
电话
家庭住址
操作
${student.sno }
${student.sname }
${student.ssex }
${student.sage }
${student.stel }
${student.shome }
大功告成!!!还有不足之处还请各位不吝赐教’’’ V ‘’’
参考:
[SSM框架理解]: https://www.cnblogs.com/verlen11/p/5349747.html
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test user=root password=root
↩︎