Spring-整合MyBatis

前端之家收集整理的这篇文章主要介绍了Spring-整合MyBatis前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

整合MyBatis

1. 步骤

1. 导入相关jar包

  • junit

  • mybatis

  • MysqL数据库

  • spring相关的

  • aop织入

  • mybatis-spring [new]

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>Spring-Study</artifactId>
            <groupId>com.wang</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>spring-10-mybatis</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
            </dependency>
            <dependency>
                <groupId>MysqL</groupId>
                <artifactId>MysqL-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <!--spring操作数据库的话,还需要有个spring-jdbc-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.6</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.5</version>
            </dependency>
        </dependencies>
    
    </project>
    

2. 编写配置文件

3. 测试

2. Mybatis-Spring

  1. 编写数据源
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 需要给接口加实现类
  5. 将自己写的实现类注入到Spring中
  6. 测试使用

1. User实体类

package com.wang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private String pwd;
}

2. UserMapper接口

package com.wang.mapper;

import com.wang.pojo.User;

import java.util.List;

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

3. UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.UserMapper">

    <select id="queryUser" resultType="User">
        select * from user;
    </select>

</mapper>

4. Spring-dao.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.xsd">

    <!--DataSource: 使用Spring的数据源替换Mybatis的配置
        我们这里使用Spring提供的JDBC: org.springframework.jdbc.datasource.DriverManagerDataSource
    -->
    <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=false&amp;useUnicodeEncode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </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:com/wang/mapper/*.xml"/>
    </bean>

    <!--sqlSessionTemplate: 就是我们使用的sqlSession!-->
    <bean id="sqlSession" class="org.mybatis.spring.sqlSessionTemplate">
        <!--只能用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

</beans>

5. 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 核心配置文件-->
<configuration>

    <!--设置管理-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!--别名管理-->
    <typeAliases>
        <package name="com.wang.pojo"/>
    </typeAliases>

</configuration>

6. applicationContext.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.xsd">

    <import resource="Spring-dao.xml"/>

    <bean id="userMapper" class="com.wang.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

</beans>

7. UserMapperImpl实现类

package com.wang.mapper;

import com.wang.pojo.User;
import org.mybatis.spring.sqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{

    //原来(只用Mybatis); 我们的所有操作都使用sqlSession来执行
    //现在(Spring整合Mybatis): 我们使用sqlSessionTemplate
    private sqlSessionTemplate sqlSession;

    //为了在Spring中注入,这里要写一个Setter
    public void setsqlSession(sqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

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

8. 测试

import com.wang.mapper.UserMapper;
import com.wang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class MyTest {
    @Test
    public void test01 () {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
        List<User> userList = userMapper.queryUser();
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

3. 使用sqlSessionDaoSupport

接口和实体类与上面一致

  1. 配置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.xsd">

    <!--DataSource: 使用Spring的数据源替换Mybatis的配置
        我们这里使用Spring提供的JDBC: org.springframework.jdbc.datasource.DriverManagerDataSource
    -->
    <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=false&amp;useUnicodeEncode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </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:com/wang/mapper/*.xml"/>
    </bean>

</beans>
  1. 写UserMapperImpl的实现类

    注意,此处要继承sqlSessionDaoSupport!

package com.wang.mapper;

import com.wang.pojo.User;
import org.mybatis.spring.support.sqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends sqlSessionDaoSupport implements UserMapper{

    @Override
    public List<User> queryUser() {
        return getsqlSession().getMapper(UserMapper.class).queryUser();
    }
}
  1. 在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="Spring-dao.xml"/>

    <!--UserMapperImpl继承的父类中要求注入sqlSessionFactory属性-->
    <bean id="userMapper2" class="com.wang.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>
原文链接:https://www.f2er.com/spring/883262.html

猜你在找的Spring相关文章