Spring boot整合druid和mybatis全注解方式

怎么创建spring boot项目就不说了,下面是pom文件



    4.0.0

    com.mybatis
    mybatistest
    0.0.1-SNAPSHOT
    jar

    mybatistest
    Demo project for Spring Boot

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

    
        UTF-8
        UTF-8
        1.8
    

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

        
            mysql
            mysql-connector-java
            runtime
        

        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        




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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


项目结构

Spring boot整合druid和mybatis全注解方式_第1张图片

 controller代码

@Controller
public class UserController {

    @Autowired
    UserService userService;



    @RequestMapping("/find")
    @ResponseBody
    public List find(){

        return  userService.find();
    }


}

service代码

 

public interface UserService {

    public List find();
}



@Service
public class UserServiceImpl implements  UserService{


    @Autowired
    private UserDao userDao;//此位置会报错,不用管,不影响使用

    @Override
    public List find() {
        return userDao.find();
    }
}

dao层代码

 


@Mapper
public interface UserDao {

    @Select("Select * from user")
    public List find();



}

pojo代码

public class user {

    private Integer id;
    private String name;
    private String address;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

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

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

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

数据库表结构

Spring boot整合druid和mybatis全注解方式_第2张图片

 

 

application.properties文件

server.port=8080
#指定数据源名(可省略)
##spring.datasource.name=mysql_test
#数据库连接池名称
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#监控统计拦截的filters
spring.datasource.druid.filters=stat
#基本配置
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=aaaaaa
#初始化大小
spring.datasource.druid.initial-size=1
#最小
spring.datasource.druid.min-idle=1
#最大
spring.datasource.druid.max-active=20
#获取连接等待的超时时间
spring.datasource.druid.max-wait=600000
#间隔多久进行一次检测,检测需要关闭的链接
spring.datasource.druid.time-between-eviction-runs-millis=60000
#一个链接在池中最小生存时间
spring.datasource.druid.min-evictable-idle-time-millis=3000000

#在这里遇到最大的坑是spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver,刚开始配置错了,搞了半天才配置对

也可把application.properties文件换成yml文件

server:
  port: 8080


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: aaaaaa
          #配置初始化大小/最小/最大
          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.mybatis.mybatistest.pojo.user

 

你可能感兴趣的:(spring,boot,spring,boot整合druid,spring,boot整合mybatis,JAVA技术)