1. 程式人生 > 其它 >整合mybatis和spring(採用idea)

整合mybatis和spring(採用idea)

技術標籤:Javaspringmybatismaven

1.在pox.xml匯入一下包

下面的build是解決maven靜態資源過濾問題。

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</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.4.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </
dependency
>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>

2.新建包名和檔案!

總體如下圖所示
在這裡插入圖片描述

resource下建立applicationcontext.xml,mybatis-config.xml,spring-dao.xml。
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>
<!--    開啟mybatis的自帶日誌服務-->
    <settings>
    <setting name="logImpl" value="STDOUT_LOGGING "/>
</settings>
<typeAliases>
<!--    採用了別名-->
    <typeAlias type="com.tg.mapper.PersonMapper" alias="person"/>
</typeAliases>

    <mappers>
        <!-- 使用對映器介面實現類的完全限定類名 -->
        <mapper class="com.tg.mapper.PersonMapper"/>
    </mappers>
</configuration>

applicationcontext.xml
主要是用來註冊bean。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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/context
        https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <import resource="spring-dao.xml"/>


    <bean id="personMapper" class="com.tg.mapper.PersonMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
    <bean id="personMapper2" class="com.tg.mapper.PersonMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

spring-dao.xml
這個配置檔案裡面替換了mybatis的資料來源,DateSorce替換了mybatis的資料來源,所以mybatis的配置檔案裡不需要載入資料來源,mybatis裡用的是sqlseession方法,需要sqlseessionFacotry構建sqlseession,同樣spring接管也需要如此做,先註冊一個sqlSessionFactory的bean,在裡面配置mybatis的配置檔案,但是spring中不是用的sqlSession,而是SqlSessionTemplate,我們將使用SqlSessionTemplate來完成mapper的呼叫。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--DateSource替換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="123456"/>
    </bean>

    <!--sqlseessionFacotry-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--繫結mybatis配置檔案-->
        <property name="configLocation" value="mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/tg/mapper/*.xml"/>
    </bean>
<!--    SqlSessionTemplate就是我們使用的方法-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--        sqlSessionFactory使用構造器注入,因為沒有set方法-->
        <constructor-arg index="0" value="sqlSessionFactory"/>
    </bean>
    </beans>

3.實體類

配置檔案寫好了就是實體類的建立,在pojo下建立一個Person的實體類。

public class Person {
    private int id;
    private  String name;
    private  int age;

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

4.寫service的方法

PersonMapper

public interface PersonMapper {
    public List<Person> selectPerson();
    }

PersonMapper.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.tg.mapper.PersonMapper">
<select id="selectPerson" resultType="person">
    select * from person;
</select>
</mapper>

PersonMapperImpl

public class PersonMapperImpl implements PersonMapper{
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    private SqlSessionTemplate sqlSession;

    //使用SqlSessionTemplate來實現所有操作
    public List<Person> selectPerson() {
        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
        return mapper.selectPerson();
    }
    }

5.測試

在測試包建立測試檔案
測試檔案隨意命名,但是不能和Test同名。

public class Mytest {
    @Test
    public  void test() throws IOException {
        //獲取容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
        //獲得mapper
        PersonMapper mapper = context.getBean("personMapper", PersonMapper.class);
        
        for (Person person : mapper.selectPerson()) {
            System.out.println(person);
        }
    }
}

以上就是整合mybatis和spring的一個小專案,spring是針對bean的生命週期進行管理的輕量級容器(lightweight container)。以上僅為個人見解,如有不足,還請見諒!!!!