zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    Hackerrank--Savita And Friends(最小直径生成树MDST)
    Hackerrank--Kundu and Tree
    Hackerrank--String Function Calculation(后缀数组)
    Hackerrank--Ashton and String(后缀数组)
    Foundation 学习
    JS 严格模式
    判断移动设备横竖屏
    Nodejs解析HTML网页模块 jsdom
    百度Map调用
    Jade 报错
  • 原文地址:https://www.cnblogs.com/wang-sky/p/13625898.html
Copyright © 2011-2022 走看看