创建mybatis项目+Mybatis对数据库的增删改查操作

一、创建Mybatis项目

1.创建maven项目

2.在mybatis项目的pom.xml文件中添加mybatis依赖

	<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.0</version>
    </dependency>

在这里插入图片描述

3.创建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>
    <!-- 配置数据源相关属性和事务 -->
    <environments default="development">
        <!-- 可以配置多个数据源环境,默认使用default中的值 -->
        <environment id="development">
            <!-- 使用jdbc的事务管理 -->
            <transactionManager type="JDBC" />
            <!-- 配置数据源,并使用自带数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url"
                          value="jdbc:mysql://localhost:3306/business?serverTimezone=UTC&amp;characterEncoding=utf-8&amp;useSSL=false" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件,可配置多个 -->

</configuration>

在这里插入图片描述

4.创建pojo类

在这里插入图片描述

5.进行mybatis操作(查询所有食品信息)

在src-》main-》resources文件夹下新建OpFoodMapper.xml文件,在里面填写mybatis操作代码

<?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="foodMapper">
    <!--    查询所有食品信息-->
    <!--    id  方法名称       resultType 返回值类型-->
    <select id="getAllFoods" resultType="com.neusoft.pojo.Food">
            select * from food
    </select>
</mapper>

在这里插入图片描述

6.测试查询

