1,声明
本博客所有内容来源于网络、书籍、和各类手册。
内容均为非盈利,旨为学习记录,方便查询、总结备份、开源分享。
部分转载内容均有注明出处,如有侵权请告知,马上删除
参考链接:
- Mybatis(六) Spring整合mybatis
- MyBatis Generator
2, 工具
Idea 2019.2
maven 3.61
mysql 8.017
mybatis
java 1.8
3 ,依赖jar包 Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<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>com.hardes.mbg</groupId>
<artifactId>MybatisGenerator_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MybatisGenerator_maven</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--he property may be accessed in the configuration file with the syntax ${dao.target.dir}-->
<!--dao.target.dir>src/main/java</dao.target.dir-->
<!-- spring版本号 -->
<spring.version>5.1.9.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.6</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mybatis jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
</dependency>
<!--spring依赖-->
<!--1)核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--2)spring DAO-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--3)spring web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--4)spring test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<!--逆向工程mybatis插件 -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<resources>
<!--用于识别mapper包下面的映射文件xxxMapper.xml-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--用于无法发现引入的配置文件generator.properties,
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
-->
</resources>
</build>
</project>
4,代码结构
5, Mybatis Generator 配置文件
generator.properties 配置文件
db.type=mysql
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
db.username=root
db.password=root
#MBGInfo
generator.location=D:/CM_Library/java/maven/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar
generator.packagePojo=com.hardes.ssm.pojo
generator.packageMappers=com.hardes.ssm.mapper
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="generator.properties"/>
<!-- 指定数据库连接驱动jar地址 -->
<classPathEntry location="${generator.location}" />
<!--一个数据库一个contex上下文-->
<context id="generator" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否删除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
<!-- 是否生成注释代时间戳 -->
<property name="suppressDate" value="false" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="${db.driver}"
connectionURL="${db.url}"
userId="${db.username}"
password="${db.password}">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成pojos类的位置 -->
<javaModelGenerator targetPackage="${generator.packagePojo}"
targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 清理从数据库返回的值前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="${generator.packageMappers}"
targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="${generator.packageMappers}"
targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表,要生成哪些表,就写哪些表,要和数据库中对应,不能写错! -->
<table tableName="user"></table>
<table tableName="person"></table>
<table tableName="generator"></table>
<table tableName="mybatis3"></table>
</context>
</generatorConfiguration>
对应的数据库表
6, Running MyBatis Generator With Maven
配置idea,使用maven 运行Mybatis generator,
在idea上配置执行命令:mybatis-generator:generate
运行mybatisGenerator
生成如图mapper和pojo类
7, 配置测试文件
applicationContext.xml 配置文件
与Mybatis generator 共用相同的JDBC的配置文件 generator.properties
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 引用java配置文件 -->
<context:property-placeholder location="classpath:generator.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全局配置文件的路径 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 批量配置,这里是批量配置mapper代理,那么下面就不用配置id了。
我们想要获取哪个mapper代理用这个格式:类名首字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hardes.ssm.mapper"></property>
<!-- 默认不需要配置,但是如果有多个数据源的配置,那么就需要配置此项 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
SqlMapConfig.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>
<!-- 取别名,或者别的其他全局配置信息,就在这里编写 -->
<typeAliases>
<!-- 别名为类名首字母大小写都可以 -->
<package name="com.hardes.ssm.pojo"/>
</typeAliases>
<!-- 原先这里还有连接数据库的一些配置,与spring整合后,都交由spring来管理 -->
<!-- 加载mapper映射文件,使用通用的配置 -->
<mappers>
<package name="com.hardes.ssm.mapper"/>
</mappers>
</configuration>
编写测试类
Person_test
package com.hardes.ssm.test;
import com.hardes.ssm.mapper.PersonMapper;
import com.hardes.ssm.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Person_test {
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonMapper pm= (PersonMapper) ac.getBean("personMapper");
Person person =pm.selectByPrimaryKey(112);
System.out.println(person.getUsername());
}
}
8, 测试结果
9, 遇到问题
1 resource generator.properties does not exist
在此之前可以生成mapper和pojo, 后来再次运行,发现generatorConfigure.xml中的
引入配置文件没有工作。
参考链接
Running MyBatis Generator With Maven In IDEA Failed, resource jdbc.properties does not exist #188
I add the properties file to POM as resource, and it works
<resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> </includes> </resource> </resources>
解决办法如下:
在pom.xml中添加如下配置
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
添加此配置就可以,生成pojo和mapper相关类和映射文件
2,运行测试文件 再次出现 error java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
解决办法,就是删掉刚刚添加的配置
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
备注
由于接口类和映射文件都是放在com.hardes.ssm.mapper下,对于idea无法自动识别到xxxMapper.xml
所以在pom.xml 中添加一下配置
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>