• springboot + mybatis +druid


    Druid Spring Boot Starter

    mybatis-spring-boot-autoconfigure

    mybatis-spring-boot-samples

    新建spring boot工程,添加pom依赖

    <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.6</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    View Code

    新增User类

    public class User {
        private Integer id;
        private String name;
        private Integer sex;
        private Integer age;
    
        public User(String name, Integer sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public User(Integer id, String name, Integer sex, Integer age) {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
    
        public User() {
        }
    
        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 getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    View Code

    新增UserMapper接口

    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    @Mapper
    public interface UserMapper {
        @Select("select * from person where id=#{id}")
        User findById(@Param("id") Integer id);
    }
    View Code

    新增UserController

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
        @Autowired
        UserMapper userMapper;
    
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String index() {
            User user=userMapper.findById(1);
            return user.getName();
        }
    }
    View Code

    配置文件

    spring.datasource.url= jdbc:mysql://192.168.31.146:3306/mydb
    spring.datasource.username= root
    spring.datasource.password= pass
    
    spring.datasource.druid.url= jdbc:mysql://192.168.31.146:3306/mydb
    spring.datasource.druid.username= root
    spring.datasource.druid.password= pass
    
    spring.datasource.druid.initial-size=5
    spring.datasource.druid.max-active=20
    spring.datasource.druid.min-idle=5
    spring.datasource.druid.max-wait=60000
    spring.datasource.druid.pool-prepared-statements=true
    spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
    #spring.datasource.druid.max-open-prepared-statements=
    spring.datasource.druid.validation-query=select 1 from dual
    #spring.datasource.druid.validation-query-timeout=
    spring.datasource.druid.test-on-borrow=false
    spring.datasource.druid.test-on-return=false
    spring.datasource.druid.test-while-idle=true
    spring.datasource.druid.time-between-eviction-runs-millis=60000
    spring.datasource.druid.min-evictable-idle-time-millis=300000
    #spring.datasource.druid.max-evictable-idle-time-millis=
    #配置多个英文逗号分隔
    spring.datasource.druid.filters=stat,wall,log4j
    View Code

    启动应用后打开浏览器:http://localhost:8080/druid/index.html

    再打开:http://localhost:8080/hello

    在druid界面查看sql

  • 相关阅读:
    职场中如何沟通 15条技巧现在学
    白领丽人:这六行盛产“钻石王老五”
    个人创业融资中的八大法律问题
    [转帖]用心领导先于理性管理
    职场中牢固人际关系的三十六计
    创业:如何制定最佳融资决策
    工作中如何演绎好你的职场情绪
    怎么成为一个成功的人
    创业不得不看:华商富豪们的成功哲学
    一流简历的10大关注项
  • 原文地址:https://www.cnblogs.com/uptothesky/p/8213537.html
Copyright © 2020-2023  润新知