欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring09_整合MyBatis

程序员文章站 2022-05-08 12:50:40
...

整合MyBatis需要用到mybatis-spring.jar包,整合后就不需要MyBatis核心配置文件了,MyBatis-Spring 需要以下版本,一定要注意版本不然会出现一些令人摸不着头脑的异常

Spring09_整合MyBatis

MyBatis-Spring MyBatis Spring 框架
2.0 3.5+ 5.0+
1.3 3.4+ 3.2.2+

编写代码顺序

在pom.xml中添加依赖编写实体类User.java,编写对应的接口UserMapper.java,编写映射文件UserMapper.xml,编写Spring配置文件ApplicationContext.xml,编写实现类UserDao并且在Spring配置文件中创建Bena,最后测试

<dependencies>
        <!--Junit4-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--MyBatis核心包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>

        <!--MySQL驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>

        <!--Spring相关-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <!--MyBatis整合包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

</dependencies>
    <!--资源过滤-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

实体类

public class User {
    private int id;
    private String name;
    private String pwd;
    //get、set和toString方法
}

接口

public interface UserMapper {
    List<User>findAll();
}

对应的映射文件

<?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="com.Mapper.UserMapper">
    <select id="findAll" resultType="com.pojo.User">
        select * from user;
    </select>
</mapper>

SQL映射文件

<?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="com.Mapper.UserMapper">
    <select id="findAll" resultType="com.pojo.User">
        select * from user;
    </select>
</mapper>

Spring配置文件

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的-->
    <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/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--创建SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--关联Mybatis-->
        <property name="configLocation" value="classpath:MyBatis_config.xml"/>
        <property name="mapperLocations" value="classpath:UserMapper.xml"/>
    </bean>

   <!--创建sqlSessionTemplate , 关联sqlSessionFactory-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--利用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <!--创建UserDao-->

    <bean id="userDao" class="com.Mapper.UserDao">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>

 UserDao编写完就可以在Spring配置文件里绑定

public class UserDao  implements UserMapper {
    //sqlSession不用创建了,Spring来管理
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> findAll() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.findAll();
    }
}

测试

@Test
    public void  Test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserMapper mapper = (UserMapper) context.getBean("userDao");
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }