您的位置:首页 > 移动开发

Spring(17):新增功能:在超市订单系统中实现订单表的查询(采用MapperScannerConfigurer)

2018-01-02 21:11 906 查看
2018/1/2

承接上文《Spring(16):新增功能:在超市订单系统中实现订单表的查询(采用MapperFactoryBean)》这集介绍使用MapperScannerConfigurer映射器进行改进。

【1】配置文件添加以下内容:

<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.smbms.dao"></property>
</bean>

<context:component-scan base-package="com.smbms.service"></context:component-scan>


同时删除MapperFactoryBean和业务Bean的配置;

【2】业务 BillServiceImpl.java 实现类做一下修改:

package com.smbms.service;

import java.util.List;

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

import com.smbms.dao.BillMapper;
import com.smbms.entities.Bill;

@Service("billService")
public class BillServiceImpl implements BillService {
@Autowired
private BillMapper billMapper;

@Override
public List<Bill> findBillsWithConditions() {
return billMapper.billShow();
}

@Override
public List<Bill> findBillsWithConditionsByThressConditions(Bill bill) {
return billMapper.billShowByCondition(bill);
}

public BillServiceImpl() {}

public BillServiceImpl(BillMapper billMapper) {
this.billMapper = billMapper;
}

}


【3】Spring配置文件全部内容如下(记得引入context 命名空间):

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/test?
useUnicode=true&characterEncoding=utf-8]]></value>
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>

<!-- SqlSessionFactoryBean 配置 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引用Mybatis配置文件的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.smbms.dao"></property>
</bean>

<context:component-scan base-package="com.smbms.service"></context:component-scan>
</beans>


【4】编写单元测试BillServiceImplTest.java:

package com.smbms.service;

import java.util.List;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.smbms.entities.Bill;

public class BillServiceImplTest {

@Test
public void test() {
System.out.println("start..");
System.out.println("----------------1----------------");
@SuppressWarnings("resource")
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
BillService billService = (BillService)ctx.getBean("billService");
List<Bill> bills = billService.findBillsWithConditions();
for(Bill b:bills){
System.out.println(b.toString());
}
System.out.println("--------------2-----------");
//		HashMap<String, String> billMap = new HashMap<String,String>();
//		billMap.put("produceName", "apple");
//		billMap.put("providerId","1");
//		billMap.put("isPayment", "2");
Bill bill = new Bill();
bill.setProviderId(1);
bill.setProduceName("apple");
bill.setIsPayment(2);
bills = billService.findBillsWithConditionsByThressConditions(bill);
for(Bill b:bills){
System.out.println(b.toString());
}
}

}


【5】输出结果:

DEBUG 01-02 20:54:44,099 JDBC Connection [jdbc:mysql://127.0.0.1:3306/test?
useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,132 ==>  Preparing: select * from smbms_bill   (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,212 ==> Parameters:   (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,741 <==    Columns: id, billCode, produceName, produceDesc, produceUint, produceCount, totalPrice, isPayment, createBy, creationDate, modifyBy, modifyDate, providerId  (JakartaCommonsLoggingImpl.java:59)
DEBUG 01-02 20:54:44,742 <==        Row: 1, 001, apple, fruit, individual, 100.00, 100.00, 2, 1, 2018-01-02 11:09:20.0, null, null, 1  (JakartaCommonsLoggingImpl.java:59)
DEBUG 01-02 20:54:44,769 <==        Row: 2, 002, banana, fruit, individual, 200.00, 100.00, 2, 1, 2018-01-02 11:26:10.0, null, null, 1  (JakartaCommonsLoggingImpl.java:59)
DEBUG 01-02 20:54:44,770 <==      Total: 2  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,773 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c33f1a9]  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,774 Returning JDBC Connection to DataSource  (DataSourceUtils.java:327)
Bill [id=1, billCode=001, produceName=apple, productDesc=null, productUnit=null, productCount=null, totalPrice=100, isPayment=2, providerId=1, createBy=1, creationDate=Tue Jan 02 11:09:20 CST 2018, modifyBy=null, modifyDate=null]
Bill [id=2, billCode=002, produceName=banana, productDesc=null, productUnit=null, productCount=null, totalPrice=100, isPayment=2, providerId=1, createBy=1, creationDate=Tue Jan 02 11:26:10 CST 2018, modifyBy=null, modifyDate=null]
--------------2-----------
DEBUG 01-02 20:54:44,775 Creating a new SqlSession  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,775 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@37883b97] was not registered for synchronization because synchronization is not active  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,776 Fetching JDBC Connection from DataSource  (DataSourceUtils.java:110)
DEBUG 01-02 20:54:44,777 JDBC Co
4000
nnection [jdbc:mysql://127.0.0.1:3306/test?
useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,777 ==>  Preparing: select b.billCode,b.produceName,p.proName,b.totalPrice,b.isPayment,b.creationDate from smbms_bill b,smbms_provider p where b.produceName=? and b.providerId=? and b.isPayment=? and b.providerId=p.id   (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,778 ==> Parameters: apple(String), 1(Integer), 2(Integer)  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,870 <==    Columns: billCode, produceName, proName, totalPrice, isPayment, creationDate  (JakartaCommonsLoggingImpl.java:59)
DEBUG 01-02 20:54:44,872 <==        Row: 001, apple, JingDong, 100.00, 2, 2018-01-02 11:09:20.0  (JakartaCommonsLoggingImpl.java:59)
DEBUG 01-02 20:54:44,874 <==      Total: 1  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,875 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@37883b97]  (JakartaCommonsLoggingImpl.java:54)
DEBUG 01-02 20:54:44,875 Returning JDBC Connection to DataSource  (DataSourceUtils.java:327)
Bill [id=null, billCode=001, produceName=apple, productDesc=null, productUnit=null, productCount=null, totalPrice=100, isPayment=2, providerId=null, createBy=null, creationDate=Tue Jan 02 11:09:20 CST 2018, modifyBy=null, modifyDate=null]


综上,以上是全部内容,采用MapperFactoryBean映射器改进,可以参考上一篇博文:《Spring(16):新增功能:在超市订单系统中实现订单表的查询(采用MapperFactoryBean)》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