(邮箱:[email protected] 欢迎批评指正)
使用mybatis呢,当然大牛级别的人物都是自己写sql语句,我这个级别嘛,就先自动生成用着吧,毕竟写sql语句自己还差的太远。
我就先说说这个mybatis自动生成所需要进行的配置吧。这里我使用的是maven项目。
- maven项目配置
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--配置文件的位置--> <configurationFile>../MybatisGenerator/src/main/resources/MybatisGeneratorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <!-- 数据库驱动 --> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <!--################## Mybatis ##################--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency><!-- mybatis分页包 --> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> </dependency> <dependency><!-- 分页插件包 --> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--################## mybatis**** ##################--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> </dependencies>这个jar包呢就不多做解释,最后那个主要就是****的核心包,build标签是****的关键配置,至于build标签的作用是什么,我也希望有个高手给个清晰的解释。网络解释很操蛋。下来就是build里面的数据库驱动部分,你使用的哪种数据库就用哪种数据库驱动。
-
****配置文件/MybatisGeneratorConfig.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> <properties resource="mybatisGeneratorSql.properties"/> <context id="context" targetRuntime="MyBatis3"> <!-- JavaBean 实现 序列化 接口 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!-- genenat entity时,生成toString --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- generate entity时,生成hashcode和equals方法--> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <!-- 这里引入扩展插件 --> <!--<plugin type="com.fxhx.gamelog.common.plugin.PaginationPlugin" />--> <!-- 自定义查询指定字段 --> <!--<plugin type="org.mybatis.generator.plugins.field.FieldsPlugin" />--> <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" /> <commentGenerator> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 生成pojo类的位置 --> <javaModelGenerator targetPackage="${sql.pojo}" targetProject=".\src\test\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成SQL map的XML文件生成器, 注意,在Mybatis3之后,我们可以使用mapper.xml文件+Mapper接口(或者不用mapper接口), 或者只使用Mapper接口+Annotation,所以,如果 javaClientGenerator配置中配置了需要生成XML的话, 这个元素就必须配置 targetPackage/targetProject:同javaModelGenerator --> <sqlMapGenerator targetPackage="${sql.mapper.java}" targetProject=".\src\test\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 对于mybatis来说,即生成Mapper接口,注意,如果没有配置该元素,那么默认不会生成Mapper接口 targetPackage/targetProject:同javaModelGenerator type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下): 1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML; 2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中; 3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML; 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER --> <javaClientGenerator type="XMLMAPPER" targetPackage="${sql.mapper.xml}" targetProject=".\src\test\java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 选择一个table来生成相关文件,可以有一个或多个table,必须要有table元素 选择的table会生成一下文件: 1,SQL map文件 2,生成一个主键类; 3,除了BLOB和主键的其他字段的类; 4,包含BLOB的类; 5,一个用户生成动态查询的条件类(selectByExample, deleteByExample),可选; 6,Mapper接口(可选) tableName(必要):要生成对象的表名; 注意:大小写敏感问题。正常情况下,MBG会自动的去识别数据库标识符的大小写敏感度, 在一般情况下,MBG会 根据设置的schema,catalog或tablename去查询数据表,按照下面的流程: 1,如果schema,catalog或tablename中有空格,那么设置的是什么格式,就精确的使用指定的大小写格式去查询; 2,否则,如果数据库的标识符使用大写的,那么MBG自动把表名变成大写再查找; 3,否则,如果数据库的标识符使用小写的,那么MBG自动把表名变成小写再查找; 4,否则,使用指定的大小写格式查询; 另外的,如果在创建表的时候,使用的""把数据库对象规定大小写,就算数据库标识符是使用的大写, 在这种情况下也会使用给定的大小写来创建表名; 这个时候,请设置delimitIdentifiers="true"即可保留大小写格式; 可选: 1,schema:数据库的schema; 2,catalog:数据库的catalog; 3,alias:为数据表设置的别名,如果设置了alias,那么生成的所有的SELECT SQL语句中,列名会变成:alias_actualColumnName 4,domainObjectName:生成的domain类的名字,如果不设置,直接使用表名作为domain类的名字;可以设置为somepck.domainName,那么会自动把domainName类再放到somepck包里面; 5,enableInsert(默认true):指定是否生成insert语句; 6,enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get); 7,enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句; 8,enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update); 9,enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete); 10,enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句; 11,enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询); 12,enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性); 13,modelType:参考context元素的defaultModelType,相当于覆盖; 14,delimitIdentifiers:参考tableName的解释,注意,默认的delimitIdentifiers是双引号,如果类似MYSQL这样的数据库,使用的是`(反引号,那么还需要设置context的beginningDelimiter和endingDelimiter属性) 15,delimitAllColumns:设置是否所有生成的SQL中的列名都使用标识符引起来。默认为false,delimitIdentifiers参考context的属性 注意,table里面很多参数都是对javaModelGenerator,context等元素的默认属性的一个复写; --> <table tableName="tb_brand" enableInsert="true" enableCountByExample="true" enableDeleteByPrimaryKey="true" enableDeleteByExample="true" enableSelectByPrimaryKey="true" enableSelectByExample="true" enableUpdateByPrimaryKey="true" enableUpdateByExample="true"/> </context> </generatorConfiguration>我对第一行的序列化进行一个解释,因为在我的项目里面进行了dubbo的使用,进行数据传输的时候,这个地方导致我找了好久的错误,最后才发现是对象没有进行序列化,后来就在****里面直接加入了序列化的配置。
-
上面配置文件的属性配置mybatisGeneratorSql.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/shopping?characterEncoding=utf-8 jdbc.username=root jdbc.password=root #这个地方写的是生成的各种文件的位置,直接写源码文件下的位置, sql.pojo=pojo sql.mapper.java=mapper sql.mapper.xml=mapper这里就是配置数据库的连接驱动,以及自动生成的pojo和mapper的存放
-
点击maven的mybatis-generator进行自动生成
-
然后就生成了,我们需要的mybatis的文件。接下来就是spring和mybatis的配置了