第十章作业

该文章是一个MyBatis入门教程,从创建数据库和表,导入数据开始,逐步讲解如何通过Eclipse创建Java项目,配置MyBatis,导入必要的jar包,创建实体类,编写MapperXML文件,使用动态SQL(如if标签)进行条件查询,以及测试类的实现。文章还涉及了数据操作,如增删改查的示例。
摘要由CSDN通过智能技术生成

一、

步骤 1: 

创建数据库

步骤 2: 

创建表

步骤 3: 

导入数据

不小心运行点了三下 就出现了6行数据

步骤 4: 

创建项目

通过ecipse创建java project: mybatis
项目目录必须是 e:\project\mybatis

 

步骤 5: 

导入jar包

本项目用到了两个jar包,在右上角的lib.rar中下载。
下载解压后,在项目目录e:/project/mybatis下新建目录lib,并复制过来
然后通过ecipse在mybatis的项目上导入这两个jar包

步骤 6: 

创建实体类

步骤 7: 

配置文件mybatis-config.xml

  

 步骤 8: 

配置文件Category.xml

 

 

 步骤 9: 

测试类TestMybatis

 

二、

 

步骤 4 : 

执行不同的条件限定,需要准备两条sql语句

假设需要对Product执行两条sql语句,一个是查询所有,一个是根据名称模糊查询。
那么按照现在的方式,必须提供两条sql语句:listProduct和listProductByName
然后在调用的时候,分别调用它们来执行。


<?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.how2java.pojo">
        <select id="listProduct" resultType="Product">
            select * from product_             
        </select>
        <select id="listProductByName" resultType="Product">
            select * from product_  where name like concat('%',#{name},'%')             
        </select>
        
    </mapper>


package com.how2java;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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 com.how2java.pojo.Product;
 
public class TestMybatis {
 
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
 
        System.out.println("查询所有的");
        List<Product> ps = session.selectList("listProduct");
        for (Product p : ps) {
            System.out.println(p);
        }
        
        System.out.println("模糊查询");
        Map<String,Object> params = new HashMap<>(); 
        params.put("name","a");
        List<Product> ps2 = session.selectList("listProductByName",params);
        for (Product p : ps2) {
            System.out.println(p);
        }       
        
        session.commit();
        session.close();
 
    }
}

步骤 5 : 

if标签

如果Product的字段比较多的话,为了应付各个字段的查询,那么就需要写多条sql语句,这样就变得难以维护。
这个时候,就可以使用Mybatis 动态SQL里的if标签

<select id="listProduct" resultType="Product">

select * from product_

<if test="name!=null">

where name like concat('%',#{name},'%')

</if>

</select>

三、

步骤 1: 

增加100条catgory数据

 ​​​​​​​

 步骤 2: 

修改Category.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.how2java.pojo">
        <insert id="addCategory" parameterType="Category" >
            insert into category_ ( name ) values (#{name})    
        </insert>
        
        <delete id="deleteCategory" parameterType="Category" >
            delete from category_ where id= #{id}   
        </delete>
        
        <select id="getCategory" parameterType="_int" resultType="Category">
            select * from   category_  where id= #{id}    
        </select>

        <update id="updateCategory" parameterType="Category" >
            update category_ set name=#{name} where id=#{id}    
        </update>
        <select id="listCategory" resultType="Category">
            select * from   category_ 
                 <if test="start!=null and count!=null">
                     limit #{start},#{count}
                </if>
        </select>        
    </mapper>

步骤 3: 

注解方式

步骤 4: 

测试

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值