1、连接mysql 数据库,无持久层框架模式,可用最基础的JdbcTemplate操作
mysql
mysql-connector-java
5.1.44
org.springframework.boot
spring-boot-starter-jdbc
spring:
datasource:
# 数据库地址
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
# 数据库账号
username: root
# 数据库密码
password: 123456
# 数据库驱动
driverClassName: com.mysql.jdbc.Driver
@Repository
public class ProductDao {
@Autowired
JdbcTemplate jdbcTemplate;
public void selectData(){
String sql = "select * from product";
List
com.alibaba
druid-spring-boot-starter
1.1.10
spring:
datasource:
# 数据库地址
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
# 数据库账号
username: root
# 数据库密码
password: 123456
# 数据库驱动
driverClassName: com.mysql.jdbc.Driver
# 指定哪种连接池
type: com.alibaba.druid.pool.DruidDataSource
druid:
#初始化连接数
initial-size: 1
#最小空闲连接
min-idle: 1
#最大活动连接
max-active: 20
#获取连接时测试是否可用
test-on-borrow: true
#监控页面启动
stat-view-servlet:
allow: true
1、普通集成
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mybatis:
# 实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.example.project.*.*.entity
# mapper.xml文件位置,如果没有映射文件,请注释掉
mapper-locations: classpath:mapper/*.xml
@Mapper
public interface ProductMapper {
public List selectProducts();
}
id,name,url
@Service
public class ProductService {
@Autowired
ProductMapper productMapper;
public void selectProduct(){
List result = productMapper.selectProducts();
}
}
2、使用通用mapper方式(不过部分人好像不推荐这种)
org.mybatis
mybatis-spring
1.3.2
tk.mybatis
mapper-spring-boot-starter
2.0.2
@Mapper
public interface ProductMapper extends tk.mybatis.mapper.common.Mapper{
}
3、集成分页插件pageHelper(利用ThreadLocal实现分页)
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
@Service
public class ProductService {
@Autowired
ProductMapper productMapper;
public void selectProduct(){
PageHelper.startPage(1,10);
List result = productMapper.selectAll();
PageInfo productPageInfo = new PageInfo<>(result);
}
}
4、使用mybatis-plus,自带分页所以用它就不需要pageHelper了,而且BaseMapper也带有很多方法,所以也不需要第二点的通用mapper,并且它还有逆向工程,可以生成代码
com.baomidou
mybatis-plus-boot-starter
3.0.1
mybatis-plus:
# 放在resource目录 classpath:/mapper/*Mapper.xml
mapper-locations: classpath:/mapper/*.xml
# 实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.example.project.*.*.mapper
# 打印日志,如果不需要查询结果
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@Configuration
@MapperScan("com.example.project.*.*.mapper")//这里千万注意只写mapper 所在文件夹,mybatis会进行代理,免得误伤其他文件夹
public class MybatisPlusConfig {
/**
* 分页插件,自动识别数据库类型
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
public interface ProductsMapper extends BaseMapper {
}
//继承mybatis-plus的ServiceImpl方式
@Service
public class ProductsServiceImpl extends ServiceImpl {
public void selectProduct(){
IPage page = new Page<>();
QueryWrapper queryWrapper = new QueryWrapper<>();
IPage result = this.page(page,queryWrapper);
}
}
//也可以不在service层继承,直接调用mapper
@Service
public class ProductsServiceImpl{
@Autowired
ProductsMapper productsMapper;
public void selectProduct(){
IPage page = new Page<>();
QueryWrapper queryWrapper = new QueryWrapper<>();
IPage result = productsMapper.selectPage(page,queryWrapper);
}
}