mybatis setting
1. mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 6 <configuration> 7 <settings> 8 <!-- changes from the defaults for testing --> 9 <setting name="cacheEnabled" value="false" /> 10 <setting name="useGeneratedKeys" value="true" /> 11 <setting name="defaultExecutorType" value="REUSE" /> 12 </settings> 13 <typeAliases> 14 <typeAlias alias="Student" type="com.zero.model.Student"/> 15 <typeAlias alias="School" type="com.zero.model.School"/> 16 <typeAlias alias="Course" type="com.zero.model.Course"/> 17 <typeAlias alias="Student_courseKey" type="com.zero.model.Student_courseKey"/> 18 </typeAliases> 19 <environments default="development"> 20 <environment id="development"> 21 <transactionManager type="jdbc"/> 22 <dataSource type="POOLED"> 23 <property name="driver" value="com.mysql.jdbc.Driver"/> 24 <property name="url" value="jdbc:mysql://localhost:3306/work"/> 25 <property name="username" value="root"/> 26 <property name="password" value="root"/> 27 </dataSource> 28 </environment> 29 </environments> 30 <mappers> 31 <mapper resource="com/zero/mapper/StudentMapper.xml" /> 32 <mapper resource="com/zero/mapper/CourseMapper.xml" /> 33 <mapper resource="com/zero/mapper/SchoolMapper.xml" /> 34 <mapper resource="com/zero/mapper/Student_courseMapper.xml" /> 35 </mappers> 36 </configuration>
2. mybatis generator -> generatorConfig.xml (fixed name)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <!--导入属性配置--> 8 <properties resource="props/mariadb.properties"></properties> 9 10 <classPathEntry location="c:\mysql-connector-java-5.1.40.jar"/> 11 12 <context id="default" targetRuntime="MyBatis3"> 13 14 <!-- optional,旨在创建class时,对注释进行控制 --> 15 <commentGenerator> 16 <property name="suppressDate" value="true"/> 17 <property name="suppressAllComments" value="true"/> 18 </commentGenerator> 19 20 <!--jdbc的数据库连接 --> 21 <jdbcConnection 22 driverClass="${jdbc.driverClass}" 23 connectionURL="${jdbc.url}" 24 userId="${jdbc.username}" 25 password="${jdbc.password}"> 26 </jdbcConnection> 27 28 29 <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> 30 <javaTypeResolver> 31 <property name="forceBigDecimals" value="false"/> 32 </javaTypeResolver> 33 34 35 <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 36 targetPackage 指定生成的model生成所在的包名 37 targetProject 指定在该项目下所在的路径 38 --> 39 <javaModelGenerator targetPackage="com.zero.model" 40 targetProject="src/main/java"> 41 42 <!-- 是否允许子包,即targetPackage.schemaName.tableName --> 43 <property name="enableSubPackages" value="false"/> 44 <!-- 是否对model添加 构造函数 --> 45 <property name="constructorBased" value="true"/> 46 <!-- 是否对类CHAR类型的列的数据进行trim操作 --> 47 <property name="trimStrings" value="true"/> 48 <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> 49 <property name="immutable" value="false"/> 50 </javaModelGenerator> 51 52 <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> 53 <sqlMapGenerator targetPackage="com.zero.mapper" 54 targetProject="src/main/java"> 55 <property name="enableSubPackages" value="false"/> 56 </sqlMapGenerator> 57 58 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 59 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 60 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 61 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 62 --> 63 <javaClientGenerator targetPackage="com.zero.dao" 64 targetProject="src/main/java" type="XMLMAPPER"> 65 <property name="enableSubPackages" value="true"/> 66 </javaClientGenerator> 67 68 69 <table tableName="course" domainObjectName="Course" 70 enableCountByExample="false" enableUpdateByExample="false" 71 enableDeleteByExample="false" enableSelectByExample="false" 72 selectByExampleQueryId="false"> 73 </table> 74 75 <table tableName="school" domainObjectName="School" 76 enableCountByExample="false" enableUpdateByExample="false" 77 enableDeleteByExample="false" enableSelectByExample="false" 78 selectByExampleQueryId="false"> 79 </table> 80 <table tableName="student" domainObjectName="Student" 81 enableCountByExample="false" enableUpdateByExample="false" 82 enableDeleteByExample="false" enableSelectByExample="false" 83 selectByExampleQueryId="false"> 84 </table> 85 <table tableName="student_course" domainObjectName="Student_course" 86 enableCountByExample="false" enableUpdateByExample="false" 87 enableDeleteByExample="false" enableSelectByExample="false" 88 selectByExampleQueryId="false"> 89 </table> 90 </context> 91 </generatorConfiguration>
3. database setting
jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://127.0.0.1:3306/work jdbc.username = root jdbc.password = root
4. maven plugin add in pom.xml <build> tag
1 <plugins> 2 <plugin> 3 <groupId>org.mybatis.generator</groupId> 4 <artifactId>mybatis-generator-maven-plugin</artifactId> 5 <version>1.3.2</version> 6 <configuration> 7 <verbose>true</verbose> 8 <overwrite>true</overwrite> 9 </configuration> 10 </plugin> 11 </plugins>
5. auto generate model/mapper/dao
mybatis-generator:generate -e
6. Spring with mybatis @Repository or @Component must be added on Mapper classes
7. By default, **mapper.xml must be put into resources directory. Otherwise, add follow code into pom.xml <build> tag can solve it
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>