一、MyBatis Generator
二、通过maven创建一个普通工程
三、在pom.xml中的代码
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Wzh</groupId>
<artifactId>Mybatis_Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Mybatis_Test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- juit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- mysql-connertor -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- mybatis generator -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<!-- 配置configureFile的路径和名称 -->
<configurationFile>
${basedir}/src/main/java/generatorConfig.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
四、创建generatorConfig.xml
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>
<!-- 引入配置文件 -->
<!-- <properties resource="init.properties" /> -->
<!-- 指定数据连接驱动jar地址 -->
<classPathEntry location="E:/Jar page/mysql-connector-java-5.1.26-bin.jar" />
<!-- 一个数据库一个context -->
<context id="mybatis_test">
<!-- 注释 -->
<commentGenerator>
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis_test" userId="root"
password="" />
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.mybatis_test.dto"
targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.mybatis_test.dto, true:com.mybatis_test.dto.[schemaName] -->
<property name="enableSubPackages" value="false" />
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="com.mybatis_test.mapper_xml"
targetProject="src/main/java">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.mybatis_test.dto, true:com.mybatis_test.dto.[schemaName] -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="com.mybatis_test.dao"
targetProject="src/main/java" type="XMLMAPPER">
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.mybatis_test.dto, true:com.mybatis_test.dto.[schemaName] -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<table schema="mybatis" tableName="student" domainObjectName="Student"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
</table>
</context>
</generatorConfiguration>
注意:targetPackage的参数开头必须小写,domainObjectName的参数开头必须大写
五、基本表Student
六、右键run->maven build
Goals:mybatis-generator:generate
七、mybatis.xml(mybits配置文件)
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis_test" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<package name="com/mybatis_test/mapper_xml/StudentMapper.xml" />
</mappers>
</configuration>
八、写一个工具包,用于获取SqlSession
package com.mybatis_test.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SessionFactoryUtil {
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sqlSessionFactory = null;
private static SqlSession sqlSession = null;
static {
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
sqlSession = threadLocal.get();
if (sqlSession == null) {
sqlSession=sqlSessionFactory.openSession();
threadLocal.set(sqlSession);
}
return sqlSession;
}
public void closeSqlSession() {
sqlSession = threadLocal.get();
if (sqlSession != null) {
sqlSession.close();
threadLocal.remove();
}
}
}
十、将StudentMapper.xml内容修改
<?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.mybatis_test.dao.StudentMapper">
<resultMap id="BaseResultMap" type="com.mybatis_test.dto.Student">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<select id="selectByPrimaryKey" resultType="com.mybatis_test.dto.Student">
select * from student where id = #{id}
</select>
</mapper>
package com.mybatis_test.dao;
import com.mybatis_test.dto.Student;
public interface StudentMapper {
Student selectByPrimaryKey(String id);
}
十二、测试类Test
package mybatis_test.main;
import org.apache.ibatis.session.SqlSession;
import com.mybatis_test.dao.StudentMapper;
import com.mybatis_test.dto.Student;
import com.mybatis_test.util.SessionFactoryUtil;
public class Test1 {
public static void main(String[] args) {
SqlSession session = SessionFactoryUtil.getSqlSession();
StudentMapper dao = session.getMapper(StudentMapper.class);
Student stu1 = dao.selectByPrimaryKey("1");
System.out.println(stu1);
}
}
运行结果:
- 参考出处
- http://blog.csdn.net/pk490525/article/details/16819307
- http://blog.csdn.net/u011410529/article/details/50484274