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>
新增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; } }
新增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); }
新增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(); } }
配置文件
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
启动应用后打开浏览器:http://localhost:8080/druid/index.html
再打开:http://localhost:8080/hello
在druid界面查看sql