实验目的
#1、使用Mybatis完成对商品的CURD
#2、对实现过程做经验总结
入门级MyBatis程序搭建
1、建表以及数据插入
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
price DECIMAL(10, 2),
description TEXT
);
INSERT INTO products (id, name, price, description) VALUES
(1, '手机', 2999.99, '这款手机性能强劲,拍照效果好'),
(2, '电视', 4999.99, '这台电视清晰度高,音响效果出色'),
(3, '洗衣机', 1999.99, '这款洗衣机省电,容量大,洗衣效果好'),
(4, '电脑', 5999.99, '这台电脑配置高,适合办公和游戏使用'),
(5, '空调', 3999.99, '这台空调制冷快,静音效果好');
2、建立Mavne工程,导入Jar包
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>
3、编写MyBatis核心配置文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- 8.x的driver驱动和url地址 -->
<!--在pom.xml中导Mysql需对应自己的版本-->
<!-- <property name="driver" value="com.mysql.cj.jdbc.Driver"/>-->
<!-- <property name="url"-->
<!-- value="jdbc:mysql://localhost:3306/chen13?"/>-->
<!-- 5.x的driver驱动和url地址 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/chen13" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/Products.xml" />
</mappers>
</configuration>
4、创建一个实体类Product
package com.dlxy.entity;
public class Product {
private Integer id;
private String name;
private double price;
private String description;
public Product() {
}
public Product(Integer id, String name, double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", description='" + description + '\'' +
'}';
}
}
5、编写ProductMapper接口类以及方法
使用MyBastis实现增删改查(CURD)
package com.dlxy.mapper;
import com.dlxy.entity.Product;
import java.util.List;
public interface ProductMapper {
// 查询所有商品
List<Product> findAll();
// 新增商品
void addProduct(Product product);
// 根据id删除商品
void deleteProduct(Integer id);
// 修改商品信息
void updateProduct(Product product);
}
6、编写Products.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- namespace:接口的名字 -->
<mapper namespace="com.dlxy.mapper.ProductMapper">
<select id="findAll" resultType="com.dlxy.entity.Product">
select*from products
</select>
<insert id="addProduct" parameterType="com.dlxy.entity.Product">
INSERT INTO products
VALUES (#{id}, #{name}, #{price}, #{description})
</insert>
<update id="updateProduct" parameterType="com.dlxy.entity.Product">
UPDATE products SET name = #{name}, price = #{price}, description = #{description}
WHERE id = #{id}
</update>
<delete id="deleteProduct" parameterType="int">
DELETE FROM products WHERE id = #{id}
</delete>
</mapper>
7、编写测试类
package com.dlxy;
import com.dlxy.entity.Product;
import com.dlxy.mapper.ProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMybatis {
@Test
public void test1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);
ProductMapper productMapper = session.getMapper(ProductMapper.class);
//开启分页查询
PageHelper.startPage(1,10);
//得到所有的User对象
List<Product> productList = productMapper.findAll();
//按照分页参数进行分页,分页之后会将数据封装到PageInfo对象中
PageInfo<Product> pageInfo = new PageInfo<Product>(productList);
for (Product product: pageInfo.getList()){
System.out.println("product"+product);
}
//关闭SqlSession
session.close();
}
@Test
public void test2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);
ProductMapper productMapper = session.getMapper(ProductMapper.class);
Product product = new Product(null,"iphone16",5999,"新款苹果手机");
productMapper.addProduct(product);
System.out.println("新增加的商品为: " + product);
session.close();
}
@Test
public void test3() throws IOException{//删除
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);
ProductMapper productMapper = session.getMapper(ProductMapper.class);
productMapper.deleteProduct(6);
System.out.println("已删除");
session.close();
}
@Test
public void test4() throws IOException{//修改
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);
ProductMapper productMapper = session.getMapper(ProductMapper.class);
Product product= new Product(6, "更新后的商品", 299.99, "更新后的商品描述");
productMapper.updateProduct(product);
System.out.println("修改后的商品为: " + product);
session.close();
}
}
8.进行各个功能的测试(增删改查)
查询所有商品
增加商品:
根据id删除商品
修改商品