使用ioc集成Mybatis和spring,像一个框架
把Mybatis框架中的对象交给spring同一创建,开发人员从spring中获取对象。开发人员就不用同时面对两个或多个框架,只需要 面对一个spring。
mybatis使用步骤,对象
1、定义dao接口,StudentDao
2、定义mapper文件 StudentDao.xml
3、定义Myubatis的主配置文件mybatis.xml
4、创建dao的代理对象
StudentDao dao=SqlSession.getMapper(StudentDao.class);
List<Student> students=dao.selectStduents();
要使用dao对象,需要使用getMapper()方法
要使用getMapper()方法,需要哪些条件
1、获取SqlSession对象,需要使用SqlSessionFactory的openSession()方法
2、创建SqlSessionFactory对象。通过Mybatis的主配置文件,创建这个对象。
主配置文件
我们会使用独立的连接池替换mybatis默认自己代的,把连接池类交给spring创建
<!-- 指定properties文件的位置,从类路径根(target/classes/)开始找文件-->
<properties resource="jdbc.properties"></properties>
<!-- defalt表示要使用哪个环境,得和environment的id一样-->
<environments default="development">
<environment id="development">
<!-- 事务类型:表示使用JDBC中的connection对象-->
<transactionManager type="JDBC"/>
<!-- dataSouce数据源,pooled使用连接池-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- sql映射文件的位置-->
<mappers>
<mapper resource="org/example/Dao/StudentDao.xml"/>
</mappers>
综上,我么需要spring创建以下对象
1、对立的连接池类对象,这里使用阿里的druid连接池
2、SqlSessionFactory对象
3、创建出dao对象
实现
第一步 加入依赖
spring、mybatis、mysql驱动、spring事务、mybatis和spring的集成依赖
<dependencies>
<!-- 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mysql-spring集成依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--阿里连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<!--Mysqlq驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--spring基于注解-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
第二步:创建实体类
构造函数、setter、getter、还有toString略
public class Student {
//目前要求属性名和列名一样
private Integer id;
private String name;
private String email;
private Integer age;
…………
…………
…………
…………
}
第三步:创建dao接口和Mapper文件
这里采用注解的方式,不用Mapper文件
package org.example;
import org.apache.ibatis.annotations.Select;
public interface StudentDao {
@Select("select * from student where id =#{studentId};")
public Student selectStudentbyid(Integer id);
}
第四步:创建Mybatis主配置文件
因为使用注解的方式,所以Mybatis主配置文件没什么要写的
<?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>
</configuration>
第五步:创建spring的配置文件:声明Mybatis的对象交给spring创建
1) 数据源
2)SqlSessionFactory
3)Dao对象
4)声明自定义的service
<?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:aop="http://www.springframework.org/schema/aop"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引用属性文件-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 数据源声明DataSource,连接数据库-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="clone">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--声明的是Mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源,ref是id-->
<property name="dataSource" ref="myDataSource"/>
<!-- mybatis主配置文件的位置-->
<property name="configLocation" value="mybatis.xml"/>
</bean>
<!-- 创建dao对象,使用SqlSession的getmapper(Studentdao.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定SqlSessionFactory的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,dao接口所在的包名,MapperScannerConfigurer
会扫描这个包中的所有接口,把每个接口都执行一次getMapper()方法,
得到每个接口的dao对象,默认名称是接口名首字母小写 -->
<property name="basePackage" value="org.example.dao"/>
</bean>
<bean id="studentService" class="org.example.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
</beans>
测试
String config="spring_total.xml";
ApplicationContext ctx=new ClassPathXmlApplicationContext(config);
StudentService studentService = (StudentService) ctx.getBean("studentService");
Student student = studentService.search(6);
System.out.println(student);