MyBatis与Spring的整合实例详解

MyBatis与Spring的整合可以提高开发效率,这里给出一个详细的整合实例:

1. 创建数据库表和实体类。这里以Student表和Student实体类为例。

2. 创建MyBatis的配置文件SqlMapConfig.xml。配置数据源、事务管理器和SQL映射文件位置等。

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/demo_database"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>         
            </dataSource>
        </environment>
    </environments>  
    <mappers>
        <mapper resource="org/mybatis/example/StudentMapper.xml"/>
    </mappers>
</configuration> 

3. 创建MyBatis的SQL映射文件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="org.mybatis.example.StudentMapper">
    <select id="selectStudent" resultType="Student">
      select * from Student where id=#{id}
    </select>
</mapper>

4. 在Spring配置文件中配置MyBatis。

<?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"
    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">  

    <!-- 配置整合mybatis -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/demo_database"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>       
    </bean> 
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 
    </bean>
</beans> 

5. 测试使用。编写测试类,通过SqlSessionTemplate从SqlSessionFactory中获取SqlSessioin,然后执行SQL查询。

java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MyBatisTest {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Test
    public void testSelectStudent() {
        Student student = sqlSessionTemplate.selectOne("org.mybatis.example.StudentMapper.selectStudent", 1);
        System.out.println(student.getName());
    }
}

以上就是MyBatis与Spring进行整合的详细实例。整合完成后,我们可以使用Spring来管理MyBatis,享受到Spring的DI和AOP等特性,更加方便进行系统开发。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论