前言
SSM整合分两步走,当前为第一步 Mybatis 和Spring的整合,第二步请看 SSM之整合SpringMVC
SSM 之 Mybatis 和Spring的整合
工程整体结构概览:
-
创建maven工程,导入web骨架。注:需在src下创建test包和resources包测试使用。
-
引入jar包依赖
<!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--spring相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.12.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!--mybatis与spring的整合包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!--数据库驱动jar--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--druid连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency>
-
创建查询的 数据库表 和 对应的pojo类
数据库表
pojo类
public class Stu { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Stu{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
-
dao层开发:创建mapper层查询数据,创建和mapper相同包路径下的查询文件-xml文件。
mapper
public interface StuMapper { public List<Stu> queryAllStu() throws Exception; }
StuMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lb.mapper.StuMapper"> <select id="queryAllStu" resultType="com.lb.pojo.Stu"> select * from stu </select> </mapper>
-
service层开发:创建service包下的接口和实现类。注意service实现类需要注入ioc容器
service接口
public interface StuService { public List<Stu> findAllStu() throws Exception; }
service实现类
@Service public class StuServiceImpl implements StuService { @Autowired private StuMapper stuMapper; @Override public List<Stu> findAllStu() throws Exception { return stuMapper.queryAllStu(); } }
-
创建数据库连接的properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.username=root jdbc.password=root
-
创建springbean的xml文件(applicationContext.xml),声明要被管理的bean对象有哪些
-
包扫描
<!--包扫描--> <context:component-scan base-package="com.lb"/>
-
引入外部资源文件jdbc.properties,注入数据库连接池datasource
<!--引入外部资源文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
-
bean: 事务管理(添加事务管理注解驱动)
<!--事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--事务管理注解驱动--> <tx:annotation-driven transaction-manager="transactionManager"/>
-
sqlsessionfactory对象
<!--sqlSessionFactory对象放在spring容器中作为单例对象管理--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--别名映射扫描--> <property name="typeAliasesPackage" value="com.lb.pojo"/> <property name="dataSource" ref="dataSource"/> </bean>
-
Mapper动态代理对象
<!--Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象--> <!--扫描mapper接口,生成代理对象,生成的代理对象会存储在ioc容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lb.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
整合完毕的applicationContext.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--包扫描--> <context:component-scan base-package="com.lb"/> <!--引入外部资源文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--事务管理--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--事务管理注解驱动--> <tx:annotation-driven transaction-manager="transactionManager"/> <!--sqlSessionFactory对象放在spring容器中作为单例对象管理--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--别名映射扫描--> <property name="typeAliasesPackage" value="com.lb.pojo"/> <property name="dataSource" ref="dataSource"/> </bean> <!--Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象--> <!--扫描mapper接口,生成代理对象,生成的代理对象会存储在ioc容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lb.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
-
-
测试,加上spring的测试注解
测试代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class MybatisSpringTest { @Autowired private StuService stuService; public void test() throws Exception{ List<Stu> allStu = stuService.findAllStu(); for (Stu stu : allStu) { System.out.println(stu); } } }
测试结果