MyBatis入門程序

MyBatis入門程序

設計案例

實現向tedu_ums數據庫的t_users數據表中插入數據。java

開發步驟

1 建立項目DAY07-MyBatis-Sample,生成web.xml,在web.xml中配置(2項),添加spring-webmvc依賴,複製Spring配置文件,添加Tomcat運行環境;mysql

2 添加新的依賴:mybatismybatis-springspring-jdbcgit

<!-- Spring-JDBC -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.2.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>

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

以上依賴中,spring-jdbc的版本需與spring-webmvc保持一致!(同一個項目中,以spring-做爲前綴的依賴都應該使用相同版本)github

3 建立實現類cn.tedu.spring.entity.User,在該類中聲明Integer idString usernameString password屬性,及相關方法;web

4 設計持久層接口:建立cn.tedu.spring.mapper.UserMapper接口,並在接口中添加抽象方法:spring

Integer reg(User user);

注意:增刪改操做固定返回受影響的行數,在設計方法時,能夠將返回值聲明爲Integer類型,或使用void表示無返回值也能夠。sql

5 從FTP下載mybatis.zip文件,將解壓獲得的EmpMapper.xml重命名爲UserMapper.xml(推薦這樣命名),而後在項目的src\main\resource新建文件夾mappers,將UserMapper.xml複製到mappers文件夾中;數據庫

6 編輯UserMapper.xml文件:apache

<!-- MyBatis的接口映射文件,根節點是mapper -->
<!-- 接口映射文件是與Java接口文件(interface)相對應的 -->
<!-- 根節點的namespace屬性用於指定Java接口文件 -->
<mapper 
    namespace="cn.tedu.spring.mapper.UserMapper">
    
    <!-- 節點名稱取決於須要執行的操做 -->
    <!-- 例如增長操做應該使用insert節點 -->
    <!-- id屬性(*)的值是Java接口中的方法名稱 -->
    <!-- parameterType屬性的值是參數類型 -->
    <!-- 節點中間編寫SQL語句 -->
    <insert id="reg"
        parameterType="cn.tedu.spring.entity.User">
        INSERT INTO t_users (
            username, password
        ) VALUES (
            #{username}, #{password}
        )
    </insert>
    
</mapper>

7 添加依賴:mysql-connector-xxxxdbcpjunitspring-mvc

8 在src\main\resources下配置db.properties

9 今後前的項目中複製spring-dao.xml,在該配置文件中,至少配置了:加載db.propertiesBasicDataSource

10 在spring-dao.xml中,配置:

<!-- 配置MapperScannerConfigurer ,掃描mapper的配置在哪裏-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 配置接口文件所在的包 -->
    <property name="basePackage"
        value="cn.tedu.spring.mapper" />
</bean>

<!-- 配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 配置數據源:如何鏈接數據庫等 -->
    <property name="dataSource"
        ref="dataSource" />
    <!-- 配置XML文件的位置 -->
    <property name="mapperLocations"
        value="classpath:mappers/UserMapper.xml" />
</bean>

11 編寫測試類,添加測試方法:

@Test
public void testReg() {
    AbstractApplicationContext ac
        = new ClassPathXmlApplicationContext(
            "spring-mvc.xml",
            "spring-dao.xml");
    
    UserMapper userMapper
        = ac.getBean(
            "userMapper", UserMapper.class);
    
    User user = new User();
    user.setUsername("Tom1");
    user.setPassword("123456");
    Integer affectedRows
        = userMapper.reg(user);
    System.out.println(
        "affectedRows=" + affectedRows);
    
    ac.close();
}

總結

  • 在spring中配置mybatis的步驟:
    • 配置掃描xxmapper.xmlMapperScannerConfigurer
    • 配置SqlSessionFactoryBean,須要用到數據庫鏈接池的數據源datasource
  • 完整的依賴文件pom.xml
<dependencies>

        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>
        
        <!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

        <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

        <!-- MyBatis-Spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>

        <!-- DBCP -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>
  • 完整的db.properties文件
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=5
maxSize=10
  • 完整的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"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
        
    <!-- 加載db.properties -->
    <util:properties id="dbConfig"
        location="classpath:db.properties" />
    
    <!-- 數據源 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" 
            value="#{dbConfig.url}" />
        <property name="driverClassName" 
            value="#{dbConfig.driver}" />
        <property name="username" 
            value="#{dbConfig.user}" />
        <property name="password" 
            value="#{dbConfig.password}" />
        <property name="initialSize" 
            value="#{dbConfig.initSize}" />
        <property name="maxActive" 
            value="#{dbConfig.maxSize}" />
    </bean>

    <!-- 配置MapperScannerConfigurer,自動掃描整個包,而且spring會自動建立UserMapper接口對象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 配置接口文件所在的包 -->
        <property name="basePackage"
            value="cn.tedu.spring.mapper" />
    </bean>
    
    <!-- 配置SqlSessionFactoryBean -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置數據源:如何鏈接數據庫等 -->
        <property name="dataSource"
            ref="dataSource" />
        <!-- 配置XML文件的位置 -->
        <property name="mapperLocations"
            value="classpath:mappers/UserMapper.xml" />
    </bean>
</beans>
相關文章
相關標籤/搜索