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.我的文件结构