Spring整合Mybatis

一:我们以上个mybatis项目为基础,里面创建一个resources/spring/spring-mybatis.xml配置文件

<!-- 1、如果要想将MyBatis交由Spring管理,最为重要的一点就是需要配置好mybatis.cfg.xml配置文件 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <!-- 配置数据源 -->
<!-- 设置MyBatis核心配置文件的路径 -->
<property name="configLocation" value="classpath:mybatis/mybatis.cfg.xml"/>
</bean>

与此同时还需要把新添的配置文件,引入相应配置:

1     <!-- 进行扫描包的设置,如果要设置多个包,则可以使用“,”拆分 -->
2     <context:component-scan base-package="com.yootk.ssm.service,com.yootk.ssm.dao"/>
3     <context:property-placeholder location="classpath:config/*.properties"/>
4     <aop:aspectj-autoproxy/>    <!-- 启用Annotation注解支持 -->
5     <import resource="spring-datasource.xml"/>
6     <import resource="spring-transaction.xml"/>
7     <import resource="spring-mybatis.xml"/>

二:

1.定义DAO查询接口:

public interface INewsDAO {
    public boolean doCreate(News vo) ;
    public News findById(Long id) ;
    /**
     * 进行数据的分页显示处理操作
     * @param params 可以传递的参数内容如下:
     * 1、key = column、value = 要进行数据查询的列;
     * 2、key = keyword、value = 模糊查询关键字;
     * 3、key = start、value = 开始行;
     * 4、key = lineSize、value = 每页显示数据行。
     *  如果此时没有传递column或者是keyword参数,则表示对全部数据进行分页
     * @return 数据的全部内容
     */
    public List<News> findSplit(Map<String,Object> params) ;
    /**
     * 数据个数的统计处理
     * @param params 可以传递的参数内容如下:
     * 1、key = column、value = 要进行数据查询的列;
     * 2、key = keyword、value = 模糊查询关键字;
     *  如果此时没有传递column或者是keyword参数,则表示统计全部数据
     * @return 返回数据行的个数
     */
    public Long getAllCount(Map<String,Object> params) ;
}

2.数据操作方法调用,后来是不用这个的

@Repository
public class NewsDAOImpl implements INewsDAO {
    @Autowired
    private SqlSessionFactory sessionFactory ;
    @Override
    public boolean doCreate(News vo) {
        return this.sessionFactory.openSession().insert("com.yootk.mapper.NewsNS.doCreate",vo) > 0;
    }
    @Override
    public News findById(Long id) {
        return this.sessionFactory.openSession().selectOne("com.yootk.mapper.NewsNS.findById",id);
    }
    @Override
    public List<News> findSplit(Map<String, Object> params) {
        return this.sessionFactory.openSession().selectList("com.yootk.mapper.NewsNS.findSplit",params);
    }

    @Override
    public Long getAllCount(Map<String, Object> params) {
        return this.sessionFactory.openSession().selectOne("com.yootk.mapper.NewsNS.getAllCount",params);
    }
}

3.创建一个service

public interface INewsService {
    public boolean add(News vo) ;
    public News get(long id) ;
    public Map<String,Object> list(String column, String keyword, long currentPage, int lineSize) ;
}

4.定义业务层实现子类

@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private INewsDAO newsDAO ;
    @Override
    public boolean add(News vo) {
        return this.newsDAO.doCreate(vo);
    }

    @Override
    public News get(long id) {
        return this.newsDAO.findById(id);
    }

    @Override
    public Map<String, Object> list(String column, String keyword, long currentPage, int lineSize) {
        Map<String,Object> result = new HashMap<>() ;   // 实现结果的返回
        Map<String,Object> params = new HashMap<>() ;   // 实现参数的传递
        params.put("column",column) ;
        params.put("keyword",keyword) ;
        params.put("start",(currentPage - 1) * lineSize) ;
        params.put("lineSize",lineSize) ;
        result.put("allRecorders",this.newsDAO.getAllCount(params)) ;
        result.put("allNewses",this.newsDAO.findSplit(params)) ;
        return result;
    }
}

5.测试

@ContextConfiguration(locations = "classpath:spring/spring-base.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class TestNewsService {
    @Autowired
    private INewsService newsService;

    @Test
    public void testAdd() {
        News vo = new News();
        vo.setTitle("Feel倍儿爽");
        vo.setContent("期待奇迹的到来");
        System.out.println(this.newsService.add(vo));
    }
    @Test
    public void testGet() {
        System.out.println(this.newsService.get(3L));
    }
    @Test
    public void testList() {
        System.out.println(this.newsService.list("title","%开心%",1,10));
    }
}
原文地址:https://www.cnblogs.com/fcitx/p/11189813.html