Spring+Mybatis整合
整合基础
使用的技术:IOC
原因:IOC能把Mybatis和Spring继承在一起。可以把Mybatis框架中的对象交给Spring统一创建,开发人员从Spring中获取对象。
Mybatis
-
Mybatis使用步骤:
1.定义Dao接口(例:StudentDao)
2.定义mapper文件(例:StudentDao.xml)
3.定义Mybatis主配置文件(例:mybatis.xml)
4.创建Dao的代理对象(例:StudentDao studentDao = SqlSession.getMapper(StudentDao.class);List list = studentDao .selectStudents();) -
getMapper()使用条件:
1.获取SqlSession对象,需要使用SqlSessionFactory的openSession()方法。
2.创建SqlSessionFactory对象,通过读取Mybatis的主配置文件创建。 -
Mybatis主配置文件的内容:
1.数据库信息
2.mapper文件的位置
Spring
- 1.连接池
Mybatis自带的连接池性能比较差,最好整合其它性能优秀的连接池(druid连接池)。 - 2.创建SqlSessionFactory对象
- 3.创建Dao对象
Spring整合Mybatis
将Mybatis的对象交给Spring创建
步骤
- 1.加入依赖
1)Spring依赖
2)Mybatis依赖
3)数据库驱动
4)Spring的事务依赖
5)Spring与Mybatis集成的依赖(用来在Spring项目中创建Mybates的SqlSessionFactory、dao对象) - 2.创建实体类
- 3.创建dao接口和mapper文件
- 4.创建Mybatis主配置文件
- 5创建Service接口和实现类
- 6.创建Spring的配置文件:声明Mybatis的对象交给Spring创建
1)数据源
2)SqlSessionFactory对象
3)Dao对象
4)声明定义的Service - 7.使用(创建service对象,通过service调用dao完成数据库的访问)
实例
- 1.创建实体类
@Component
public class Student {
private int id;
private String name;
private int age;
private String email;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
- 2.定义Dao接口
public interface StudentDao {
int insertSrudent(Student student);
List<Student> selectStudent();
}
- 3.创建Service
public interface StudentService {
int addStudent(Student student);
List<Student> selectStudent();
}
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public int addStudent(Student student) {
int num = studentDao.insertSrudent(student);
return num;
}
@Override
public List<Student> selectStudent() {
List<Student> list = studentDao.selectStudent();
return list;
}
}
- 4.定义mapper文件
<select id="selectStudent" resultType="com.xqj.pojo.Student">
select id,name,age,email from student order by id desc
</select>
<!-- 向列表中插入Student -->
<insert id="insertSrudent">
insert into student (name,age,email) values(#{name},#{age},#{email})
</insert>
- 5.定义Mybatis主配置文件
<configuration>
<!-- settings:控制mybatis全局行为 -->
<settings>
<!-- 设置mybatis输出日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 设置别名 -->
<typeAliases>
<!-- name:实体类包的所在目录 -->
<package name="com.xqj.pojo"/>
</typeAliases>
<!-- sql mapper(sql映射文件)的位置-->
<mappers>
<!-- <mapper resource=""/>-->
<!-- 加载name属性值的目录下所有xml文件 -->
<package name="com.xqj.dao"/>
</mappers>
</configuration>
- 6.Spring中加入依赖
//配置文件的内容(config.properties)
postgresql_url=jdbc:postgresql://localhost:5432/study
postgresql_name=postgres
postgresql_password=postgres
postgresql_maxActive=20
//Spring配置文件(applicationContext.xml)
<!-- 注入配置文件 -->
<context:property-placeholder location="classpath:config.properties"/>
<!-- 声明数据源DataSource,用于连接数据库 (使用的Druid线程池) -->
<bean id="postgresql" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 通过set注入的方式对DruidDataSource信息进行注入 -->
<property name="url" value="${postgresql_url}"/>
<property name="username" value="${postgresql_name}"/>
<property name="password" value="${postgresql_password}"/>
<property name="maxActive" value="${postgresql_maxActive}"/>
</bean>
<!-- 声明mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory对象(提供了数据库操作的方法) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 将需要的数据源赋值给sqlSessionFactory对象的dataSource属性。通过set注入的方式赋值 -->
<property name="dataSource" ref="postgresql" />
<!-- Spring中需要将所有外部文件注入,configLocation属性是Resource类型,用于读取配置文件
在创建sqlSessionFactory时,会将引入的外部配置文件都注入
-->
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
</bean>
<!-- 创建dao对象,使用SqlSession的getMapper方法
MapperScannerConfigurer:在内部调用getMapper()生成每个dao接口的代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定SqlSessionFactory对象的id -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定包名,需要注入的dao接口的包目录。多个可以用,间隔
MapperScannerConfigurer会扫描注入的所有包中的所有dao接口,
并且每一个dao接口都会调用一个getMapper(),从而创建对应的dao对象,放入Spring的容器中
-->
<property name="basePackage" value="com.xqj.dao"/>
</bean>
<!-- 添加Service层的对象 -->
<bean id="studentService" class="com.xqj.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
- 7.测试类
@Test
public void test01(){
String config = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
StudentDao studentDao = (StudentDao)applicationContext.getBean("studentDao");
List<Student> list = studentDao.selectStudent();
System.out.println(list);
}