come in handy

来源:互联网 发布:淘宝lee是正品吗 编辑:程序博客网 时间:2024/04/28 21:08

业务复杂度

业务上的复杂度是由其所需要的数据的存储方式决定的,所以数据存储(数据库或者其它)的设计犹为重要。

标识符说明

$ 这是钱,值钱的钱,重要的内容,表示这个内容很重要。
& 这个是与,与人交流,沟通,表示需要发周报的内容。
# 说明这个东西有问题,或者警告。

Jvm编码

Charset.defaultCharset() 可以获取到jam默认编码,默认为utf-8,可以通过参数 -Dfile.encoding=gbk设置,对于编码是gbk的项目来说,这样设置即可。

mybatis的sql语句

mybatis的sql语句在 org.apache.ibatis.executor.SimpleExecutor 类的各方法中。

数据库命名规则

命名规则是,每个都是小写字母,单词是用下划线分隔,不是pascal,也不是camel,每个表,都有修改时间,创建时间,修改人,创建人的字段。不要建立约束,只建索引。

maven项目编码问题

今天在DOS下执行mvn compile命令时报错说缺少必要符号,事实上根本就没有缺少,但何以如此呢,为啥eclipse在编译时就没有这问题呢?

原因是编码的问题造成的!

eclipse在编译的使用使用的是UTF-8,因为整个项目设置的是UTF-8格式编码。

当在DOS下执行mvn compile命令时,默认使用的是系统的GBK格式编码。

问题解决方案是在pom.xml文件中添加如下代码

<properties>    <!-- 文件拷贝时的编码 -->    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    <!-- 编译时的编码 -->    <maven.compiler.encoding>UTF-8</maven.compiler.encoding></properties>

或者添加如下代码:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-compiler-plugin</artifactId>    <version>3.1</version>    <configuration>        <source>1.5</source>        <target>1.5</target>        <encoding>UTF-8</encoding>        <!-- 指定编码格式,否则在DOS下运行mvn compile命令时会出现莫名的错误,因为系统默认使用GBK编码 -->    </configuration></plugin><plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-resources-plugin</artifactId>    <version>2.6</version>    <configuration>        <encoding>UTF-8</encoding>        <!-- 指定编码格式,否则在DOS下运行mvn命令时当发生文件资源copy时将使用系统默认使用GBK编码 -->    </configuration></plugin>

maven resources

maven 本身有resources目录,但是有时候有些资源文件并不是写在这里的,比如,为了方便把myBatis的mapper.xml文件写在和java同一个目录下。但是直接编译的时候并不会复制非java文件。此时需要用配置解决。配置资源文件的路径及匹配规则,并且将其附加到jar包中。

