Springboot+gradle+Mybatis-Generator

1.数据库准备

在mysql中创建一个t_user的数据库,如图1:


图1.png

2.开发软件:ieda

3.创建Gradle+Springboot+Mybatis项目

file——>new——>Project:

图2.png
图3.png
图4.png
图5.png
图6.png
图7

到这一步,我们就创建好项目了,但是还不能运行。之后的工作完成后整个项目的结构是:


图8.png

4步骤

1.build.gradle配置

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.jarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    //数据库
    compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '8.5.23'
    //日志
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    //fastjson
    compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47'


}

//mybatis generator plugin ------ start
buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.arenagod.gradle:mybatis-generator-plugin:1.4"
    }
}

apply plugin: "com.arenagod.gradle.MybatisGenerator"

configurations {
    mybatisGenerator
}

mybatisGenerator {
    verbose = true
    configFile = 'src/main/resources/tools/generatorConfig.xml'
}
//mybatis generator plugin ------ end

2.generatorConfig.xml配置(在resource的tools文件夹下),

(1)在resource中创建一个mybatis文件(用于生成mapper.xml);
(2)将配置的文件路径和数据库地址改成自己项目的。






    
    

        
        

        
        
            
            
            
            
        

        
        
        

        
        
            
            
        


        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
        


        
        
            
            
        

        
        

3.点击开关(双击),自动生成文件代码

图9.png

生成mybatis代码:


图10.png

4.添加Controller和Service实现增删改查

/**
 * 用户中心
 */
@RestController
public class UserController extends BaseController {


    @Autowired
    private UserServiceImpl userService;


   @RequestMapping("/getUserById")
    @ResponseBody
    public DefaultResultInfo getUserById(@RequestParam(name = "id", defaultValue = "1") long id) {
        User user = userService.getUserInfo(id);
        if (user != null) {
            logger.info(JSONArray.toJSON(user));
        }
        return new DefaultResultInfo<>(user);
    }
}
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;


    @Override
    public void registerUser(User user) {
        userMapper.insert(user);
    }


    @Override
    public void login(User user) {


    }

    @Override
    public void logout(User user) {

    }

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

    @Override
    public void updateteUserInfo(User user) {

    }

    @Override
    public void updateUserImage() {

    }


}

5.修改Application

@SpringBootApplication
@MapperScan(basePackages = "com.jarry.time")
public class TimeApplication {




    //DataSource配置
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new DataSource();
    }

    //提供SqlSeesion
    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }



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

6.配置application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yueji?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

7.点击运行

图11.png

8.访问地址:http://localhost:8080/getUserById?id=1

图12.png

你可能感兴趣的:(Springboot+gradle+Mybatis-Generator)