发表于: 2020-03-22 22:38:11

1 1378


学习了SQL动态语句

参考:http://www.cnblogs.com/ysocean/p/7289529.html

静态SQL,在编译阶段就可以确定数据库要做什么事情。在某种高级语言中,如果嵌入了SQL语句,而这个SQL语句的主体结构已经明确,

例如在Java的一段代码中有一个待执行的SQL“select * from t1 where c1>5”,在Java编译阶段,就可以将这段SQL交给数据库管理系统去分析,数据库软件可以对这段SQL进行语法解析,生成数据库方面的可执行代码,这样的SQL称为静态SQL。

动态SQL,而如果嵌入的SQL没有明确给出,如在Java中定义了一个字符串类型的变量sql:String sql;,然后采用preparedStatement对象的execute方法去执行这个sql,该sql的值可能等于从文本框中读取的一个SQL或者从键盘输入的SQL,但具体是什么,在编译时无法确定,只有等到程序运行起来,在执行的过程中才能确定,这种SQL叫做动态SQL。

动态SQL:if 语句、还有 if+where语句等

根据 username 和 sex 来查询数据。如果username为空,那么将只根据sex来查询;反之只根据username来查询

首先不使用 动态SQL 来书写

<select id="selectUserByUsernameAndSex" 

 resultType="user" parameterType="com.ys.po.User">

 <!-- 这里和普通的sql 查询语句差不多,对于只有一个参数,后面的 #{id}表示占位符,里面不一定要写id,   写啥都可以,但是不要空着,如果有多个参数则必须写pojo类里面的属性 --> 

 select * from user where username=#{username} and sex=#{sex} 

 </select>

上面的查询语句,我们可以发现,如果 #{username} 为空,那么查询结果也是空,如何解决这个问题呢?使用 if 来判断

<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">

select * from user where

<if test="username != null">

username=#{username} 

</if> 

<if test="username != null">

 and sex=#{sex} 

</if> 

</select>

2、了解了Mysql数据类型-整数数据类型(bigint、int、smallint、tinyint)

参考:https://baijiahao.baidu.com/s?id=1621991150198350613&wfr=spider&for=pc

bigint型数据的存储大小为8个字节,共64位。其中63位用于表示数值的大小,1位用于表示符号。bigint型数据可以存储的数值范围是-263~263-1,即 -9 223 372 036 854 775 808 ~9 223 372 036 854 775 807。 在应用中除非明确说明,否则那些接受int表达式作为其参数的函数、语句和系统存储过程都不会改变,从而不会支持将 bigint表达式隐式转换为这些参数。因此,当 bigint值在int数据类型支持的范围内时,SQL Server 才将 bigint隐式转换为int。如果 bigint表达式包含了一个在int数据类型支持范围之外的值,就会在运行时出现转换错误。

tinyint型数据的存储大小只有1个字节,共8位,全部用于表示数值的大小,由于没有符号位,所以tinyint型的数据只能表示正整数。tinyint型数据存储的数值范围是0~255。TINYINT类型的字段如果不设置UNSIGNED类型,存储-128到127的整数。一个TINYINT型数据只占用一个字节,一个INT型数据占用四个字节。这看起来似乎差别不大,但是在比较大的表中,字节数的增长是很快的。


学习使用mybatis-generator自动生成代码

需要在pom文件中加入插件,注意这个地方的<plugins></plugins>要和<pluginManagement></pluginManagement>同级,不然会导不了

<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>

然后编写配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
       PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
       "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

<!--指定特定数据库的jdbc驱动jar包的位置-->
   <classPathEntry location="E:\Maven\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar"/>

<context id="mysqlTables" targetRuntime="MyBatis3">

<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
           <property name="suppressAllComments" value="true"/>
</commentGenerator>

<!--jdbc的数据库连接 -->
       <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/task?characterEncoding=UTF-8"
                       userId="root"
                       password="admin123"/>

<!-- 非必需,类型处理器,指定生成的类型为java类型,在数据库类型和java类型之间的转换控制-->
       <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>

<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
           targetPackage     指定生成的model生成所在的包名
           targetProject     指定在该项目下所在的路径
       -->
       <javaModelGenerator targetPackage="com.artroom.model" targetProject="src/main/java">
<!-- 是否对model添加 构造函数 -->
           <property name="constructorBased" value="true"/>
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
           <property name="enableSubPackages" value="false"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
           <property name="trimStrings" value="true"/>
</javaModelGenerator>



<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
       <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- 是否允许子包 -->
           <property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
               type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
               type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
               type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
       -->
       <javaClientGenerator targetPackage="com.artroom.mapper" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>


<!--tableName:指定了表名
           domainObjectName:指定了实体类的名称
       -->
       <table tableName="banner" domainObjectName="Banner" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="collection" domainObjectName="Collection" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="manager" domainObjectName="Manager" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="module" domainObjectName="Module" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="track" domainObjectName="Track" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="visitor" domainObjectName="Visitor" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="works" domainObjectName="Works" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
<table tableName="workshop" domainObjectName="Workshop" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>

点击插件后


别的还没写 

今日问题 没有头绪 有点不知道 干嘛



返回列表 返回列表
评论

    分享到