SpringBoot整合Mybatis 以及 mybatis自动生成代码配置

1、springboot的配置文件application.yml

#端口
server:
  port: 8080

  #模板页面
  #注释的部分是Thymeleaf默认的配置,如有其它需求可以自行更改
  spring:
    thymeleaf:
      cache: false
      prefix: classpath:/templates/
      suffix: .html
      mode: LEGACYHTML5
#      encoding: UTF-8
#      content-type: text/html

##数据源一
#spring:
#      datasource:
#          driverClass: com.mysql.jdbc.Driver
#          url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
#          username: xuan
#          password: 123456
#数据源二
spring:
      datasource:
          url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
          username: xuan
          password: 123456
          # 使用druid数据源
          type: com.alibaba.druid.pool.DruidDataSource
          driver-class-name: com.mysql.cj.jdbc.Driver
          filters: stat
          maxActive: 20
          initialSize: 1
          maxWait: 60000
          minIdle: 1
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 300000
          validationQuery: select 'x'
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
          poolPreparedStatements: true
          maxOpenPreparedStatements: 20

#spring-boot整合mybatis
mybatis:
  #config-location: classpath:/mapper/config/mybatisConfig.xml #可以注射掉,没用到该配置文件
  mapper-locations: classpath:/mapper/*Mapper.xml
  #type-aliases-package: com.xuan.entity


2、maven的配置文件pom.xml (加入springboot整合mybatis的jar包,以及mysql驱动jar包,阿里的数据库链接池jar包)

以及mybatis 自动生成代码的一些配置



    4.0.0

    com.xuan
    myspringboot
    0.0.1-SNAPSHOT
    jar

    myspringboot
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8

        1.3.2
        6.0.6


        
        
        
        /Users/chenqixuan/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar
        ${basedir}/src/main/java
        ${basedir}/src/main/resources
        ${basedir}/src/main/java
        ${basedir}/src/main/resources/generator/generatorConfig.xml
        true
    

    
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
        
            mysql
            mysql-connector-java
            ${mysql.connector.java.version}
        
        
        
            com.alibaba
            druid
            1.0.11
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            net.sourceforge.nekohtml
            nekohtml
            1.9.22
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                ${mybatis.generator.version}
            
        
    




3、mybatis的配置文件 (可以不要需要,因为在application.yml配置了)





    
        
        
    

    
        
        
    



4、用户实体类xml映射文件 UserMapper.xml





	
	
	

5、自动生成配置文件 generatorConfig.xml(项目目录下运行命令: mvn mybatis-generator:generate -e )

注意:generatorConfig.xml配置文件中有些参数笔者是配置在pom.xml里的,另外mysql-connector-java-5.1.38.jar  数据库mysql驱动也不要使用太高的版本(笔者使用6.0.6版本是遇到错误,换成低版本就没事)





    

    
        
        

        
            
        

        
            
            
        

        
            
        

        
            
        

        
        

6、用户实体类:

package com.xuan.entity;

import java.io.Serializable;

/**
 * Created by chenqixuan on 17/10/24.
 */
public class User implements Serializable{
    private static final long serialVersionUID = 2120869894112984147L;

    private Integer id;

    private String  name;

    private  Integer age;

    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

7、dao层是使用@Mapper注解接口代理,以前spring是使用自动扫描

package com.xuan.mapper;

import com.xuan.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by chenqixuan on 17/10/24.
 */
@Mapper
public interface UserMapper {

    public List getAllUser();

}

8、controller引用:

package com.xuan.controller;

import com.xuan.entity.User;
import com.xuan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by chenqixuan on 17/10/24.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping(value="/all")
    public List getAllUser(){

        return userMapper.getAllUser();
    }


}

9、效果图:


10、目录结构:








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