用mybatis接口代理开发模式实现以下功能: (1) 创建Product类,对应ssm数据库中product表; (2) 创建ProductDao接口+映射文件ProductMapper.xml,实现以下功能:按id查询商品信息;查询所有商品信息;新增商品;修改商品信息;按id删除商品 (3) 配置applicationContext.xml和mybatis-config.xml (4) 创建ProductController,定义test方法测试以上功能 (5) 创建测试类
时间: 2024-02-11 19:04:23 浏览: 87
好的,我知道了。以下是对应的代码实现:
Product.java
```java
public class Product {
private int id;
private String name;
private double price;
private int stock;
// getter and setter methods
}
```
ProductDao.java
```java
public interface ProductDao {
Product getProductById(int id);
List<Product> getAllProducts();
void addProduct(Product product);
void updateProduct(Product product);
void deleteProduct(int id);
}
```
ProductMapper.xml
```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="com.example.dao.ProductDao">
<resultMap id="productResultMap" type="com.example.model.Product">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="price" property="price"/>
<result column="stock" property="stock"/>
</resultMap>
<select id="getProductById" resultMap="productResultMap">
SELECT * FROM product WHERE id = #{id}
</select>
<select id="getAllProducts" resultMap="productResultMap">
SELECT * FROM product
</select>
<insert id="addProduct">
INSERT INTO product(name, price, stock) VALUES(#{name}, #{price}, #{stock})
</insert>
<update id="updateProduct">
UPDATE product SET name=#{name}, price=#{price}, stock=#{stock} WHERE id=#{id}
</update>
<delete id="deleteProduct">
DELETE FROM product WHERE id=#{id}
</delete>
</mapper>
```
applicationContext.xml
```xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<bean id="productDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.example.dao.ProductDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
```
mybatis-config.xml
```xml
<?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="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
```
ProductController.java
```java
@Controller
public class ProductController {
@Autowired
private ProductDao productDao;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
Product product = new Product();
product.setName("Test Product");
product.setPrice(100.0);
product.setStock(10);
productDao.addProduct(product);
product.setPrice(200.0);
productDao.updateProduct(product);
Product product2 = productDao.getProductById(product.getId());
System.out.println(product2.getName());
List<Product> productList = productDao.getAllProducts();
for (Product p : productList) {
System.out.println(p.getName());
}
productDao.deleteProduct(product.getId());
return "test";
}
}
```
ProductControllerTest.java
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ProductControllerTest {
@Autowired
private ProductController productController;
@Test
public void test() {
productController.test();
}
}
```
阅读全文