applicationContext
<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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis配置文件--><!--configLocation这个不能改变 意思是配置位置 classpath必须标明是类路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 加载mybatis的映射文件--><!-- 一般是接口和其映射文件没有在同一包下 需要配置-->
<property name="mapperLocations">
<!-- <value>classpath:config\UserMapper.xml</value>-->
<!-- 将包config包及其子包下的.xml全部配置了-->
<value>classpath:config/*.xml</value>
</property>
</bean>
<!-- 自动扫描--><!-- 自动扫描指定包下的所有接口和映射文件--><!-- 这个配置了数据访问层-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.oupeng.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 自动扫描--><!--自动扫描指定的包 将包下的注解进行启用--><!-- 主要配置业务逻辑层-->
<context:component-scan base-package="com.oupeng.service.impl"></context:component-scan>
</beans>
UserServiceImpl
package com.oupeng.service.impl;
import com.oupeng.mapper.UserMapper;
import com.oupeng.pojo.User;
import com.oupeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService1")
public class UserServiceImpl implements UserService {
// 注入数据访问层
// 一定写的是接口名
@Autowired
private UserMapper userMapper;
@Override
public List<User> findUserList(User user) {
return userMapper.getUserList(user);
}
@Override
public boolean addNewUser(User user) {
int result = userMapper.addUser(user);
return result > 0 ? true : false;
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
applicationContext
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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis配置文件--><!--configLocation这个不能改变 意思是配置位置 classpath必须标明是类路径 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 加载mybatis的映射文件--><!-- 一般是接口和其映射文件没有在同一包下 需要配置-->
<property name="mapperLocations">
<!-- <value>classpath:config\UserMapper.xml</value>-->
<!-- 将包config包及其子包下的.xml全部配置了-->
<value>classpath:config/*.xml</value>
</property>
</bean>
<!-- 配置SqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 必须以构造注入的方式 注入sqlSessionFactory-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 配置数据访问层组件 Provider -->
<bean id="providerMapper" class="com.oupeng.mapper.provider.impl.ProviderMapperImpl">
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
<!--配置业务逻辑层 provider-->
<bean id="providerService" class="com.oupeng.service.impl.ProviderServiceImpl">
<!-- 完成数据访问层对象的注入-->
<property name="providerMapper" ref="providerMapper"></property>
</bean>
</beans>
BillServiceImpl
package com.oupeng.service.impl;
import com.oupeng.mapper.bill.BillMapper;
import com.oupeng.pojo.Bill;
import com.oupeng.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("billService")
public class BillServiceImpl implements BillService {
// 注入数据访问层
@Autowired
private BillMapper billMapper;
@Override
public List<Bill> findBill(Bill bill) {
return billMapper.getBill(bill);
}
public BillMapper getBillMapper() {
return billMapper;
}
public void setBillMapper(BillMapper billMapper) {
this.billMapper = billMapper;
}
}
BillMapper
<?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.oupeng.mapper.bill.BillMapper"><!--Mapper映射器-->
<select id="getBill" parameterType="Bill" resultMap="billProviderList">
select b.billCode,b.productName,p.proName,b.totalPrice,b.isPayment,b.creationDate from smbms_bill b,smbms_provider p
where b.productName like concat('%',#{productName},'%') and b.providerId=#{providerId} and b.isPayment=#{isPayment} and b.providerId=p.id
</select>
<resultMap id="billProviderList" type="Bill">
<result property="providerName" column="p.proName"/>
</resultMap>
</mapper>