<build>    <resources>        <resource>            <directory>src/main/resources</directory>            <includes>                <include>**/*</include>            </includes>            <filtering>true</filtering>        </resource>        <resource>            <directory>src/main/java</directory>            <includes>                <include>**/*.xml</include>                <include>**/*.txt</include>            </includes>        </resource>    </resources>    <plugins>        <plugin>            <artifactId>maven-source-plugin</artifactId>            <executions>                <execution>                    <id>attach-sources</id>                    <goals>                        <goal>jar</goal>                    </goals>                </execution>            </executions>            <configuration>                <excludes>                    <exclude>**/*.xml</exclude>                    <exclude>**/*.properties</exclude>                </excludes>            </configuration>        </plugin>    </plugins></build>

mybatis逆向工程生成代码

1 什么是逆向工程
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)
由数据库的表生成java代码。
2 下载逆向工程中需要的jar
mybatis-generator-core-1.3.4.jar
当然还需要一些其他的jar,比如连接mysql的jar
mysql-connector-java-5.1.28-bin.jar
3 使用方法
以下介绍两种方法
3.1 通过java project运行逆向工程
使用java程序方式,不依赖开发工具。
步骤1:添加生成代码配置文件 generatorConfig.xml
内容如下:

<?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>    <context id="testTables" targetRuntime="MyBatis3" defaultModelType="flat">        <commentGenerator>            <!-- 是否去除自动生成的注释 true:是 : false:否 -->            <property name="suppressAllComments" value="true" />        </commentGenerator>        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.78:3306/fdd_yun" userId="admin" password="Admin_12345">        </jdbcConnection>        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"    connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg"  password="yycg">    </jdbcConnection> -->        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和    NUMERIC 类型解析为java.math.BigDecimal -->        <javaTypeResolver>            <property name="forceBigDecimals" value="false" />        </javaTypeResolver>        <!-- targetProject:生成PO类的位置 -->        <javaModelGenerator targetPackage="com.fdd.newhouse.po" targetProject=".\src">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />            <!-- 从数据库返回的值被清理前后的空格 -->            <property name="trimStrings" value="true" />        </javaModelGenerator>        <!-- targetProject:mapper映射文件生成的位置 -->        <sqlMapGenerator targetPackage="com.fdd.newhouse.mapper" targetProject=".\src">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />        </sqlMapGenerator>        <!-- targetPackage:mapper接口生成的位置 -->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.fdd.newhouse.mapper" targetProject=".\src">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!-- 指定数据库表 -->        <table tableName="yun_customer"></table>        <table tableName="yun_guide_record"></table>    </context></generatorConfiguration>

需要修改的为生成po,mapper的包路径,需要指定的数据库表

import java.io.File;import java.util.ArrayList;import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.config.xml.ConfigurationParser;import org.mybatis.generator.internal.DefaultShellCallback;public class Start {    public static void main(String[] args) throws Exception{        List<String> warnings = new ArrayList<String>();        boolean overwrite = true;        File configFile = new File("generatorConfig.xml");        ConfigurationParser cp = new ConfigurationParser(warnings);        Configuration config = cp.parseConfiguration(configFile);        DefaultShellCallback callback = new DefaultShellCallback(overwrite);        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);        myBatisGenerator.generate(null);    }}

步骤3:执行main方法,生成的代码如下
步骤4:拷贝
需要将生成工程中所生成的代码拷贝到自己的工程中。

3.2通过maven方式运行
步骤1:在pom中添加

<plugin>    <groupId>org.mybatis.generator</groupId>    <artifactId>mybatis-generator-maven-plugin</artifactId>    <version>1.3.4</version>    <dependencies>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.30</version>        </dependency>    </dependencies></plugin>

步骤二:添加生成代码配置文件 generatorConfig.xml放到src/main/resource下

<?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>    <!--     <classPathEntry location="classpath:mysql-connector-java-5.1.30.jar" /> -->    <context id="testTables" targetRuntime="MyBatis3" defaultModelType="flat">        <commentGenerator>            <!-- 是否去除自动生成的注释 true:是 : false:否 -->            <property name="suppressAllComments" value="true" />        </commentGenerator>        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.78:3306/fdd_yun" userId="admin" password="Admin_12345">        </jdbcConnection>        <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"    connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg"  password="yycg">    </jdbcConnection> -->        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和    NUMERIC 类型解析为java.math.BigDecimal -->        <javaTypeResolver>            <property name="forceBigDecimals" value="false" />        </javaTypeResolver>        <!-- targetProject:生成PO类的位置 -->        <javaModelGenerator targetPackage="com.fdd.newhouse.po" targetProject=".\src\main\java">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />            <!-- 从数据库返回的值被清理前后的空格 -->            <property name="trimStrings" value="true" />        </javaModelGenerator>        <!-- targetProject:mapper映射文件生成的位置 -->        <sqlMapGenerator targetPackage="com.fdd.newhouse.mapper" targetProject=".\src\main\java">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />        </sqlMapGenerator>        <!-- targetPackage:mapper接口生成的位置 -->        <javaClientGenerator type="XMLMAPPER" targetPackage="com.fdd.newhouse.mapper" targetProject=".\src\main\java">            <!-- enableSubPackages:是否让schema作为包的后缀 -->            <property name="enableSubPackages" value="false" />        </javaClientGenerator>        <!-- 指定数据库表 -->        <table tableName="yun_customer"></table>        <table tableName="yun_guide_record"></table>    </context></generatorConfiguration>

步骤三: 执行maven命令
mvn mybatis-generator:generate
覆盖之前写的文件,则如下:
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

注:此插件生成的代码里面,会同时生成一个Example结尾的类,类中会有全部字段的常用查询方式,以面向对象的方式写sql,在mybatis中的Example查询中会用到,此类的以对象形式编写sql确实很强大,但是,功能只局限于自动生成,这样的代码,完全不可能写出来,仅作参考,思想交流之用。

如果不想生成Example,可使用如下配置:

<table schema="minghan" tableName="sys_admin" domainObjectName="Admin"    enableCountByExample="false" enableUpdateByExample="false"    enableDeleteByExample="false" enableSelectByExample="false"    selectByExampleQueryId="false"/>

enableCountByExample,enableUpdateByExample,enableDeleteByExample,enableSelectByExample等配置为false后, 就不会生成生成Examle类了, 这样看起来代码干净很多, 如果需要用到where条件语句的时候 可以自已在对应的配置文件来配置sql mapper就可以了.

mybatis 插入数据后获取自增主键

MyBatis 3.2.6插入时候获取自增主键方法有二

以MySQL5.5为例:

方法1:

insert into person(name,pswd) values(#{name},#{pswd})

方法2:


select LAST_INSERT_ID()

insert into person(name,pswd) values(#{name},#{pswd})

插入前实体id属性为0;
插入后实体id属性为保存后自增的id;
注:主键需要从实体中获取,而不是返回值。
推荐使用方法1:它是使用java.sql.Statement.getGeneratedKeys()获取,也就是jdbc标准。

mybatis 多参数传值

如果参数是单个list,则key为list #{list} 引用,foreach中直接使用 list 引用。
如果是map,则通过 #{key} ,key即为map中的key
如果传递了多个参数,mybatis会自动封装成一个map,如果用序号,则是 #{0},#{1} 下标从0开始,但是在foreach中不能这样使用,需要使用 param1 param2 ,从1开始。
建议使用下面的方法:
1. 在接口参数中使用注解 @Param 声名参数名称,这样即可使用声名的名称来引用参数。
2. 自行封装参数为 map 对象,并传入,通过 key 来引用参数。

注:当传入一个参数时直接使用 _parameter 引用,多个值可以用 _parameter.get(0) 引用,_parameter 就是 mybatis自动生成的 map。

1 0
原创粉丝点击