javaweb杂七杂八

maven

在setting下搜索maven
maven压缩包 提取码:6666
在这里插入图片描述

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>maven_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>

          <!--添加lsf4j日志api-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.20</version>
        </dependency>

        <!--添加logback-classic 依赖-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!--添加logback-core依赖-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>
    </dependencies>

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>80</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
   <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

     <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.5</version>
    </dependency>
  
  </dependencies>


</project>

Mapper 代理开发

sql映射文件mapper接口
注意:directory/分隔
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Mybatis

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
按照顺序

在这里插入图片描述
在这里插入图片描述

查询操作

方法一
在这里插入图片描述

方法二

在这里插入图片描述

id:完成主键字段的映射
columu:表的列名
proterty:实体类的属性名
1.转义字符
2.CDATA  区

条件查询三种方法

 @Test
    public void testSelectByCondition() throws IOException {

        int status = 0;
        String companyName = "三只";
        String brandName = "华为";


        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";

        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);

        Map map = new HashMap();
        map.put("status",status);
        map.put("brandName",brandName);
        map.put("companyName",companyName);

        //1、加载mybatis和信念配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        //方法一
        //List<Brand> brands = brandMapper.selectByCondition(status,companyName,brandName);

        //方法二
        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);

        //方法三

        sqlSession.close();
    }

接口

package com.mapper;


import com.st.projo.Brand;
import jdk.nashorn.internal.runtime.ListAdapter;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

public interface BrandMapper {

    public List<Brand> selectAll();

    public Brand selectBYId(int id);

    List<Brand> selectByCondition(@Param("status")int status, @Param("companyName")String companyName, @Param("brandName") String brandName);

    List<Brand> selectByCondition(Brand brand);

    List<Brand> selectByCondition(Map map);
}

动态sql

 <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
      <where>
              <if test="status!=null">
                  status = #{status}
              </if>
              <if test="companyName">
                  and company_name like #{companyName}
              </if>
              <if test="brandName">
                  and brand_name like #{brandName}
              </if>
       </where>

    </select>

但条件查询

 <select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
       <choose>
           <when test="status!=null">
               status = #{status}
           </when>
           <when test="companyName">
               company_name like #{companyName}
           </when>
           <when test="brandName">
                brand_name like #{brandName}
           </when>
           <otherwise> 1 = 1 </otherwise>
       </choose>
        </where>
    </select>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改

   <update id="update">
      update tb_brand
      set
          brand_name = #{brandName},
          company_name = #{companyName},
          ordered = #{ordered},
          description = #{description},
          status = #{status}
      where id = #{id};

    </update>

动态修改

<update id="update">
      update tb_brand
      <set>
          <if test="brandName!=null and brandName != '' ">
              brand_name = #{brandName},
          </if>
          <if test="companyName != null and companyName != '' ">
              company_name = #{companyName},
          </if>
          <if test="ordered != null">
              ordered = #{ordered},
          </if>
          <if test="description != null and description != '' ">
              description = #{description},
          </if>
          <if test="status != null">
              status = #{status}
          </if>
      </set>
      where id = #{id};

批量删除
在这里插入图片描述
注解式
在这里插入图片描述
在这里插入图片描述

posted @   三枪一个麻辣烫  阅读(2)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示