SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式。
SpringBoot整合Mybatis也有两种方式,分别为XML配置方式和注解方式,主要优势点如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot的Mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- alibaba的druid的数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
spring:
datasource:
url: jdbc:mysql://localhost:3306/products?characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 30000
filters: stat
async-init: true
server:
port: 8080
## 该配置节点为独立的节点,有很多同学容易将这个配置放在spring的节点下,导致配置无法被识别
mybatis:
mapper-locations: classpath:mappers/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.mybaits.pojo # 注意:对应实体类的路径
##pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
逆向工程生成的pojo以及mapper,复制到本工程下,将com.mybaits.mapper下的ProducsMapper.xml放到resources下
public interface IProductService {
//添加产品
void addProducts(Product product);
//查找所有分页查询
List<Product> find(int page,int rows);
}
@Service
public class ProductServiceImpl implements IProductService {
@Autowired
private ProductMapper products;
@Override
public void addProducts(Product product) {
products.insert(product);
}
@Override
public List<Product> find(int page, int rows) {
//此时example只是一个空对象,没有带条件查询,所以是查询所有
ProductExample example = new ProductExample();
// 加入pageHelper对象,实现分页查询
PageHelper.startPage(page, rows);
List<Product>list =products.selectByExample(example);
return list;
}
}
@RestController
public class ProductController {
@Autowired
private IProductService Service;
@RequestMapping("/addproduct")
@ResponseBody
public String addProduct(Product product) {
Product p = new Product();
p.setName("人间词话");
p.setPrice((float) 50);
p.setNum(100);
Service.addProducts(p);
return "success";
}
//实现resfull风格
@RequestMapping("/findproduct/{page}/{row}")
@ResponseBody
public List<Product> findAll(@PathVariable int page, @PathVariable int row) {
List<Product> list = Service.find(page, row);
return list;
}
}
@SpringBootApplication(scanBasePackages= {"com.mybaits.Controller","com.mybaits.Service"})
@MapperScan(basePackages={"com.mybaits.mapper"})//一定要用此注解单独扫描mapper接口
public class ProductTest {
public static void main(String[] args) {
SpringApplication.run(ProductTest.class, args);
}
}
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot的Mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- alibaba的druid的数据库连接池 ,一定要注意版本问题 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
spring:
datasource:
url: jdbc:mysql://localhost:3306/products?characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 30000
filters: stat
async-init: true
server:
port: 8082
public interface ProductMapper {
// @select注解不需要配置xml
@Select("SELECT *FROM product")
//查找全部
List<Product> selectProduct();
}
public interface IProductService {
List<Product> AllProduct();
}
@Service
public class ProductServiceImpl implements IProductService {
@Autowired
private ProductMapper product;
@Override
public List<Product> AllProduct() {
return product.selectProduct();
}
}
@RestController
public class ProductController {
@Autowired
private IProductService service;
@RequestMapping("/select")
public List<Product> select(){
return service.AllProduct();
}
}
@SpringBootApplication(scanBasePackages= {"com.mybaitsIn.Controller","com.mybaitsIn.Service"})
@MapperScan(basePackages={"com.mybaitsIn.mapper"})//一定要用此注解单独扫描mapper接口
public class ProductTest {
public static void main(String[] args) {
SpringApplication.run(ProductTest.class, args);
}
}