Spring快速整合MyBatis+”神器“MyBatis-Generator

什么是MyBatis-Generator?

官网介绍:MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器。它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代码。它将内省数据库表(或许多表),并将生成可用于访问表的工件。这减少了设置对象和配置文件以与数据库表交互的初始麻烦。MBG寻求对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍然需要为连接查询或存储过程手动编写SQL和对象代码。

所谓“神器”,指的就是MGB可以根据数据库表,反向生成JavaBean对象、Dao层接口文件以及SQL映射文件(包含针对于单表的基本操作,像CURD,计数,排序等),因为这是其实都是一些重复化的工作,所以MGB可以说帮我们节约了大量时间和精力。废话不多少,正式开始今天的学习吧!

  1. pom文件引入相关依赖

     <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
            <maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
            
            <spring-version>4.3.17.RELEASEspring-version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring-version}version>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>${spring-version}version>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>${spring-version}version>
            dependency>
    
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.1version>
            dependency>
    
            
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>2.0.1version>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.18.8version>
                <scope>providedscope>
            dependency>
    dependencies>
    
  2. 再在pom文件加入Mybatis-Generator的相关引擎和依赖

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <configuration>
                        <source>1.7source>
                        <target>1.7target>
                    configuration>
                    <version>3.3version>
                plugin>
                <plugin>
                    <groupId>org.mybatis.generatorgroupId>
                    <artifactId>mybatis-generator-maven-pluginartifactId>
                    <version>1.3.2version>
                    <dependencies>
                        <dependency>
                            <groupId>mysqlgroupId>
                            <artifactId>mysql-connector-javaartifactId>
                            <version>5.1.38version>
                        dependency>
                    dependencies>
                    <configuration>
                        
                        <configurationFile>${basedir}/src/main/resources/generator-						config.xmlconfigurationFile>
                        <overwrite>trueoverwrite>
                    configuration>
                plugin>
            plugins>
        pluginManagement>
    build>
    
  3. 配置generator-config.xml文件

    
    
    
    <generatorConfiguration>
        <context id="testTables" targetRuntime="MyBatis3">
            <commentGenerator>
                
                <property name="suppressAllComments" value="true" />
            commentGenerator>
            
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
               connectionURL="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"
               userId="root" password="a">
            jdbcConnection>
    
            
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            javaTypeResolver>
    
            
            <javaModelGenerator targetPackage="com.tys.pojo"
                                targetProject=".\src\main\java">
                
                <property name="enableSubPackages" value="false" />
                
                <property name="trimStrings" value="true" />
            javaModelGenerator>
            
            <sqlMapGenerator targetPackage="com.tys.dao.mapper"
                             targetProject=".\src\main\java">
                
                <property name="enableSubPackages" value="false" />
            sqlMapGenerator>
            
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.tys.dao"
                                 targetProject=".\src\main\java">
                
                <property name="enableSubPackages" value="false" />
            javaClientGenerator>
            
            <table tableName="student">table>
    
            
        context>
    generatorConfiguration>
    
  4. 使用maven命令(mybatis-generator:generate)运行Mybatis-Generator,如下图所示(我用的是idea)
    Spring快速整合MyBatis+”神器“MyBatis-Generator_第1张图片Spring快速整合MyBatis+”神器“MyBatis-Generator_第2张图片

  5. 可以看到,神器就帮我们自动生成了pojo实体类,mapper映射文件以及dao层接口
    Spring快速整合MyBatis+”神器“MyBatis-Generator_第3张图片

  6. 接下来我们就可以开始用Spring整合MyBatis了,配置beans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">

	
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="${jdbc.url}"/>
		<property name="driverClassName" value="${jdbc.driverClass}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	bean>	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource"/>
		
		
		
		
	   <property name="mapperLocations"
	   		value="classpath:com/tys/dao/mapper/*.xml"/>
	bean>

	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="com.tys.dao"/>
	bean>
	
	<context:component-scan base-package="com.tys.biz"/>

beans>
  1. 写个Spring测试类运行一下
@RunWith(SpringRunner.class)
@ContextConfiguration("/beans.xml")//指明配置文件的路径
public class Test {
    @Autowired
    private StudentBiz sbiz;

    @org.junit.Test
    public void test(){
        List<Student> sList= sbiz.queryAllStudentList();
        for (Student student : sList) {
            System.out.println(student);
        }
    }
}

咦!居然还报错!在这里插入图片描述
行吧!我们再来找下错误原因,它是说我们的SQL映射文件不存在!没道理啊,路径也没错啊。难道是编译后没有我们的映射xml文件?于是我们再看看编译后的target文件夹里有没有?
Spring快速整合MyBatis+”神器“MyBatis-Generator_第4张图片
还真没有!!!说明xml文件没有被maven打包到target下面,(奇怪,之前一直都是用eclipse,没有出现这种情况,现在换成idea就出现了,可能还跟编译器有挺大关系的)

解决办法:在pom.xml的build标签里加上相关配置,告诉maven—记得把我的SQL映射文件也打包。。


<resources>
    <resource>
        <directory>src/main/javadirectory>
        <includes>
            <include>**/*.xmlinclude>
        includes>
        <filtering>truefiltering>
    resource>
resources>

最后再来测试,看还有没有毛病?顺利打印,没毛病!Spring快速整合MyBatis+”神器“MyBatis-Generator_第5张图片
9. 最后贴上项目总结构图
Spring快速整合MyBatis+”神器“MyBatis-Generator_第6张图片

你可能感兴趣的:(MyBatis,Mybatis,Spring整合Mybatis)