文章目录
spring-mybatis整合步骤
为什么要整合
如果没有整合?
在mybatis种,操作数据库需要获取SQLSession对象,而此对象是用过SQLSessionFactoryBuilder读取全局配置文件来实例化一个SQLSessionFactory,通过SQLSessionFactory来获取到SQLSession对象。
而用到整合,可以通过单例的形式来管理SQLSessionFactory对象,mybatis-spring中提供了一个SQLSessionFactoryBean类来实例化SQLSessionFactory,SQLSessionFactory中需要必须的属性是DataSource以及Mapper
整合步骤
引入依赖
需要引入mybatis提供的整合jar包 mybatis-spring
<!--mybatis和spring的整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
mybatis
mybatis全局配置
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>
<!--配置懒加载、二级缓存 Setting配置需要该文件-->
<!--配置数据源,在Spring整合过程中,数据源交给Spring管理-->
<!--配置Mapper ,也是交给Spring管理-->
</configuration>
pojo类Student类
public class Student1 {
private Integer SID;
private String Sname;
private Integer Ssex;
private Integer Sage;
get/set/tostring
}
dao层StudentMapper接口
public interface StudentMapper {
public Student1 selectStudentById(Integer id);
}
StudentMapper.xml配置
<?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,命令空间:保证命名空间唯一,一般是对应的mapper.java的包全路径-->
<mapper namespace="com.nsh.dao.StudentMapper">
<select id="selectStudentById" parameterType="int" resultType="com.nsh.pojo.Student1">
select * from student where SID = #{id}
</select>
</mapper>
spring配置
创建spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--配置连接池c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<!--配置数据连接的核心配置-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/text_db"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--获取会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--加载mybatis的mapper配置-->
<property name="mapperLocations" value="mapper/*.xml"/>
</bean>
<!--获取StudentMapper的代理对象-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--会话工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--指定接口文件位置-->
<property name="mapperInterface" value="com.nsh.dao.StudentMapper"/>
</bean>
</beans>
使用spring容器管理对象
public class TextDemo {
public static void main(String[] args) {
String path = "spring-mybatis.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
//从IOC中获取需要的对象实例
StudentMapper studentMapper = (StudentMapper) applicationContext.getBean("studentMapper");
Student1 student = studentMapper.selectStudentById(1);
System.out.println(student);
}
}
需要注意你的数据库名和数据库中的表要对应好