SpringBoot项目MyBatis-Generater安装(连接PostgreSql)

Step1

在pom.xml中添加依赖

<!-- 加载postgresql驱动 -->
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<version>42.2.14</version>
		</dependency>

		<!-- 加载jdbc连接数据库 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!-- 加载mybatis jar包 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

Step2

application.yml文件中添加配置


spring:
  datasource:
    #type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://xxx.xxx.xxx.xxx:5432/postgres
    username: postgres
    password: 123456

mybatis:
  #  全局配置的文件位置
  #  config-location: classpath:mybatis-config.xml
  #  映射路径
  mapper-locations: classpath:mappers/*.xml
  #  可以不写配置文件路径 写下面这个 但是上面的全局配置文件和下面这个不能同时存在 否则SpringBoot 不知道解析哪一个
  configuration: #指定mybatis全局配置文件中的配置项
    map-underscore-to-camel-case: true

Step3

到这一步,MyBatis配置完成,可以试一下是否能成功和数据库连接:

  1. 创建Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    
    @GetMapping("/test")
    public Object test(){
        userService.test();
        return 123;
    }
}
  1. 写Service接口(由于懒所以没有把接口和实现类分开)
@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    public void test(){
        List<User> allUsers = mapper.getAllUsers();
        System.out.println("aaaaaaabbbbbbb" + allUsers);
    }
}
  1. 写Mapper,从数据库拉数据
@Mapper
public interface UserMapper {
    @Select("select * from user_table")
    List<User> getAllUsers();
}
  1. 在数据库中建立对应的table,并且存入数据
    `
  2. 访问http://ip/test, 能够接收到数据,则连接成功

Step4

从这一步开始安装MyBatis-Generator
在pom.xml中导入插件依赖

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
    <!-- 配置数据库链接及mybatis generator core依赖 生成mapper时使用 -->
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.14</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
</plugin>

Step5

在resource文件夹下创建.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>
    <context id="DB2Tables" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql://xxx.xxx.xxx.xxx:5432/postgres"
                        userId="postgres"
                        password="123456">
        </jdbcConnection>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用BigDecimals,false可自动转化以下类型(Long Integer Short等) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.xxx.springbootdemo.po" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.springbootdemo.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->

        <table tableName="user_table" domainObjectName="UserPO"></table>
<!--        <table tableName="sys_center_control" domainObjectName="SysCenterControl" enableCountByExample="false"-->
<!--               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--               selectByExampleQueryId="false"></table>-->
<!--        <table tableName="sys_date_schedule" domainObjectName="SysDateSchedule" enableCountByExample="false"-->
<!--               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--               selectByExampleQueryId="false"></table>-->
<!--        <table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false"-->
<!--               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
<!--               selectByExampleQueryId="false"></table>-->

    </context>
</generatorConfiguration>

在启动类注释mapper 接口类扫描包配置:

// mapper 接口类扫描包配置
@MapperScan(basePackages = "com.lmy.springbootdemo.mapper")
@SpringBootApplication
public class SpringBootDemoApplication {

Step6

在数据库pgSQL设置好表结构,并在generatorConfig.xml文件中配置要生成的表
(这一步千万不要忘了!!)
在这里插入图片描述

然后运行插件,即可生成对应的mapper,无需重复写SQL语句
`
生成UserPO:

生成UserPOMapper:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值