集成 MyBatis
操作流程
- 创建 Maven 项目
- 添加 Maven 依赖
- Spring 依赖
- MyBatis 依赖
- MySQL 驱动
- Spring 事务的依赖
- MyBatis、Spring 集成的依赖
- MyBatis 官方提供用来在 Spring 项目中创建 MyBatis 的 SqlsessionFactory、dao 对象
- 创建 MyBatis 实体类、接口 和 mapper 文件
- 创建 MyBatis 主配置文件、创建 Spring 主配置文件
- 声明 MyBatis 对象,由 Spring 创建
- 数据源
- SqlSessionFactory 对象
- 接口代理对象
- 声明自定义 service
- 创建测试类
- 获取 service 对象
- 通过 service 调用接口代理对象完成数据库访问
Druid
-
Spring 配置文件中数据源 Druid 配置模板
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_user}" />
<property name="password" value="${jdbc_password}" />
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="6000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
SqlSessionFactory
-
Spring 配置文件中创建 SqlSessionFactory 对象
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
SqlSession
- 可以得到 SqlSession 对象,但也要创建 dao 接口实现类
- 在接口实现类中直接使用 sqlSession
- 设置属性 sqlSession 并赋值直接使用 sqlsession
- 或者直接在配置文件中生成接口代理对象
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" value="sqlSessionFactory"/>
</bean>
接口对象
声明 Dao 接口对象
- 使用 MapperFactoryBean 创建 dao 接口代理对象
- 单独创建接口代理对象
- mapperInterface:接口路径
- 赋值 SqlSessionFactroy 对象或 sqlsession对象
- 同时赋值时 sqlSession 会覆盖 SqlSessionFactory
- SqlSession 需要工厂创建,MapperFactoryBean 会接管工厂
单独注册
<bean id="goodsMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="sqlSessionTemplate" ref="sqlSession"/>
<property name="mapperInterface" value="demo.dao.GoodsDao"/>
</bean>
<bean id="orderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="sqlSessionTemplate" ref="sqlSession"/>
<property name="mapperInterface" value="demo.dao.OrderDao"/>
</bean>
<bean id="service" class="demo.service.ShoppingServiceImpl">
<property name="goods" ref="goodsMapper"/>
<property name="order" ref="orderMapper"/>
</bean>
扫描注册
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="demo.dao"/>
</bean>
声明 service 对象
<bean id="demoService" class="demo.service.DemoServiceImpl">
<property name="demoDao" ref="demoDao"/>
</bean>
测试
- Spring 整合 MyBatis 框架会默认自动提交事务
@Test
public void testAdd() {
DemoService service = (DemoService) SpringUtil.getBean("demoService");
int rows = service.add(new Demo(123, "二狗", 23, new Date()));
System.out.println("受影响行数 " + rows);
}
@Test
public void testGetByName() {
DemoService service = (DemoService) SpringUtil.getBean("demoService");
List<Demo> all = service.getByName("狗");
for (Demo demo : all) {
System.out.println(demo);
}
}
配置文件
Spring
applicationContext.xml
- Spring 配置文件
- 基本只需改动 MyBatis 配置文件的路径
- 或自定义数据源配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:dataSource.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="5"/>
<property name="maxWait" value="6000"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="demo.dao"/>
</bean>
<bean id="demoService" class="demo.service.DemoServiceImpl">
<property name="demoDao" ref="demoDao"/>
</bean>
</beans>
属性配置
jdbc.url=jdbc:mysql://localhost:3306/demo01
jdbc.username=root
jdbc.password=123456
MyBaits
- 只需要指定 Mapper 文件映射
- 不再使用 MyBatis 的 默认连接池
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<mappers>
<mapper resource="demo/dao/DemoDao.xml"/>
</mappers>
</configuration>
事务处理
- 事务:一组 sql 语句的集合,作为一个整体执行
- 使用事务时机
- 涉及多表或多个 DML 语句执行需要保证语句同时成功或全部失败不执行操作以保证数据的一致
- Java 中控制事务在 service 类的业务方法上
- 业务方法调用多个 dao 接口方法执行 sql 语句
- JDBC 及 MyBatis 访问数据库、处理事务方法
- JDBC:Connection con、con.commit()、con.rollback()
- MyBatis:SqlSession.commit()、SqlSession.rollback
- 不同数据库访问技术和处理事务的对象、方法不同
- Spring 有统一的事务处理模型
- 使用统一的步骤、方式 完成不同数据库访问技术、事务处理机制
- 抽象了事务处理的各方面。定义事务处理步骤
- 声明式事务
- 事务相关资源和内容都提供给 spring,由 spring 处理事务提交、回滚
- 仅使用很少的代码量
使用
- 固定步骤,提供事务使用信息给 spring
- spring 配置文件中使用
<bean> 声明数据库访问技术对应的事务管理器实现类 - 指定要加入事务功能的类、方法
- 指定方法需要的隔离级别、传播行为【超时 可不设置】
- 事物内部提交、回滚事务使用事务管理器对象来代替完成
- 事务管理器是一个接口和其众多实现类
- 接口:
PlatforinTranstionalManager
- 定义事务方法:
commit() 、rollback() - 实现类:spring 创建对应数据库访问技术的事务处理类
- MyBatis:
DataSourceTransactionManager - Hibernate:
HibernateTransactionManager
控制事务
TransactionDefinition :事务定义接口
隔离级别
传播行为
- 控制业务方法是否有事务,事务类型
- 共 7 种传播行为,表示业务方法调用时事务如何使用
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_MANDATORY = 2;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
- 例如:
REQUIRED 传播行为
默认超时时限
- 方法最长执行时间:
int TIMEOUT_DEFAULT = -1
- 若方法执行超过时间进行事务回滚
- 单位:s;整数值,默认 -1
- 影响因素较多,一般不可控,一般不进行设置
例如:MyBatis
<bean id="mybatis" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"/>
事务执行
- 业务方法执行成功
- spring 在方法执行后自动提交事务,由事务管理器 commit
- 业务方法抛出运行时异常时
- spring 执行回滚,调用事务管理器的 rollback
- 运行时异常:RuntimeException 及其子类
- 业务方法抛出非运行时异常
使用事务
固定步骤,提供事务使用信息给 spring
- spring 配置文件
<bean> 声明数据库访问技术对应的事务管理器实现类 - 指定要加入事务功能的类、方法
- 指定方法需要的隔离级别、传播行为【超时 一般不设置】
注解方案
Aspectj
-
使用 Aspectj 的 AOP 配置管理事务
- 使用 xml 配置文件配置事务代理
- 不足:每个目标类都需要配置事务代理
- 但是可以自动为每个符合切入点表达式的类生成事务代理
- 适合大型项目
- 针对大量类、方法需要配置事务的情况
- 使用 aspectj 框架在 spring 配置文件中声明类、方法需要的事务
- 业务方法和事务配置完全分离
-
aspectj 使用步骤
- 添加 aspectj 的 maven 依赖
- 声明事务管理器对象
- 声明方法需要的事务类型
- 配置方法的事务属性:隔离级别、传播行为、超时
name :完整方法名,不含包、类;使用 * 表示任意字符rollbackFor :值是异常类全限定名,其他事务属性值正常配置- 基本使用默认属性值即可
- 配置 AOP
- 指定要创建代理的包、类、方法,将事务织入
- 关联配置的方法事务和切入点表达式
示例
<tx:annotation-driven transaction-manager="mybatis"/>
<tx:advice id="myAdvice" transaction-manager="mybatis">
<tx:attributes>
<tx:method name="query*" read-only="true"/>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="servicePT" expression="execution(* *..service..*.*(..))"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="servicePT"/>
</aop:config>
完整使用
简单使用 Spring + MyBatis 框架的流程逻辑
- IoC 使用
- AOP 使用
- 整合 MyBatis
- 使用注解 或 配置文件 进行
配置文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>li_maven</groupId>
<artifactId>demo12</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.encoding>UTF-8</project.build.encoding>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
applicationContext.xml
- 配置数据源,自定义数据库连接池
- 声明
SqlSessionFactory 对象,读取 mybatis 配置文件 - 声明
dao 接口对象 - 声明事务管理器
- 使用 Spring 提供 AOP 实现进行注解添加事务
- 开启事务注解驱动
- 或使用
Aspectj 声明式事务处理
- 配置事务属性,根据不同方法设置
- 配置 AOP
- 配置切入点表达式:指定使用事务的包、类、方法
- 关联通知和切入点表达式
- 声明 service 对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:dataSource.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="20"/>
<property name="initialSize" value="5"/>
<property name="maxWait" value="6000"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="demo.dao"/>
</bean>
<bean id="mybatis" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"/>
</bean>
<tx:advice id="myAdvice" transaction-manager="mybatis">
<tx:attributes>
<tx:method name="buy" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*" read-only="true">
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="servicePT" expression="execution(* *..service..*.*(..))"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="servicePT"/>
</aop:config>
<bean id="shopping" class="demo.service.ShoppingServiceImpl">
<property name="goods" ref="goodsDao"/>
<property name="order" ref="orderDao"/>
</bean>
</beans>
属性配置文件
jdbc.url=jdbc:mysql://localhost:3306/demo01
jdbc.username=root
jdbc.password=123456
mybatis-config.xml
- 不再使用 MyBatis 的连接池
- 仅配置 mapper 文件的映射以及其他的 MyBatis 操作
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<mappers>
<mapper resource="demo/dao/GoodsDao.xml"/>
<mapper resource="demo/dao/OrderDao.xml"/>
</mappers>
</configuration>
domain
Goods
package demo.domain;
public class Goods {
private int id;
private String name;
private double price;
private int nums;
public Goods(){}
public Goods( String name, double price, int nums) {
this.name = name;
this.price = price;
this.nums = nums;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public long getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
@Override
public String toString() {
return "商品: " +
"\t编号 = " + id +
", \t商品名 = " + name +
", \t价格 = " + price +
", \t库存 = " + nums;
}
}
Order
package demo.domain;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Order {
private int id;
private int goodsId;
private String goodsName;
private int nums;
private double totalPrice;
private Date date;
public Order() {
}
public Order(String goodsName, int goodsId, int nums, double totalPrice) {
this.goodsName = goodsName;
this.goodsId = goodsId;
this.nums = nums;
this.totalPrice = totalPrice;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public long getNums() {
return nums;
}
public void setNums(int nums) {
this.nums = nums;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
private String date2String(Date date) {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
}
@Override
public String toString() {
return "订单: " +
"\t编号 = " + id +
", \t商品编号" + goodsId +
", \t商品名 = " + goodsName +
", \t数量 = " + nums +
", \t总价 = " + totalPrice +
", \t日期 = " + date2String(date);
}
}
dao
- MyBatis 使用的 dao 接口和 mapper.xml 文件
GoodsDao.java
package demo.dao;
import demo.domain.Goods;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface GoodsDao {
int add(Goods goods);
int update(Goods goods);
int updateNums(@Param("id") int id,
@Param("nums") int nums);
List<Goods> getAll();
List<Goods> getByName(String name);
double getPrice(int id);
int getNums(int id);
String getName(int id);
}
GoodsDao.xml
<?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="demo.dao.GoodsDao">
<resultMap id="goods" type="demo.domain.Goods">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="price" property="price"/>
<result column="nums" property="nums"/>
</resultMap>
<insert id="add">
insert into goods(name, price)
values (#{name}, #{price})
</insert>
<update id="update">
update goods set id = #{id}
<if test="name != null">
,name = #{name}
</if>
<if test="price != 0">
,price = #{price}
</if>
<if test="nums != 0">
,nums = #{nums}
</if>
where id = #{id}
</update>
<update id="updateNums">
update goods
set nums = nums - #{nums}
where id = #{id}
</update>
<select id="getAll" resultMap="goods">
select *
from goods
</select>
<select id="getByName" resultMap="goods">
select name, price, nums
from goods
where name like '%' #{name} '%'
</select>
<select id="getPrice" resultType="double">
select price
from goods
where id = #{id}
</select>
<select id="getNums" resultType="int">
select nums
from goods
where id = #{id}
</select>
<select id="getName" resultType="String">
select name
from goods
where id = #{id}
</select>
</mapper>
OrderDao.java
package demo.dao;
import demo.domain.Order;
import java.util.List;
public interface OrderDao {
int add(Order order);
OrderDao getById(int id);
List<OrderDao> getAll();
}
OrderDao.xml
<?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="demo.dao.OrderDao">
<insert id="add">
insert into `order`(goodsId, goodsName, nums, totalPrice, date)
values (#{goodsId}, #{goodsName}, #{nums}, #{totalPrice}, #{date})
</insert>
<select id="getById" resultType="demo.domain.Order">
select * from
`order` where id = #{id}
</select>
<select id="getAll" resultType="demo.domain.Order">
select * from `order`
</select>
</mapper>
service
ShoppingService.java
package demo.service;
import demo.domain.Goods;
import java.util.List;
public interface ShoppingService {
void buy(int id, int nums);
List<Goods> query();
}
ShoppingServiceImpl.java
package demo.service;
import demo.dao.GoodsDao;
import demo.dao.OrderDao;
import demo.domain.Goods;
import demo.domain.Order;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
public class ShoppingServiceImpl implements ShoppingService {
private GoodsDao goods;
private OrderDao order;
public void setGoods(GoodsDao goods) {
this.goods = goods;
}
public void setOrder(OrderDao order) {
this.order = order;
}
@Override
public void buy(int id, int nums) {
int amount = goods.getNums(id);
if (amount < nums) {
System.out.println("商品库存不足,仅剩余 " + amount + "件");
return;
} else if (amount == 0) {
System.out.println("商品库存已经清空");
return;
}
Order order = new Order();
order.setGoodsId(id);
order.setGoodsName(goods.getName(id));
order.setNums(nums);
order.setTotalPrice(nums * goods.getPrice(id));
order.setDate(new Date());
int rows = this.order.add(order);
if (rows == 1){
System.out.println("购买成功");
}
rows = goods.updateNums(id, nums);
if (rows == 1){
System.out.println("已修改库存");
}
}
public List<Goods> query() {
return goods.getAll();
}
}
utils
SpringUtil.java
package demo.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringUtil {
private static ApplicationContext context;
static {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public static Object getBean(String id) {
return context.getBean(id);
}
}
test
testShopping.java
package testDao;
import demo.domain.Goods;
import demo.service.ShoppingService;
import demo.utils.SpringUtil;
import org.junit.Test;
public class TestShopping {
@Test
public void testQuery() {
ShoppingService shopping = (ShoppingService) SpringUtil.getBean("shopping");
for (Goods goods : shopping.query()) {
System.out.println(goods);
}
}
@Test
public void testBuy() {
ShoppingService shopping = (ShoppingService) SpringUtil.getBean("shopping");
shopping.buy(1, 20);
}
}
Web项目
web 项目使用容器对象
|