Mybatis笔记
mybatis-generator生成mapper,model和dao
三种方式
- cmd命令生成
- eclipse插件
- maven方式:参考 http://www.cnblogs.com/JsonShare/p/5521901.html
1.命令方式
- 下载jar包:mybatis-generator-core-1.3.6.jar和mysql-connector-java-5.1.38.jar
- 编写generatorConfig.xml配置文件
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- jar包文件路径 -->
<classPathEntry location="mysql-connector-java-5.1.38.jar" />
<context id="context" targetRuntime="MyBatis3">
<!-- 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制 -->
<commentGenerator>
<!-- 是否取消注释 -->
<property name="suppressDate" value="true" />
<!-- 生成model注释 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 链接配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:Mysql://localhost:3306/snsbang?characterEncoding=utf8"
userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="cn.bdqn.mybatis"
targetProject="src">
<!-- 是否在当前路径下新加一层schema,eg:fase路径cn.bdqn.mybatis, true:cn.bdqn.mybatis.[schemaName] -->
<property name="enableSubPackages" value="true" />
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="false" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="cn.bdqn.mybatis.dao"
targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.bdqn.mybatis.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="snsbang" tableName="chatroom" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
<property name="useActualColumnNames" value="false" />
<columnRenamingRule searchString="^AA_"
replaceString="" />
</table>
<table tableName="member" domainObjectName="Member" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="notice" domainObjectName="Notice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
- 当前文件下执行命令:
java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
2.eclipse安装插件
3.maven方式 命令: mybatis-generator:generate
简述:
- pom添加相关依赖
- 添加generatorConfig.xml,如果需要model类生成mysql中的中文注释,需要自定义CommentGenerator,然后配置到commentGenerator标签,将MyCommentGenerator类打包生成jar,添加到maven库中,如:
<commentGenerator type="cn.procode.mybatis.MyCommentGenerator">
<!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
<!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
</commentGenerator>
拓展CommentGenerator
package cn.procode.mybatis;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyCommentGenerator extends org.mybatis.generator.internal.DefaultCommentGenerator {
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().equals("")) {
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
// addJavadocTag(field, false);
field.addJavaDocLine(" */");
}
}
/**
* 1.执行main方法生成代码,不需要打jar包添加到maven中
* 2.run as maven build 需要打jar包添加到maven中
* @param args
*/
public static void main(String[] args) {
try {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("G:/javaEE/j2ee/WorkSpace/testm/test/src/main/resources/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);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
}
}
pom
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dq</groupId>
<artifactId>test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<version>3.3</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.saddestmoon</groupId>
<artifactId>MyCommentGenerator</artifactId>
<version>0.1.SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/mygenerator.jar</systemPath>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径 -->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
hashCode知识点
hashCode()方法的作用:hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,这样的散列集合包括HashSet、HashMap以及HashTable。
当集合要添加新的对象时,先调用这个对象的hashCode方法,得到对应的hashcode值,实际上在HashMap的具体实现中会用一个table保存已经存进去的对象的hashcode值,如果table中没有该hashcode值,它就可以直接存进去,不用再进行任何比较了;如果存在该hashcode值, 就调用它的equals方法与新元素进行比较,相同的话就不存了,不相同就散列其它的地址
1.提高查找效率
2.重写equals方法必须重写hashCode()
对于两个对象,如果调用equals方法得到的结果为true,则两个对象的hashcode值必定相等;
如果equals方法得到的结果为false,则两个对象的hashcode值不一定不同;
如果两个对象的hashcode值不等,则equals方法得到的结果必定为false;
如果两个对象的hashcode值相等,则equals方法得到的结果未知。