Spring实践作业4——MyBatis配置与操作

一、实验目的

掌握Mybatis基本环境搭建流程

掌握核心配置文件mybatis-config.xml的配置

掌握映射文件*Mapper.xml配置

掌握基本的增加和查询操作流程

二、实验内容

创建项目并运用Mybatis框架技术完成增加和查询功能

三、实验内容与要求

创建项目并引入依赖

创建实体类Product(id, product_name, description, price, images, inventory, is_shelves, add_time, update_time)

创建并配置核心配置文件mybatis-config.xml

创建并配置映射文件ProductMapper.xml

通过Junit完成CRUB各项功能

四、程序设计

截图,代码,关键步骤,结果验证

1. 数据库表

2. 产品测试添加

 

产品测试添加结果

 

3. 产品测试更新

产品测试更新结果

 

4. 产品测试删除

产品测试删除结果

5. 产品测试查询

产品测试查询结果

五、相关代码具体如下

package com.gyn.mybatis.entity;
import java.util.Date;
public class Product {
    private Integer id;
    private String product_name;
    private String description;
    private Float price;
    private String images;
    private Integer inventory;
    private Integer is_shelves;
    private Date add_time;
    private Date update_time;
    public Integer getId() {
        return id;
    } public void setId(Integer id) {
        this.id = id;
    }
    public String getProduct_name() {
        return product_name;
    } public void setProduct_name(String product_name) {
        this.product_name = product_name;
    } public String getDescription() {
        return description;
    } public void setDescription(String description) {
        this.description = description;
    } public Float getPrice() {
        return price;
    } public void setPrice(Float price) {
        this.price = price;
    } public String getImages() {
        return images;
    } public void setImages(String images) {
        this.images = images;
    } public Integer getInventory() {
        return inventory;
    } public void setInventory(Integer inventory) {
        this.inventory = inventory;
    } public Integer getIs_shelves() {
        return is_shelves;
    } public void setIs_shelves(Integer is_shelves) {
        this.is_shelves = is_shelves;
    }

    public Date getAdd_time() {
        return add_time;
    } public void setAdd_time(Date add_time) {
        this.add_time = add_time;
    } public Date getUpdate_time() {
        return update_time;
    } public void setUpdate_time(Date update_time) {
        this.update_time = update_time;
    }
    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", product_name='" + product_name + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                ", images='" + images + '\'' +
                ", inventory=" + inventory +
                ", is_shelves=" + is_shelves +
                ", add_time=" + add_time +
                ", update_time=" + update_time +
                '}';
    }
}

package com.gyn.mybatis.mapper;
import com.gyn.mybatis.entity.Product;
import java.util.List;
public interface ProductMapper {
    //添加产品信息
    int insertProduct();
    //更新产品
    int updateProduct();
    //删除产品
    int deleteProduct();
    //查询产品详情
    Product getProductById();
    //查询产品列表
    List<Product> getAllProduct();
}

 

<?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.gyn.mybatis.mapper.ProductMapper">
    <insert id="insertProduct">
        INSERT INTO w_product VALUES(null,'手办1','眼镜厂出产',100,'图片1',2000,1,now(),null)
    </insert>
    <update id="updateProduct">
        UPDATE w_product SET product_name='手办2' , update_time = now() WHERE id = 2
    </update>
    <update id="deleteProduct">
        DELETE FROM w_product WHERE id = 1
    </update>
    <!--查询必须指定resultType或者resultMap-->
    <select id="getProductById" resultType="Product">
        select * from w_product where id = 2
    </select>
    <select id="getAllProduct" resultType="Product">
        select * from w_product
    </select>
</mapper>
        <!--
        5.编写mapepr配置文件-->

dev.driverClass=com.mysql.jdbc.Driver
dev.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
dev.username=root
dev.password=123456
<?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>
    <!--2.配置mybatis核心配置文件-->
    <!--(properties?,settings?,typeAliases?,typeHandlers?,
    objectFactory?,objectWrapperFactory?,reflectorFactory?,
    plugins?,environments?,databaseIdProvider?,mappers?)".-->
    <properties resource="jdbc.properties"></properties>
    <typeAliases><!--别名,不区分大小写-->
        <package name="com.gyn.mybatis.entity"/><!--该目录下所有类都默认设置类名的别名-->
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <!-- JDBC/MANAGED-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED"><!--数据源,POOLED/UNPOOLED/JNDI-->
                <property name="driver" value="${dev.driverClass}"/>
                <property name="url" value="${dev.url}"/>
                <property name="username" value="${dev.username}"/>
                <property name="password" value="${dev.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers><!--指定映射文件的路径-->
        <!--<mapper resource="mappers/UserMapper.xml"/>-->
        <package name="com.gyn.mybatis.mapper"/><!--引入该包下的所有映射文件,
        接口名必须和映射文件名相同-->
    </mappers>
</configuration>
package com.gyn.mybatis.test;
import com.gyn.mybatis.entity.Product;
import com.gyn.mybatis.mapper.ProductMapper;
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 MybatisTest {
    @Test
    public void testInsertProduct() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSession工厂对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession(true);//自动提交
        //获取Mapper接口对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        int i = mapper.insertProduct();
        System.out.println("result "+i);
    }
    @Test
    public void testUpdateProduct() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSession工厂对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession(true);//自动提交
        //获取Mapper接口对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        int i = mapper.updateProduct();
        System.out.println("result "+i);
    }
    @Test
    public void testDeleteProduct() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSession工厂对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession(true);//自动提交
        //获取Mapper接口对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
        int i = mapper.deleteProduct();
        System.out.println("result "+i);
    }
    @Test
    public void testGetProduct() throws IOException {
        //加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        //获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //获取SqlSession工厂对象
        SqlSessionFactory build = builder.build(resourceAsStream);
        //获取SqlSession
        SqlSession sqlSession = build.openSession(true);//自动提交
        //获取Mapper接口对象
        ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);

        Product productById = mapper.getProductById();
        System.out.println(productById);
        List<Product> allProduct = mapper.getAllProduct();
        System.out.println(allProduct);
    }

六、实验总结

通过这次实验,我对于Mybatis基本环境搭建流程有了熟悉的了解,通过实践操作熟悉了几个配置的操作:核心配置文件mybatis-config.xml的配置,映射文件ProductMapper.xml的配置和基本的增加和查询操作流程。

本次实验中,我察觉到自己对于sql语句的应用有些生疏,通过查阅资料复习了相关部分内容,加深了印象,在今后的学习中,应当也要时常温故知新。 

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

米莱虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值