首页 > 编程语言 > IDEA 格式化SQL代码技巧分享
2021
02-26

IDEA 格式化SQL代码技巧分享

写在前面

IntelliJ IDEA集成了很多我们开发中常用的工具,Linux连接,Git,数据库操作…

这里我们介绍,IDEA集成数据库操作,并对SQL可格式化…

通过命令,但是在合适的执行文件中…

Ctrl Alt L

1.1、建立连接,数据库服务器地址,账号,密码等…

1.2、基本操作,很多,增删改查,控制台SQl编写等等

SQL执行控制台,这里是格式化之后的,我们平时手写的SQL都是没有格式化的,注意这里提供了输入框,操作占位符的参数也很方便…

那么这里的格式化的SQL是怎么实现的呢??

这里的哈,IDEA的快捷键 Ctrl + Alt + L,就可以格式化SQL

补充:解决idea的src目录下不能编译SQL语句的xml配置文件的问题

一:正常配置下的情况

1.编译的目录下不会编译映射配置文件.xml,但是resources目录中的文件都被编译出来。

2.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>com.sly</groupId>
 <artifactId>hrm01</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <name>hrm01 Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
 </properties>
 
 <dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
 </dependency>
 <!-- 导入jar包
 1.spring的jar包:11个
 com.springsource.org.aopalliance-1.0.0.jar
 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
 spring-aop-5.0.1.RELEASE.jar
 spring-aspects-5.0.1.RELEASE.jar
 spring-beans-5.0.1.RELEASE.jar
 spring-context-5.0.1.RELEASE.jar
 spring-core-5.0.1.RELEASE.jar
 spring-expression-5.0.1.RELEASE.jar
 spring-jcl-5.0.1.RELEASE.jar
 spring-jdbc-5.0.1.RELEASE.jar
 spring-tx-5.0.1.RELEASE.jar
 -->
  <!-- https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance -->
  <dependency>
  <groupId>org.aopalliance</groupId>
  <artifactId>com.springsource.org.aopalliance</artifactId>
  <version>1.0.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.aspectj/com.springsource.org.aspectj.weaver -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>com.springsource.org.aspectj.weaver</artifactId>
   <version>1.6.4.RELEASE</version>
  </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>5.0.10.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>5.1.0.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jcl</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <!--导入jar包
 2.springmvc的jar包:2个
 spring-web-5.0.1.RELEASE.jar
 spring-webmvc-5.0.1.RELEASE.jar
 3.MyBatis的jar包:13个
 mybatis-3.4.2.jar
 依赖jar包12个
 4.MyBatis和Spring整合的jar包:1个
 mybatis-spring-1.3.1.jar
 -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.0.1.RELEASE</version>
 </dependency>
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.2</version>
 </dependency>
 <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
 </dependency>
 <!--导入jar包
5.数据库驱动jar包:1个
mysql-connector-java-5.1.7-bin.jar
6.数据源druid的jar包:1个
druid-1.1.5.jar
7.JSTL的jar包:2个
jstl-1.1.2 .jar
standard-1.1.2.jar
-->
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.48</version>
 </dependency>
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.5</version>
 </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.1.2</version>
 </dependency>
 <dependency>
  <groupId>taglibs</groupId>
  <artifactId>standard</artifactId>
  <version>1.1.2</version>
 </dependency> 
<!--导入Tomcat的jar包-->
 <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-servlet-api -->
 <dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-servlet-api</artifactId>
  <version>9.0.21</version>
 </dependency> 
 </dependencies> 
 <build>
 <finalName>hrm01</finalName>
 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  <plugins>
  <plugin>
   <artifactId>maven-clean-plugin</artifactId>
   <version>3.1.0</version>
  </plugin>
  <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  <plugin>
   <artifactId>maven-resources-plugin</artifactId>
   <version>3.0.2</version>
  </plugin>
  <plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.0</version>
  </plugin>
  <plugin>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.22.1</version>
  </plugin>
  <plugin>
   <artifactId>maven-war-plugin</artifactId>
   <version>3.2.2</version>
  </plugin>
  <plugin>
   <artifactId>maven-install-plugin</artifactId>
   <version>2.5.2</version>
  </plugin>
  <plugin>
   <artifactId>maven-deploy-plugin</artifactId>
   <version>2.8.2</version>
  </plugin>
  </plugins>
 </pluginManagement> 
 </build>
</project>

3.控制台无报错且网页报错404 找不到,或者500如下错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userHandler': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserDao' defined in file [D:\Tomcat\apache-tomcat-9.0.27\webapps\hrm01_war\WEB-INF\classes\com\hrm\user\dao\IUserDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Wrong namespace. Expected 'com.hrm.user.dao.IUserDao' but found 'com.hrm.user.dao.UserDao'.

二、解决方案

1.在pom文件中的build标签下配置该属性即可解决这个问题,但是要注意src前面不要加 / 。

一定不要写为:(/src/main/java)。

这样在编译目录下xml文件也被编译。

2.加入该配置后编译目录下有了映射配置文件,见上图IUserDao.xml。

控制台可以正常显示数据。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。如有错误或未考虑完全的地方,望不吝赐教。

编程技巧