1. 程式人生 > 實用技巧 >Spring-08-整合Mybatis

Spring-08-整合Mybatis

1、學習文件

學習文件

2、所需依賴

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</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.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>

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

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>

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

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>

3、配置檔案

  • spring-dao.xml

    <!--
    DataSource資料來源配置
    sqlSessionFactory
    sqlSession
    -->

    <?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
    https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--DataSource資料來源配置:使用Spring的資料來源來替換Mybatis的配置-->
    <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=UTF-8"/>
    <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:com/hmx/mapper/*.xml"/>
    </bean>

    <!--sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    </beans>
  • 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>

    <typeAliases>
    <typeAlias type="com.hmx.pojo.User"/>
    </typeAliases>

    <settings></settings>

    </configuration>

4、方式一

  • applicationContext.xml

    <!--配置bean以及使用-->

    <?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
    https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring.xml"/>

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

    </beans>
  • 實體類:User

    @Data
    public class User {

    private int id;
    private String name;
    private String pwd;
    }
  • 介面類:UserMapper.java

    public interface UserMapper {
    public List<User> selectUser();
    }
  • Mybatis的xml檔案:UserMapper.xml

    <mapper namespace="com.hmx.mapper.UserMapper">
    <select id="selectUser" resultType="user">
    select * from mybatistest
    </select>
    </mapper>
  • 介面類的實現類:UserMapperImpl.java

    public class UserMapperImpl implements UserMapper {

    //和之前不同,我們現在所有操作都使用SqlSessionTemplate來執行
    private SqlSessionTemplate sqlSessionTemplate;

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    this.sqlSessionTemplate = sqlSessionTemplate;
    }

    public List<User> selectUser() {
    UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
    return mapper.selectUser();
    }
    }
  • 測試

    public static void selectTest() {
    
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper sqlSession = context.getBean("userMapper", UserMapper.class);
        for (User user : sqlSession.selectUser()) {
            System.out.println(user);
        }
    }

5、方式二

  • applicationContext.xml

    <?xml version="1.0" encoding="UTF8"?>
    <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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <import resource="spring-dao.xml"/>
    
        <bean id="userMapper" class="com.hmx.mapper.UserMapperImpl">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    
    </beans>
  • 介面類的實現類:UserMapperImpl.java

    public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
        public List<User> selectUser() {
            return getSqlSession().getMapper(UserMapper.class).selectUser();
        }
    }