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

Spring整合Mybatis

整合Mybatis

步驟:

  1. 匯入相關jar包
    • junit
    • mybatis
    • mysql資料庫連線
    • spring相關包
    • aop織入
    • mybatis-spring
  2. 編寫配置檔案
  3. 測試

1、匯入相關jar包

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>mysq1</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>
<!--Spring操作資料庫的話,還需要一個spring-jdbc-->
<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.8.13</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

2、回憶Mybatis

  1. 編寫實體類
  2. 編寫核心配置檔案
  3. 編寫介面
  4. 編寫Mapper.xml
  5. 測試

3、Mybatis-Spring

1、匯入相關jar包

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

2、建立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>
    
  <typeAliases>
	  <package name="com.qilu.bean"/>
  </typeAliases>

</configuration>

3、建立spring-mybatis.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

	<!--DataSource:使用Spring的資料來源提供的Mybatis的配置 c3p0 dbcp druid
           我們這裡使用Spring提供的JDBC
	-->
	<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/test?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/kuang/mapper/*.xml"/ >
	</bean>
        
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能使用構造器注入,因為他沒有構造方法 -->
	    <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
</beans>

4、編寫程式碼

1、編寫bean類

public class User {
    private int id;
    private String name;
    private String password;
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2、編寫UserMapper介面

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

3、編寫UserMapper.xml配置檔案

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.qilu.mapper.UserMapper">
	<select id="selectUser" resultType="com.qilu.bean.User">
    	select * from user;
    </select>
</mapper>

4、編寫UserMapperImpl實現類

public class UserMapperImpl implements UserMapper {
    //我們的所有操作,原來都由sqlSession來執行,現在都由SqlSessionTemplate來完成
    private SqlsessionTemplate sqlSession;

    public void setSq1Session(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
        public List<User> selectUser () {
            UserMapper mapper = sq1Session.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
}

5、注入spring-mybatis.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

	<!--DataSource:使用Spring的資料來源提供的Mybatis的配置 c3p0 dbcp druid
           我們這裡使用Spring提供的JDBC
	-->
	<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/test?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/kuang/mapper/*.xml"/ >
	</bean>
    
    <!-- 相當於mybatis中的SqlSession -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能使用構造器注入,因為他沒有構造方法 -->
	    <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    
    <!-- 將UserMapperImpl注入到容器中 -->
	<bean id="userMapperImpl" class="com.qilu.mapper.UserMapperImpl" />
</beans>

6、編寫測試類

public class MyTest {
    @Test
    public void test() throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}