MyBatis Starter Program

MyBatis Starter Program

design example

Implement inserting data into the data table of the database tedu_ums.t_users

Development steps

1 Create a project DAY07-MyBatis-Sample, generate web.xmlit, web.xmlconfigure it in (2 items), add spring-webmvcdependencies, copy the Spring configuration file, and add the Tomcat runtime environment;

2 Add new dependencies: mybatis, mybatis-spring, spring-jdbc:

<!-- 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>

In the above dependencies, spring-jdbcthe version must be spring-webmvcconsistent with! (In the same project, spring-all dependencies prefixed with a prefix should use the same version)

3 Create an implementation class cn.tedu.spring.entity.User, declare Integer id, String username, String passwordproperties, and related methods in this class;

4 Design persistence layer interface: create an cn.tedu.spring.mapper.UserMapperinterface and add abstract methods to the interface:

Integer reg(User user);

Note: The number of rows affected by the addition, deletion and modification operation is fixed. When designing the method, the return value can be declared as an Integer type, or void can be used to indicate that there is no return value.

5 Download the file from FTP , rename mybatis.zipthe decompressed one (recommended), and then copy it to the new folder of the project ;EmpMapper.xmlUserMapper.xmlsrc\main\resourcemappersUserMapper.xmlmappers

6 Edit the UserMapper.xmlfile:

<!-- 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 Add dependencies: mysql-connector-xxxx, dbcp, junit;

src\main\resources8 is configured under db.properties;

9 Copy from the previous project spring-dao.xml, in this configuration file, at least configure: load db.properties,BasicDataSource

10 In spring-dao.xml, configure:

<!-- 配置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 Write a test class and add test methods:

@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();
}

Summarize

  • Steps to configure mybatis in spring:
  • xxmapper.xmlConfigure the scanMapperScannerConfigurer
  • Configuration SqlSessionFactoryBean, you need to use the database connection pool数据源datasource

  • complete dependency filepom.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>
  • complete db.propertiesfile
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=5
maxSize=10
  • complete spring-dao.xmlfile
<?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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325913313&siteId=291194637