Mybatis整合官方文档
Mybatis整合官方Demo
Spring AOP IOC 12
SpringMVC 2
Spring依赖日志包 1
Mybatis及Mybatis-Spring整合 2
MySQL-Connector 1
JSTL 2
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
mybatis-3.4.1.jar
mybatis-spring-1.3.0.jar
mysql-connector-java-8.0.16.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
taglibs-standard-impl-1.2.1.jar
taglibs-standard-spec-1.2.1.jar
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<spring.version>5.0.2.RELEASEspring.version>
<slf4j.version>1.6.6slf4j.version>
<log4j.version>1.2.12log4j.version>
<mysql.version>5.1.6mysql.version>
<mybatis.version>3.4.5mybatis.version>
properties>
<dependencies>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.6.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>compilescope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
<type>jartype>
<scope>compilescope>
dependency>
dependencies>
Mybatis全局配置文件
此文件通过在Spring配置文件ApplicationContext中的
SqlSessionFactoryBean对象的属性来指定位置。
注:整合后所有Bean对象交给Spring IOC配置
配置数据源的工作交给SpringIOC创建连接池对象附带数据源属性,Mapper配置也转移到Spring配置文件中
其他全局配置,例如“typeAlias的package:为某个包下所有类批量起别名”可作为SQLSessionFactory的属性在Spring
标签中配置
这样就不需要在Mybatis-config.xml配置大量bean(包括mapper)对象
只留一些Mybatis Settings配置,例如数据库标识符别名等。
对每个JavaBean、POJO的独立SQL(CRUD)映射文件
数据库配置文件
注:开启多条SQL合并执行 ?allowMultiQueries=true
标明Spring配置文件路径 (IOC容器与Web服务器一同启动)
标明SpringMVC配置文件路径 (令Servlet与Web服务器一同启动)
提示:如果在eclipse中安裝了Spring IDE插件則這裏的Spring和SpringMVC標簽
直接Alt+/
選擇#ContextLoaderListener
#dispatcherservlet
即可添加
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyBatis_06_ssmdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
默认约定,如果不指名Spring配置路径,以及SpringMVC配置路径,
则Spring配置默认路径:/WEB-INF/applicationContext.xml
SpringMVC配置默认路径:/WEB-INF/servlet-name-servlet.xml
如果想修改SpringMVC配置文件路径则在
加入以下内容
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
Spring配置文件
创建Spring Bean Configuration File类型文件,在config(SourceFolder)路径下
点击xml下方的NameSpaces标签页,可以选择自动注入的名称空间
加入context、tx、mybatis-spirng namespace
<context:component-scan base-package="com.atguigu.mybatis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<mybatis-spring:scan base-package="com.atguigu.mybatis.dao"/>
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSource">property>
bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSource">property>
<property name="configLocation" value="classpath:mybatis-config.xml">property>
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml">property>
bean>
创建数据库配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
jdbc.username=******
jdbc.password=******
创建Spring Bean Configuration File类型文件
在WebContent的WEB-INF目录下与web.xml同级
加入Context namespace以及Mvc namespace
<context:component-scan base-package="com.atguigu.mybatis" use-default-filters="false">
<context:include-filter type="annotation" expression=" org.springframework.stereotype.Controller"/>
context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:default-servlet-handler/>
Mybatis用Mapper interface+XML 代替了QueryRunner.java,并将SQL写在XML中
解决了SQL语句与Java文件的强耦合问题,并且减少大量创建完全实现类的工作
SpringMVC解决页面跳转及处理页面请求request
过程是:页面请求controller,controller调用Service,Service调用Dao的Mapper
SpringMVC解决了Servlet的耦合,以及减少大量重复代码
并在拦截器功能上复现了Spring AOP思想
注意:所有的class="",一定要!!!看清!!!不要有空格!!!