spring-mybatis整合

看完狂神视频没有整理笔记,直接把老师笔记粘过来吧。。。


 

导入相关jar包,版本自己看着办

junit

<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-connector-java

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</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>

aspectJ AOP 织入器

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.9.4</version>
</dependency>

mybatis-spring整合包 【重点】

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.2</version>
</dependency>

配置Maven静态资源过滤问题!

<build>
   <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>true</filtering>
       </resource>
   </resources>
</build>

 

实体类

@Data  //此处使用了lombok,需要对应的jar
public class User {
    private int id;
    private String username;
    private int age;
}

mapper/dao

import com.wpz.pojo.User;

import java.util.List;

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

增加Dao接口的实现类;私有化sqlSessionTemplate

public class UserMapperImpl implements UserMapper{
    private SqlSessionTemplate sqlSession;

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

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

测试类

    @org.junit.Test
    public void test() throws IOException {
        String resouce = "mybatisConfig.xml";
        InputStream in = Resources.getResourceAsStream(resouce);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.getUser();
        for (User user:list) {
            System.out.println(user);
        }
        sqlSession.close();
    }

整合实现一 :配置

spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 1 引入Spring配置文件beans.xml-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 2 使用Spring数据源 替换mybatis数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai"/>
            <property name="username" value="root"/>
            <property name="password" value="admin"/>
        </bean>
<!-- 3   sqlSessionfactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
<!-- 3          绑定mybatis配置文件-->
            <property name="configLocation" value="classpath:mybatisConfig.xml"/>
            <property name="mapperLocations" value="classpath:com/wpz/dao/UserMapper.xml"/>
        </bean>

<!--  4  注册sqlSessionTemplate,关联sqlSessionFactory;-->
       <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--  使用构造器注入-->
           <constructor-arg index="0" ref="sqlSessionFactory"/>
       </bean>
<!-- 注册bean实现 -->
    <bean id="userImpl" class="com.wpz.dao.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
    <bean id="userImpl2" class="com.wpz.dao.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

整合实现二 :继承SqlSessionDaoSupport

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{

    @Override
    public List<User> getUser() {
        return getSqlSession().getMapper(UserMapper.class).getUser();
    }
}

修改bean的配置

    <bean id="userImpl2" class="com.wpz.dao.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

测试类

    @org.junit.Test
    public void testSpring(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper mapper = context.getBean("userImpl2", UserMapperImpl2.class);
        for (User user:mapper.getUser()) {
            System.out.println(user);
        }
    }

总结 : 整合到spring以后可以完全不要mybatis的配置文件,除了这些方式可以实现整合之外,我们还可以使用注解来实现。待续...

 

 

 
posted @ 2021-03-11 14:31  少时也曾爱白衣  阅读(50)  评论(0)    收藏  举报