springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)

项目源码以及sql:

链接:https://pan.baidu.com/s/1tbZcHZIK6GI236aDJIssiA 密码:4ybd

环境/版本一览:

开发工具:Intellij IDEA 2018.1.6

springboot: 2.0.3

jdk:1.8

maven:3.5

alibaba Druid 数据库连接池:1.1.9

PageHelper 分页插件

mybatis generator 自动生成代码插件


新建一个springboot demo项目。

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第1张图片

附上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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.3.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.2version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>1.1.9version>
            dependency>
            <dependency>
                <groupId>org.apache.commonsgroupId>
                <artifactId>commons-lang3artifactId>
                <version>3.4version>
            dependency>
        
            
            
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <dependencies>
                    <dependency>
                        <groupId> mysqlgroupId>
                        <artifactId> mysql-connector-javaartifactId>
                        <version> 5.1.39version>
                    dependency>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.5version>
                    dependency>
                dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <phase>packagephase>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    
                    <verbose>trueverbose>
                    
                    <overwrite>trueoverwrite>
                    
                    <configurationFile>
                        src/main/resources/mybatis-generator.xmlconfigurationFile>
                configuration>
            plugin>
        plugins>
    build>


project>

新建package:

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第2张图片


默认的配置文件为application.pro..,删除后新建

application.yml:

  spring:
      datasource:
          name: mysql_test
          type: com.alibaba.druid.pool.DruidDataSource
          #druid相关配置
          druid:
            #监控统计拦截的filters
            filters: stat
            driver-class-name: com.mysql.jdbc.Driver
            #基本属性
            url: jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
            username: root
            password: root
            #配置初始化大小/最小/最大
            initial-size: 1
            min-idle: 1
            max-active: 20
            #获取连接等待超时时间
            max-wait: 60000
            #间隔多久进行一次检测,检测需要关闭的空闲连接
            time-between-eviction-runs-millis: 60000
            #一个连接在池中最小生存的时间
            min-evictable-idle-time-millis: 300000
            validation-query: SELECT 'x'
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false
            #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
            pool-prepared-statements: false
            max-pool-prepared-statement-per-connection-size: 20

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.model

#pagehelper
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
    returnPageInfo: check


新建mysql数据库表结构:

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第3张图片



逆向工程generater

需要的依赖已在上图pom.xml中贴出:

在resoures下新建mybatis-generater.xml,名字可以自定义,根据自己数据库以及项目结构修改,:





<generatorConfiguration>
    

    <context id="DB2Tables" targetRuntime="MyBatis3">
        
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        commentGenerator>

        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mytest"
                        userId="root"
                        password="root">
        jdbcConnection>
        
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator  targetPackage="com.example.demo.dao"  targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>

        
        <table tableName="kd_user" domainObjectName="User"
               enableCountByExample="false" enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByExample="false">
        table>

    context>
generatorConfiguration>

点击ieda上方点击run—-》EditConfigurations

若没有如图的maven,点击+号。

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第4张图片

配置如上图,OK.

右击项目工程名—》maven-Reimport–》运行
springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第5张图片

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第6张图片

运行即可。

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第7张图片


生成的Mapper.java

我去掉了几个不用的方法
加上@Mapper注解用于service能够扫描。若不加的话需要在启动类配置

@MapperScan(“com.example.demo.dao”)

package com.example.demo.dao;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    int deleteByPrimaryKey(String id);

    int insert(User record);

    User selectByPrimaryKey(String id);

    int updateByPrimaryKey(User record);
}

service

package com.example.demo.service;

import com.example.demo.model.User;

public interface UserService {

    int deleteByPrimaryKey(String id);

    int insert(User record);

    User selectByPrimaryKey(String id);

    int updateByPrimaryKey(User record);
}

serviceImpl

package com.example.demo.service.impl;

import com.example.demo.dao.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//@Service("userService")
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }


    @Override
    public User selectByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }
}

新建controller

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @Autowired
    private UserService userService;
       @RequestMapping(value = "/")
      public User index(){
           User user =  userService.selectByPrimaryKey("1");
           System.out.print(user);
           return user;
      }

}

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第8张图片

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

若mapper.java文件不加@Mapper注解。

@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

项目结构:

springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第9张图片

启动项目,启动成功后,访问 localhost:8080
springboot2.0集成mybatis1.3.2以及配置generater代码生成(附源码)_第10张图片

项目源码以及sql文件:

链接:https://pan.baidu.com/s/1tbZcHZIK6GI236aDJIssiA 密码:4ybd

你可能感兴趣的:(mybatis,SpringBoot)