更多内容,点击了解: https://how2j.cn/k/spring-mybatis/spring-mybatis-tutorial/1134.html?p=81777

本教程是为了后续学习SSM,使学习曲线更加平滑而准备的。 用于演示Spring和Mybatis如何整合在一起

步骤 1 : 基于Mybatis的课程

本教程基于Mybatis的课程进行, 所以在开始之前希望你建库,表结构,Mybatis的常用用法都已经熟练了。

步骤 2 : pojo

package com.how2java.pojo;

public class Category {

    private int id;

    private String name;

    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;

    }

    @Override

    public String toString() {

        return "Category [id=" + id + ", name=" + name + "]";

    }

}

步骤 3 : Mapper

package com.how2java.mapper;

import java.util.List;

import com.how2java.pojo.Category;

public interface CategoryMapper {

    public int add(Category category); 

    public void delete(int id); 

    public Category get(int id); 

    public int update(Category category);  

    public List<Category> list();

    public int count(); 

}

步骤 4 : Category.xml

Category.xml需要和上一个CategoryMapper类放在同一个包下,并且namespace必须写CategoryMapper

<mapper namespace="com.how2java.mapper.CategoryMapper">

4495.png
uploading.4e448015.gif正在上传…

重新上传
取消

Category.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

    <mapper namespace="com.how2java.mapper.CategoryMapper">

        <insert id="add" parameterType="Category" >

            insert into category_ ( name ) values (#{name})   

        </insert>

        <delete id="delete" parameterType="Category" >

            delete from category_ where id= #{id}  

        </delete>

        <select id="get" parameterType="_int" resultType="Category">

            select * from   category_  where id= #{id}   

        </select>

        <update id="update" parameterType="Category" >

            update category_ set name=#{name} where id=#{id}   

        </update>

        <select id="list" resultType="Category">

            select * from   category_     

        </select>    

    </mapper>

步骤 5 : applicationContext.xml

<context:annotation-config />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

配置连接数据库的驱动,URL,账号和密码

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">

扫描XML配置文件

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

扫描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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <context:annotation-config />

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"

        <property name="driverClassName"

            <value>com.mysql.jdbc.Driver</value

        </property

        <property name="url"

            <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value

        </property

        <property name="username"

            <value>root</value

        </property

        <property name="password"

            <value>admin</value

        </property>  

   </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="typeAliasesPackage" value="com.how2java.pojo" />

        <property name="dataSource" ref="dataSource"/>

        <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>

    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.how2java.mapper"/>

    </bean>

</beans>

步骤 6 : 测试

使用Spring注解方式测试,拿到注入的CategoryMapper对象,当调用add方法的时候,会自动去找Category.xml里id="add"的sql语句。

package com.java.test;

import java.util.List;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

import com.how2java.mapper.CategoryMapper;

import com.how2java.pojo.Category;

@RunWith(SpringJUnit4Cla***unner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class MybatisTest {

    @Autowired

    private CategoryMapper categoryMapper;

    @Test

    public void testAdd() {

        Category category = new Category();

        category.setName("new Category");

        categoryMapper.add(category);

    }

    @Test

    public void testList() {

        System.out.println(categoryMapper);

        List<Category> cs=categoryMapper.list();

        for (Category c : cs) {

            System.out.println(c.getName());

        }

    }

}

步骤 7 : 可运行项目

下载区(点击进入)有本知识点对应的可运行项目下载 ,实在自己搞不出来,就下载解压出来比较一下。

更多内容,点击了解: https://how2j.cn/k/spring-mybatis/spring-mybatis-tutorial/1134.html?p=81777