xml主配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
mybatis 配置文件 主要配置数据库登录相关信息
-->
<properties resource="db.properties"></properties>
<!-- 注意 settings 必须写在 紧跟 properties -->
<!-- <settings>
<!–
将数据库_的字段改为驼峰式写法 tb:student_name 映射为 studentName
–>
<!– <setting name="mapUnderscoreToCamelCase" value="true"/>–>
</settings>-->
<!--
在typeAliases 自己配置别名 注意 写的顺序
1.使用 <typeAlias type="com.baidu.entity.Student" alias="student"></typeAlias>
type 是类全限定名
alias 一般是类名小写,在使用时 resultType="STUDENT" 忽略大小写别名的引用
2. 使用 <package name="com.baidu.entity"/> 别名 为 对应应类名的消息
注意:一个类最好 生成别名 一次
-->
<typeAliases>
<!-- <typeAlias type="com.baidu.entity.Student" alias="studenta"></typeAlias>
<typeAlias type="com.baidu.entity.User" alias="user"></typeAlias>
-->
<package name="com.zzz.entity"/>
</typeAliases>
<!--配置mybatis环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<!--缓存池获取数据源-->
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<!--
对应的道层接口 必须在配置文件 <mappers>内声明,声明有三种方式
1. 指定mapper.xml 文件位置 <mapper resource="com/baidu/dao/IStudentDao.xml"></mapper>
2. 指定dao接口 <mapper class="com.baidu.dao.IStudentDao"></mapper>
3. 指定包名 <package name="com.baidu.dao"/> 常用
-->
<mappers>
<package name="com.zzz.mapper"/>
</mappers>
</configuration>
mapper文件头:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zzz.mapper.StudentMapper">
<select id="selectStudent" resultType="com.zzz.entity.Student">
select * from student where id=#{id};
</select>
</mapper>
pom文件:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.14</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
log4j.properties:
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=mybatis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
逆向工程配置文件:generator.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>
<classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
<context id="msqlTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf-8"
driverClass="com.mysql.jdbc.Driver" userId="root" password="123456">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类和数据库表一一对应 -->
<javaModelGenerator targetPackage="com.zzz.entity" targetProject="D:\restart\mybatis-demo\src\main\java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--映射xml文件及dao接口-->
<sqlMapGenerator targetPackage="com.zzz.dao" targetProject="D:\restart\mybatis-demo\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zzz.dao" targetProject="D:\restart\mybatis-demo\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--数据库表-->
<table tableName="admin" domainObjectName="Admin"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="book_info" domainObjectName="BookInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="lend_list" domainObjectName="LendList"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="notice" domainObjectName="Notice"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="reader_info" domainObjectName="ReaderInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="type_info" domainObjectName="TypeInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
逆向工程生成java
import org.mybatis.generator.api.MyBatisGenerator;
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;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GeneratorTest {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
String grt="D:\\restart\\mybatis-demo\\src\\main\\resources\\generatorConfig.xml";
List<String> warning = new ArrayList<String>();
boolean overRide = true;
File config = new File(grt);
ConfigurationParser configurationParser =new ConfigurationParser(warning);
Configuration configuration = configurationParser.parseConfiguration(config);
DefaultShellCallback callback = new DefaultShellCallback(overRide);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, callback, warning);
myBatisGenerator.generate(null);
}
}