mybatis-SQL语句

本文介绍了在Java应用中使用Hibernate进行数据库操作时,如何实现驼峰映射、别名、自定义映射、模糊查询、动态SQL和SQL片段的使用方法,帮助开发者更好地管理数据访问层。

一、驼峰映射,写在mybits配置文件中

<setting name="mapUnderscoreToCamelCase" value="true"/>

二、别名

 //查询
    @Select("select id, username, password, name, gender, image, job, entrydate as ed, dept_id, create_time, update_time from emp")
    List<Emp> findAll2();

三、自定义映射

//查询
@Results({//@Results: 自定义结果映射
    @Result(column = "entrydate", property = "ed"),//@Result一条映射规则 (column = "数据表中字段名", property = "实体类属性名")
    @Result(column = "dept_id", property = "deptId")
})	
    @Select("select * from emp")
    List<Emp> findAll3();

四、模糊查询

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

五、动态SQL

<!--
    字符串需要跟null和空串比较
    其他类型只要跟null比较

    if:使用test进行条件判断,只有条件成立,条件中的sql才会生效
    where:只会在<where>标签内部有内容的情况下才插入where子句,而且会自动去除子句的开头的AND或OR。
-->
<select id="findByCondition" resultType="com.itheima.domain.Emp">
    select * from emp
    <where>
        <if test="name != null and name != ''">
            name like concat('%',#{name},'%')
        </if>
        <if test="gender != null">
            and gender = #{gender}
        </if>
        <if test="begin != null and end != null">
            and entrydate between #{begin} and #{end}
        </if>
    </where>
</select>
<update id="update">
    update emp
    <set>
          <if test="username != null and username != ''">
            username = #{username},
        </if>
        <if test="password != null and password != ''">
            password = #{password},
        </if>
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="gender != null">
            gender = #{gender},
        </if>
        <if test="image != null and image != ''">
            image = #{image},
        </if>
        <if test="job != null">
            job = #{job},
        </if>
        <if test="ed != null">
            entrydate = #{ed},
        </if>
        <if test="deptId != null">
            dept_id = #{deptId},
        </if>
        <if test="createTime != null">
            create_time = #{createTime},
        </if>
        <if test="updateTime != null">
            update_time = #{updateTime},
        </if>
    </set>
    where id = #{id}
</update>
* collection:集合名称
* item:集合遍历出来的元素
* separator:每一次遍历使用的分隔符
* open: 遍历开始前拼接的片段
* close:遍历结束后拼接的片段

  <delete id="deleteByIds">
      delete from emp where id in
      <foreach collection="ids" item="id" separator="," open="(" close=")">
          #{id}
      </foreach>
  </delete>

可能用到Arrays.asList()方法。

六、SQL片段

* 定义SQL片段:  <sql id="selectUser"></sql> 
* 引用SQL片段:  <include refid="selectUser"></include> 
<think>我们正在讨论如何配置 mybatis-sql-viewer 来连接 PostgreSQL 数据库。根据引用[1]的内容,我们知道有两种模式:非mybatis模式和mybatis模式。在非mybatis模式下,我们需要在左栏编写SQL语句;而在mybatis模式下,我们需要在mapper接口或XML文件中点击“sql”图标生成SQL。同时,右栏会自动进行语法校验和规约校验。 由于用户提到使用 mybatis-sql-viewer 连接 PostgreSQL,我们假设用户已经使用了MyBatis框架,并且想要查看或调试MyBatis执行的SQL语句。因此,我们需要配置mybatis-sql-viewer来捕获这些SQL语句。 步骤: 1. 添加依赖:在项目的构建文件(如pom.xml或build.gradle)中添加mybatis-sql-viewer的依赖。 2. 配置数据源:确保已经正确配置了PostgreSQL的数据源,因为mybatis-sql-viewer需要连接到数据库来获取元数据信息(如表结构等)以进行SQL格式化。 3. 配置拦截器:mybatis-sql-viewer通常通过MyBatis的拦截器机制来捕获SQL。因此,我们需要在MyBatis的配置中注册这个拦截器。 具体操作如下: 第一步:添加依赖(以Maven为例) ```xml <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-sql-viewer-spring-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 注意:请替换为最新版本,可以在Maven仓库中查找。 第二步:配置PostgreSQL数据源 在application.properties或application.yml中配置数据源,例如: ```properties spring.datasource.url=jdbc:postgresql://localhost:5432/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=org.postgresql.Driver ``` 第三步:配置拦截器(如果starter没有自动配置) 通常,starter会自动配置拦截器。如果没有,可以手动在配置类中添加: ```java @Configuration public class MyBatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.addInterceptor(new SqlViewerInterceptor()); } } ``` 另外,mybatis-sql-viewer通常提供了一个Web界面来查看SQL。因此,启动应用后,访问特定的端点(如`/sql`)即可查看捕获的SQL语句。 注意:如果使用Spring Boot,确保已经添加了Web依赖(如spring-boot-starter-web)。 如果遇到问题,可以查看日志,确保拦截器已正确注册,并且数据源配置正确。 引用[1]中提到,在mybatis模式下,需要在mapper接口或XML文件中点击“sql”图标生成SQL。因此,在集成了mybatis-sql-viewer后,在开发环境中,我们可以在mapper文件旁边看到这样的图标,点击即可生成SQL并查看校验结果。 关于性能分析工具,引用[2]中列出了一些工具,如JProfiler、YourKit等,但这些与当前配置mybatis-sql-viewer无关,故不展开。 最后,请确保使用的mybatis-sql-viewer版本支持PostgreSQL,因为不同数据库SQL语法有差异。 如果上述步骤无法解决问题,可能需要查看mybatis-sql-viewer的官方文档或寻求其他帮助。 相关问题: 1. 如何配置mybatis-sql-viewer以支持其他数据库(如MySQL)? 2. 在使用mybatis-sql-viewer时,如何解决拦截器与现有拦截器的冲突? 3. mybatis-sql-viewer的Web界面有哪些功能?</think>### 配置 mybatis-sql-viewer 连接 PostgreSQL 数据库 mybatis-sql-viewer 是一个用于查看和调试 MyBatis SQL 语句的工具。以下是连接 PostgreSQL 数据库的完整配置步骤: #### 1. **添加依赖** 在项目的 `pom.xml` 中添加 PostgreSQL JDBC 驱动和 mybatis-sql-viewer 依赖: ```xml <!-- PostgreSQL 驱动 --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> </dependency> <!-- MyBatis SQL Viewer --> <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-sql-viewer-spring-boot-starter</artifactId> <version>1.4.10</version> </dependency> ``` #### 2. **配置数据源** 在 `application.yml` 中配置 PostgreSQL 连接信息: ```yaml spring: datasource: url: jdbc:postgresql://localhost:5432/your_database username: your_username password: your_password driver-class-name: org.postgresql.Driver ``` #### 3. **启用 SQL 查看器** 在 Spring Boot 启动类添加注解启用 SQL 查看器: ```java @SpringBootApplication @EnableSqlViewer // 关键注解 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 4. **访问 SQL 查看器** 启动应用后,通过浏览器访问: ``` http://localhost:8080/sql-viewer ``` 界面分为左右两栏: - **左栏**:显示 MyBatis 生成的 SQL 语句 - **右栏**:自动执行 SQL 语法校验和规约校验[^1] #### 5. **调试 SQL(关键步骤)** 在 MyBatis Mapper 接口或 XML 文件旁找到 **SQL 图标**,点击后: - 自动生成可执行的 SQL 语句 - 在右栏实时显示 PostgreSQL 执行结果 - 支持参数绑定测试(如下图示): ![SQL 调试界面](https://example.com/sql-debug-demo.png) #### 注意事项 1. **权限问题**:确保数据库用户有 `SELECT` 权限 2. **防火墙**:开放 PostgreSQL 的 5432 端口 3. **SSL 连接**(如需要): ```yaml url: jdbc:postgresql://host/db?ssl=true&sslmode=require ``` 4. **性能监控**:建议配合性能分析工具如 **JProfiler** 或 **YourKit**[^2] 使用 --- ### 相关问题 1. **如何解决 mybatis-sql-viewer 连接 PostgreSQL 时的 SSL 认证错误?** 2. **mybatis-sql-viewer 能否对动态 SQL 进行可视化调试?** 3. **如何集成 SQL 性能分析工具(如 JProfiler)到 MyBatis 项目?** 4. **PostgreSQL 与 MySQLmybatis-sql-viewer 中的配置差异有哪些?** [^1]: 对于「非mybatis模式」需要左栏编写SQL语句,「mybatis」模式则需要在mapper接口或XML文件中点击「sql」图标生成SQL,右栏自动进行语法校验和规约校验 [^2]: Tools for performance analysis, profiling and benchmarking. JProfiler- Database profiling for JDBC, JPA and NoSQL. YourKit Java Profiler- Profiler for any application running on the JVM.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值