1.导入依赖
可以使用maven导入依赖包,
<<maven的一些常用dependency>>
文章中表明了ssm需要的依赖,可以参考一下
也可以不使用maven,自己手动导入包
2.配置maven
具体怎么配置mybatis-config.xml全局配置文件可以看
跳转到:Mybatis基本使用及其配置
跳转到:mybatis缓存
不过因为是整合ssm,spring可以全权管理mybatis,所以mybatis-config.xml可有可无.
留下mybatis-config.xml可以做一些不常用和经常修改的数据的设置.
在工程路径下
创建mybatis-config.xml全局配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!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="jdbcTypeForNull" value="NULL"/>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<databaseIdProvider type="DB_VENDOR">
<property name="Mysql" value="mysql"/>
</databaseIdProvider>
</configuration>
和dbconfig.properties数据库连接配置文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/aaa?useSSL=false&serverTimezone=UTC
username=root
password=123456
3.配置spring mvc
到WEB-INF下的web.xml中添加
使得DispatcherServlet接受所有请求
<!--springMVC配置-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
到WEB-INF下添加spring-servlet.xml (DispatcherServlet的name-servlet.xml)
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--只扫描控制器-->
<context:component-scan base-package="com.pt">
<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注解驱动,
在spring中一般采用@requestMapping注解完成映射关系
想要使用@requestMapping,必须注册一个DefaultAnnotationHandlerMapping和DefaultAnnotationHandlerAdapter
这两个实例分别在类级别和方法级别处理,
而annotation-driven配置帮我们完成了上面的两个实例的注册
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--让springmvc不处理静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
4.配置spring
到WEB-INF下的web.xml中添加
添加Spring核心监听,指定配置文件
<!--Spring配置-->
<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>
到类路径下applicationContext.xml
<?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:cotext="http://www.springframework.org/schema/context"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!--spring管理所有业务逻辑-->
<cotext:component-scan base-package="com.pt">
<cotext:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</cotext:component-scan>
<!--引入数据库资源文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<!--配饰c3p0数据源,事务控制,AOP-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driver}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--spring事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启基于事务的注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--整合mybatis
目的:1.spring管理所有组件.mapper的实现类,@Autowired自动注入mapper
2.spring管理事务
-->
<!--创建sqlSessionFactory对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<!--以前mybatis全局配置文件的内容都可以在这写成property -->
<property name="dataSource" ref="dataSource"></property>
<!--mybatis全局配置文件,不写的话就不需要mybatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--指定mapper的位置,否则mapper.xml需要和mapper接口在一个路径下-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>
<!--扫描所有mapper接口,自动注入-->
<mybatis-spring:scan base-package="com.pt.dao"></mybatis-spring:scan>
<!--或使用以前的一种写法 扫描所有mapper接口,自动注入-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.pt.dao"></property>
</bean>
-->
</beans>
接下来就可以使用ssm了