Mybatis自动生成bean对象基本配置

版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/83002860
  1. List item

在build里面添加插件

   <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
  1. 在resources创建一个generatorConfig.xml文件,
  2. 配置数据库驱动,本地真实的jar路径 配置数据库驱动,本地真实的jar路径
    在这里插入图片描述
<?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="D:/m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.sxt.po" targetProject="D:/Spring/Mybaties/Myspring04/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.sxt.mapper" targetProject="D:/Spring/Mybaties/Myspring04/src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.sxt.dao" targetProject="D:/Spring/Mybaties/Myspring04/src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <table tableName="account" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="card" domainObjectName="Card" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

生成Model类存放位置
生成映射文件存放位置
生成Dao类存放位置
通过copy path更改
在IDEA中运行即可
在这里插入图片描述
将相应的bean对象,空构造以及toString方法,方便打印,就是一个地址
编写service

package com.sxt.service;

import com.sxt.dao.AccountMapper;
import com.sxt.po.Account;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AccountService {
    @Resource
    private AccountMapper accountMapper;

    public Account queryAccountById(Integer id){
        return accountMapper.selectByPrimaryKey(id);
    }
}

编写对应的测试类

package com.sxt.service;

import com.sxt.po.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"} )
public class AccountServiceTest {
    @Resource
    private AccountService accountService;
    @Test
    public void queryAccountById() throws Exception{
        Account account = accountService.queryAccountById(8);
        System.out.println("--------------------------------------");
        //打印的是一个地址.因为没写toString方法
        System.out.println(account);
        System.out.println("--------------------------------------");
    }
}

猜你喜欢

转载自blog.csdn.net/wwwzydcom/article/details/83002860