1、导入相应的jar包
2、导入spring配置文件ssm.xml
3、在web.xml中配置DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<servlet>
<servlet-name>springMVC</servlet-name>
<!-- 在<servlet>标签中配置DispatcherServlet的访问路径 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springMVC的地址 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
a. servlet-class的值是一个jar包中的DispatcherServlet类
b. <init-param>标签中的<param-name>标签中写入contextConfigLocation这个值是固定的,他是dispathcherServlet中的一个属性。<param-value>标签中写入classpath:SpringMVC的配置文件地址。
c. 配置<load-on-startup>值为1即在项目启动时创建servlet对象
d. 配置servlet-mapping
3、导入Mybatis的映射文件,文件名就和Dao接口一样就可以
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bb.hrb.dao.DeptDao" >
<insert id="addDept" parameterType="com.oracle.pojo.Dept">
insert into t_dept values(#{deptno},#{dname},#{loc});
</insert>
<delete id="delDept" parameterType="int">
delete from t_dept where deptno=#{deptno};
</delete>
<update id="updateDept" parameterType="com.oracle.pojo.Dept">
update t_dept set dname=#{dname} ,loc=#{loc} where deptno=#{deptno};
</update>
<select id="findByDeptno" parameterType="int" resultType="com.oracle.pojo.Dept">
select * from t_dept where deptno=#{deptno}
</select>
<select id="findAll" resultType="com.oracle.pojo.Dept">
select * from t_dept
</select>
</mapper>
4、MyBatis的主配置文件这里就不导入了,直接在Spring配置文件里配置
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="url" value="jdbc:mysql://localhost:3306/j244?useUnicode=true&characterEncoding=utf-8"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="maxActive" value="30"></property>
<property name="initialSize" value="5"></property>
<property name="maxIdle" value="10"></property>
<property name="minIdle" value="5"></property>
<property name="maxWait" value="3000"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/oracle/hrb/dao/*.xml"></property>
</bean>
<!-- 配置Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.oracle.hrb.dao"></property>
<property name="annotationClass" value="com.oracle.hrb.annotation.MyBatisRepository"></property>
</bean>
<!-- 开启IOC注解 -->
<context:component-scan base-package="com.oracle"></context:component-scan>
<!-- 开启MVC注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
5、现在可以在Service类中使用了,举个栗子
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DeptService {
@Autowired
private DeptDao deptDao;
@Override
public List<Dept> findAll() {
return deptDao.findAll();
}
}