咱們都知道隨着技術的進步,SSM框架很是火熱,那麼對於咱們來講就得搞清楚其中整合配置的流程,以便於提升咱們的開發效率而且在咱們在程序中出現Bug時能夠快速定位,下面就SSM框架中的配置流程(基於配置文件)做出說明,若有不當之處,敬請你們指正。html
首先咱們要知道SpringMVC是基於MVC的框架Web層框架,解決了前端頁面和後端代碼的分離問題,實現了一個請求對應一個方法。前端
要使用SpringMVC第一步首先要導入所必要的jar包,SpringMVC必要的jar包爲: 4個spring核心包+1個日誌包+2個springmvc包(web,webmvc)+1個aop註解包。java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="${pageContext.request.contextPath }/say.do">點擊發送請求say</a> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <!-- 配置核心控制器攔截全部的請求 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定配置文件的路徑 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
package com.xkt.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller //組件註解 public class HelloController { @RequestMapping(value="/say") public String say(){ System.out.println("HelloWorld!"); return "/hello.jsp"; } }
<?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" 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"> <!-- 配置組件掃描器 --> <context:component-scan base-package="com.xkt.controller"></context:component-scan> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 你好世界 </body> </html>
說明: 1.SpringMVC默認狀況下是線程不安全的 2.配置核心控制器後能夠在web.xml攔截全部的請求,實現一個請求對應一個方法。 3.在配置文件中,咱們配置< init-parm >是但願指定自定義的配置文件路徑。若是使用框架默認的路徑(放到WEB-INF下,且命名爲dispatcherServlet-servlet.xml)能夠不配置 4.配置< context:component-scan >是由於咱們是經過組件註解(@Controller)的方式,將類的對象加載到容器裏面。因此必需要配置。mysql
咱們要知道Mybatis是一個持久層框架。可以對數據庫進行增刪改查操做,而且是用少許的代碼就能夠實現對數據庫的操做,能夠大大提升開發效率。web
1個Mybatis包+1個數據庫驅動包spring
注意:Mybatis框架的配置文件使用DTD規範文件的。因此須要經過DTD規則文件生成。(xml的約束文件DTD 以及 schema)sql
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" > <configuration> <environments default="sms"> <!-- 任何配置文件,的參數均可以在框架代碼裏面找到!! --> <!-- 大部分的框架,對配置文件的解釋的所在類的分包的包名,configuration以及縮寫、builder以及縮寫 --> <environment id="sms"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <!-- property 對應的就是set方法--> <property name="driver" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/sms"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
package com.xkt.utils; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtils { public static final SqlSessionFactory SSF=MybatisUtils.getSSF(); private static final ThreadLocal<SqlSession> THREAD_LOCAL=new ThreadLocal<>(); /** * 得到會話工廠 * @return */ private static SqlSessionFactory getSSF() { try { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); return builder.build(reader); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 得到會話 * @return */ public static SqlSession getSession(){ if(THREAD_LOCAL.get()==null){ SqlSession session = SSF.openSession(); THREAD_LOCAL.set(session); } return THREAD_LOCAL.get(); } /** * 關閉會話 */ public static void close(){ if(THREAD_LOCAL.get()!=null){ SqlSession session = THREAD_LOCAL.get(); session.close(); THREAD_LOCAL.remove(); } } public static void main(String[] args) { System.out.println(MybatisUtils.getSession()); } }
package com.xkt.pojo; public class Student { private String stuId;//BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '學生編號', private String stuName;//VARCHAR(50) NULL DEFAULT NULL COMMENT '學生名字', private String stuAge;//INT(11) NULL DEFAULT NULL COMMENT '學生年齡', private String stuPassword;//VARCHAR(50) NULL DEFAULT NULL COMMENT '密碼', public String getStuId() { return stuId; } public void setStuId(String stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuAge() { return stuAge; } public void setStuAge(String stuAge) { this.stuAge = stuAge; } public String getStuPassword() { return stuPassword; } public void setStuPassword(String stuPassword) { this.stuPassword = stuPassword; } }
packagecom.xkt.mapper; import com.xkt.pojo.Student; public interface StudentMapper { /** * 插入學生 * @param student * @return */ int insert(Student student); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" > <mapper namespace="com.xkt.mapper.StudentMapper"> <insert id="insert"> INSERT INTO tb_student (stu_name, stu_age, stu_password) VALUES (#{stuName}, #{stuAge}, #{stuPassword}) </insert> </mapper>
package com.xkt.test.mapper; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.xkt.mapper.StudentMapper; import com.xkt.pojo.Student; import com.xkt.utils.MybatisUtils; public class StudentMapperTest { @Test public void insert(){ //得到操做對象 SqlSession session = MybatisUtils.getSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); Student student=new Student(); student.setStuName("張三"); int count = studentMapper.insert(student); System.out.println(count); session.commit(); MybatisUtils.close(); } }
SpringMVC整合Mybatis就是,就是讓Mybatis拋棄本身實現的數據源,再使用Spring提供的數據源。(由於咱們要讓Mybatis使用Spring框架裏面的事務代理機制,Spring框架的事務代理依賴Spring JDBC裏面的數據源)數據庫
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 1.配置數據源 --> <bean name="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <!-- 四要素 --> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/sms" /> <property name="username" value="root" /> <property name="password" value="1234" /> </bean> <!-- 2.配置會話工廠 --> <!-- 默認狀況下:mybatis是不支持spring的數據源的 --> <!-- 問題:那麼咱們如何可讓mybatis支持spring的數據源呢? --> <!-- 答:須要一個整合包 mybatis-spirng.jar SqlSessionFactoryBean:做用就是讓Mybatis能夠經過Spring的數據源建立會話工廠的 --> <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 加載映射文件的路徑 --> <property name="mapperLocations" value="classpath:com/xkt/mapper/xml/*Mapper.xml"></property> </bean> <!-- 3.配置掃描器,將映射接口的動態對象建立,而且注入到spring容器裏面 --> <!-- 默認狀況下:spring是不支持經過接口建立對象!!而Mybatis就是經過接口建立對象的 問題:Spring必需要實現類建立能夠注入到容器,而Mybatis就是使用接口建立動態對象的。不能兼容Spring的要求。怎麼辦呢? 答:整合包提供了一個映射接口掃描器,用於經過映射接口建立了對象,而且能夠建立的對象注入到容器裏面 -根據表述掃描器必須要的兩個條件 1.須要會話工廠 2.必需要指定映射接口的路徑 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.xkt.mapper"></property> <!-- 指定掃描註解 --> <property name="annotationClass" value="org.apache.ibatis.annotations.Mapper"></property> </bean> <!-- 4.配置事務代理,編程式事務 --> <!-- 注意:Mybatis是的spring jdbc的事務代理 --> <!-- 建立事務代理對象 --> <bean name="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 指定數據源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 啓動事務代理 --> <tx:annotation-driven/> </beans>