MyBatis-Spring 简单入门

1、依赖

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
    </dependencies>

2、在spring的xml中配置数据源,创建sqlSession

  固定写法;

  <!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
        这里使用的Spring提供的JDBC:org.springframeword.jdbc.datasource
        -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定MyBatis配置文件-->
        <property name="configLocation" value="classpath:mybatisConfig.xml"></property>
        <!--配置mapper-->
        <property name="mapperLocations" value="classpath:com/doubleh/mapper/*.xml"></property>
    </bean>

    <!--SqlSessionTemplate:就是我们用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <!--注入实现类-->
    <bean class="com.doubleh.mapper.UserMapperImpl" id="userMapperImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>

3、调整mybatis配置文件

  <environments> 和 <mappers>就不需要了;

4、编写实现类

  方式一:

  定义一个SqlSessionTemplate sqlSession参数,并在spring xml中注入 <property name="sqlSession" ref="sqlSession"></property>
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSession;

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

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

 方式二:

  继承 SqlSessionDaoSupport,并在spring xml中注入 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>;
  相比方式一,就少了setSqlSession的定义。
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

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

5、测试

  以spring的方式调用操作;

 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapperImpl userMapperImpl = applicationContext.getBean("userMapperImpl", UserMapperImpl.class);
        List<User> select = userMapperImpl.selectUser();
        for (User user : select) {
            System.out.println(user);
        }

 

posted @ 2020-02-28 20:45  门虫不是虫  阅读(326)  评论(0编辑  收藏  举报