Spring整合MyBatis

1.首先导jar包

2.然后配置applicationContext.xml文件:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/bookshop"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<!--声明SqlSessionFactory类-->
	<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--声明数据访问接口,用到:接口扫描器,相当于mybatis.xml文件中的<mappers>-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.demos.mapper"></property>
		<!--关联SqlSessionFactory-->
		<property name="sqlSessionFactory" ref="factory"></property>
	</bean>
	<!--声明业务逻辑类-->
	<bean id="categoryService" class="com.demos.service.impl.CategoryServiceImpl">
		<!--被依赖的对象CategoryMapper,引用的是categoryMapper-->
		<!--接口的代理对象名,默认是首字母小写-->
		<property name="categoryMapper" ref="categoryMapper"></property>
	</bean>

3.写实体类,例子中的实体类是Category

package com.demos.entity;

public class Category {
    private Long id;
    private String cname;

    public Long getId() {
        return id;
    }

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

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
}

4.编写实体类的接口CategoryMapper

package com.demos.mapper;

import com.demos.entity.Category;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CategoryMapper {
    @Select("select * from category")
    public List<Category> find();
}

5.业务逻辑接口CategoryService

package com.demos.service;

import com.demos.entity.Category;

import java.util.List;

//业务逻辑接口
public interface CategoryService {
    //获取所有Category
    public List<Category> getCategories();
}

6.通过CategoryServiceImpl实现业务逻辑接口中的方法

package com.demos.service.impl;

import com.demos.entity.Category;
import com.demos.mapper.CategoryMapper;
import com.demos.service.CategoryService;

import java.util.List;

//实现一个CategoryService接口中的所有的方法
public class CategoryServiceImpl implements CategoryService {
    //依赖于数据访问接口的对象
    //被依赖的对象作为属性,并提供get和set访问器
    private CategoryMapper categoryMapper;

    public CategoryMapper getCategoryMapper() {
        return categoryMapper;
    }

    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public List<Category> getCategories() {
        return this.categoryMapper.find();
    }
}

7.单元测试类TestCategoryServiceImpl

package com.demos.test;


import com.demos.entity.Category;
import com.demos.service.CategoryService;
import com.demos.service.impl.CategoryServiceImpl;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestCategoryServiceImpl {
    @Test
    public void testGetCategories() throws Exception{
        //先创建文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //解析出对象,并当接口对象CategoryService.class返回
        CategoryServiceImpl cs = ac.getBean("categoryService",CategoryServiceImpl.class);
        System.out.println(cs.getCategoryMapper());
        List<Category> actual = cs.getCategories();
        Assert.assertEquals(9,actual.size());
    }
}

PS.我的文件结构

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值