在src-》test-》java文件夹下新建Test类,在里面测试代码:

	import com.neusoft.pojo.Food;
	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 java.io.IOException;
	import java.io.InputStream;
	import java.util.List;
	
	public class Test {
	    public static void main(String[] args) {
	//        调用mybatis的数据库查询方法
	//    1.加载配置文件
	        InputStream inputStream = null;
	        try {
	//            1.加载配置文件
	            inputStream = Resources.getResourceAsStream("mybatis.xml");
	
	//            2.获取mybatis   数据库工厂类
	        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(inputStream);
	
	//                3.获取数据库连接
	        SqlSession sqlSession = ssf.openSession();
	        //        4.调用数据库查询方法
	//            4.1查询所有
	      List<Food> list = sqlSession.selectList("foodMapper.getAllFoods");
	        for (Food f:list){
	            System.out.println(f.toString());
	        }

	
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	
	    }
	
	}

7.配置映射文件

在Mybais配置文件中(即在src-》main-》resources文件夹下的mybatis.xml文件中)配置映射文件

 <!-- 配置映射文件,可配置多个 -->
    <mappers>
        <mapper resource="OpFoodMapper1.xml"/>
    </mappers>

在这里插入图片描述

二、Mybatis操作

  (1)增删改操作一定要加 sqlSession.commit();操作才能被提交到数据库,数据库中才能做相应的修改

  (2) id 方法名称

  (3)parameterType 指定参数类型

  (4) resultType 返回值类型(增删改操作中返回值为受影响的行数,默认为int,可不写)

   (5)com.neusoft.pojo.Business(当返回值类型或指定参数类型为对象时,要写对应的pojo类)

1.查询所有商家信息

操作代码:

 <select id="findAll" resultType="com.neusoft.pojo.Business">
        select * from business
    </select>

测试代码:

  List<Business> list = sqlSession.selectList("BusinessMapper.findAll");
       for (Business b: list) {
            System.out.println(b.toString());
       }

2.添加商家信息

测试代码

		    Business b1 = new Business();
            b1.setBusinessName("肯德基");
            b1.setBusinessAddress("东北大学");
            b1.setBusinessExplain("周三特价");
            b1.setBusinessImg("http://fdfsad.jpg");
            b1.setOrderTypeId(30);
            b1.setStarPrice(30);
            b1.setDeliveryPrice(5);
            b1.setRemarks("无");
            b1.setPassword("123456");

操作代码

 <insert id="insertBusi" parameterType="com.neusoft.pojo.Business">
        insert into business(businessName, businessAddress, businessExplain, businessImg, orderTypeId, starPrice, deliveryPrice, remarks, password)
        values (#{businessName},#{businessAddress},#{businessExplain},#{businessImg},#{orderTypeId},#{starPrice},#{deliveryPrice},#{remarks},#{password})
    </insert>

3.更新商家信息

操作代码

 <update id="updateBusi" parameterType="com.neusoft.pojo.Business">
        update business set businessName=#{businessName},password=#{password} where businessId=#{businessId}
    </update>

实现代码

 		   Business b1 = new Business();
            b1.setBusinessName("益禾堂1");
            b1.setPassword("123456");
            b1.setBusinessId(3);

            int n = sqlSession.insert("BusinessMapper.updateBusi", b1);
            sqlSession.commit();
            if (n > 0) {
                System.out.println("信息修改成功");
            }
            else {
                System.out.println("信息修改失败");
            }

4.删除商家

操作代码:

    <delete id="deleteBusi" parameterType="int">
        delete from business where businessId=#{businessId}
    </delete>

实现代码:

			 int n =  sqlSession.delete("BusinessMapper.deleteBusi",10);
            sqlSession.commit();
            if (n > 0) {
                System.out.println("信息删除成功");
            }
            else {
                System.out.println("信息删除失败");
            }

5.单条件查询

操作代码:

    <select id="selectByName" parameterType="String" resultType="com.neusoft.pojo.Business">
        select * from business where businessName=#{businessName}
    </select>

实现代码:

            Business b2 = sqlSession.selectOne("BusinessMapper.selectByName","肯德基");
            System.out.println(b2.toString());

6.多条件查询

操作代码:

   <!--    id  方法名称    parameterType 指定参数类型(当多个参数时,类型为对象)   resultType 返回值类型-->
    <!--参数为对象的属性(即填写pojo 类中的属性名)-->
   <select id="selectByConditions" parameterType="com.neusoft.pojo.Business" resultType="com.neusoft.pojo.Business">
       select * from business where businessName=#{businessName} or businessAddress=#{businessAddress}
    </select>

实现代码:

  			Business b3 = new Business();
            b3.setBusinessName("好想见面");
            b3.setBusinessAddress("红豆街道");
            List<Business> list = sqlSession.selectList("BusinessMapper.selectByConditions",b3);
            for (Business b: list) {
                System.out.println(b.toString());
            }

7.模糊查询()

   concat()为字符串拼接函数

操作代码:

<select id="selectBusiMohu" parameterType="String" resultType="com.neusoft.pojo.Business">
        select * from business where businessName like concat("%",#{businessName},"%")
    </select>

实现代码:

    List<Business> list = sqlSession.selectList("BusinessMapper.selectBusiMohu");
            for (Business b: list) {
                System.out.println(b.toString());
            }

8.实现任意更改店铺的姓名、地址、简介中的任意一项

操作代码:

    <update id="updateBusiByWhere" parameterType="com.neusoft.pojo.Business" >
        update business
        <set>
            <if test="businessName != null and businessName != ''">
                businessName = #{businessName},
            </if>
            <if test="businessAddress != null and businessAddress != ''">
                businessAddress = #{businessAddress},
            </if>
            <if test="businessExplain != null and businessExplain != ''">
                businessExplain = #{businessExplain},
            </if>
        </set>
            where  businessId = #{businessId}
    </update>

实现代码:

			 Business b1 = new Business();
            b1.setBusinessId(6);
//            b1.setBusinessName("益禾堂1");

            b1.setBusinessAddress("秦皇岛");
//            b1.setBusinessExplain("奶茶超甜");
            b1.setBusinessId(3);

            int n = sqlSession.insert("BusinessMapper.updateBusiByWhere", b1);
            sqlSession.commit();
            if (n > 0) {
                System.out.println("信息修改成功");
            }
            else {
                System.out.println("信息修改失败");
            }

9.实现根据店铺的姓名、地址、起送价搜索店铺

操作代码

		<!--互斥条件时的查询用choose-when-->
    <select id="selectBusiByWhere" parameterType="com.neusoft.pojo.Business" resultType="com.neusoft.pojo.Business">
        select  * from business
            <where>
                <choose>
                    <when test="businessName != null and businessName != ''">
                        and businessName = #{businessName}
                    </when>
                    <when test="businessAddress != null and businessAddress != ''">
                        and businessAddress = #{businessAddress}
                    </when>
                    <otherwise>
                        and starPrice = #{starPrice}
                    </otherwise>
                </choose>
            </where>
    </select>

实现代码:

  			Business b3 = new Business();
//            b3.setBusinessName("好想见面");
//            b3.setBusinessAddress("红豆街道");
             b3.setStarPrice(30);
            List<Business> list = sqlSession.selectList("BusinessMapper.selectBusiByWhere",b3);
            for (Business b: list) {
                System.out.println(b.toString());
            }

            Business b1 = new Business();
            b1.setBusinessName("益禾堂1");
            b1.setPassword("123456");
            b1.setBusinessId(3);

            int n = sqlSession.insert("BusinessMapper.updateBusi", b1);
            sqlSession.commit();
            if (n > 0) {
                System.out.println("信息修改成功");
            }
            else {
                System.out.println("信息修改失败");
            }

10.并列条件查询

操作代码:

 <select id="selectBusiByWhere1" parameterType="com.neusoft.pojo.Business" resultType="com.neusoft.pojo.Business">
        select  * from business
        <where>

                <if test="businessName != null and businessName != ''">
                    and businessName = #{businessName}
                </if>
                <if test="businessAddress != null and businessAddress != ''">
                    and businessAddress = #{businessAddress}
                </if>

        </where>
    </select>

实现代码:

  			 Business b3 = new Business();
//            b3.setBusinessName("好想见面");
//            b3.setBusinessAddress("红豆街道");
             b3.setStarPrice(30);
            List<Business> list = sqlSession.selectList("BusinessMapper.selectBusiByWhere1",b3);
            for (Business b: list) {
                System.out.println(b.toString());
            }

            Business b1 = new Business();
            b1.setBusinessName("益禾堂1");
            b1.setPassword("123456");
            b1.setBusinessId(3);

            int n = sqlSession.insert("BusinessMapper.updateBusi", b1);
            sqlSession.commit();
            if (n > 0) {
                System.out.println("信息修改成功");
            }
            else {
                System.out.println("信息修改失败");
            }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值