- 浏览: 60024 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (93)
- java (3)
- ios (9)
- wp (15)
- android (0)
- js (1)
- 服务器 (0)
- db (0)
- linux (1)
- python (0)
- xcode (0)
- ide (2)
- maven (0)
- spring (0)
- sql (0)
- 第三方 (1)
- nexus (0)
- nginx (11)
- tomcat (0)
- jenkins (0)
- zookeeper (1)
- git (1)
- svn (0)
- uml (0)
- redis (4)
- activemq (1)
- flume (0)
- kafka (0)
- mysql (1)
- memcached (0)
- mybatis (0)
- mac (0)
- mongo (1)
- docker (6)
- cache (0)
- jvm (0)
- markdown (0)
- springboot (24)
- mycat (3)
- LTS (3)
- 运维 (0)
- opts (1)
- netty (1)
- tcc (0)
- ffmpeg (2)
- 直播 (6)
- cxf (0)
- nodejs (0)
- storm (0)
- elasticjob (0)
- php (0)
最新评论
================================================
pom.xml
================================================
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- db start -->
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid}</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.1</version>
</dependency>
<!-- db end -->
================================================
application.xml
================================================
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://host:port/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=name
spring.datasource.password=psd
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
================================================
MybatisConfig.java
================================================
package org.spring.springboot.ds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = MybatisConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "org.spring.springboot.dao";
static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
@Value("${spring.datasource.type}")
private Class<? extends DataSource> dataSourceType;
@Bean(name="masterDataSource", destroyMethod = "close", initMethod="init")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().type(dataSourceType).build();
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MybatisConfig.MAPPER_LOCATION));
//分页插件
PageHelper pageHelper = new PageHelper();
Properties props = new Properties();
props.setProperty("dialect", "mysql");
props.setProperty("pageSizeZero", "true");
props.setProperty("reasonable", "false");
//=============================
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageHelper.setProperties(props);
//添加插件
sessionFactory.setPlugins(new Interceptor[]{pageHelper});
return sessionFactory.getObject();
}
}
================================================
CityDao.java
================================================
@Repository
public interface CityDao {
/**
* 根据城市名称,查询城市信息
*
* @param cityName 城市名
*/
List<City> findByName(@Param("cityName") String cityName);
}
================================================
CityMapper.xml
resource/mapper/CityMapper.xml
================================================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.spring.springboot.dao.CityDao">
<resultMap id="BaseResultMap" type="org.spring.springboot.domain.City">
<result column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" />
<result column="description" property="description" />
</resultMap>
<parameterMap id="City" type="org.spring.springboot.domain.City"/>
<sql id="Base_Column_List">
id, province_id, city_name, description
</sql>
<select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List" />
from city
where city_name = #{cityName}
</select>
</mapper>
================================================
CityServiceImpl.java
================================================
@Autowired
private CityDao cityDao;
public PageInfo findCityByName(String cityName) {
PageHelper.startPage(1, 10);
PageInfo<City> page = new PageInfo(cityDao.findByName(cityName));
return page;
}
pom.xml
================================================
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- db start -->
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid}</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.1</version>
</dependency>
<!-- db end -->
================================================
application.xml
================================================
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://host:port/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=name
spring.datasource.password=psd
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
================================================
MybatisConfig.java
================================================
package org.spring.springboot.ds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = MybatisConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "org.spring.springboot.dao";
static final String MAPPER_LOCATION = "classpath:mapper/*.xml";
@Value("${spring.datasource.type}")
private Class<? extends DataSource> dataSourceType;
@Bean(name="masterDataSource", destroyMethod = "close", initMethod="init")
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().type(dataSourceType).build();
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MybatisConfig.MAPPER_LOCATION));
//分页插件
PageHelper pageHelper = new PageHelper();
Properties props = new Properties();
props.setProperty("dialect", "mysql");
props.setProperty("pageSizeZero", "true");
props.setProperty("reasonable", "false");
//=============================
props.setProperty("supportMethodsArguments", "true");
props.setProperty("returnPageInfo", "check");
props.setProperty("params", "count=countSql");
pageHelper.setProperties(props);
//添加插件
sessionFactory.setPlugins(new Interceptor[]{pageHelper});
return sessionFactory.getObject();
}
}
================================================
CityDao.java
================================================
@Repository
public interface CityDao {
/**
* 根据城市名称,查询城市信息
*
* @param cityName 城市名
*/
List<City> findByName(@Param("cityName") String cityName);
}
================================================
CityMapper.xml
resource/mapper/CityMapper.xml
================================================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.spring.springboot.dao.CityDao">
<resultMap id="BaseResultMap" type="org.spring.springboot.domain.City">
<result column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" />
<result column="description" property="description" />
</resultMap>
<parameterMap id="City" type="org.spring.springboot.domain.City"/>
<sql id="Base_Column_List">
id, province_id, city_name, description
</sql>
<select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List" />
from city
where city_name = #{cityName}
</select>
</mapper>
================================================
CityServiceImpl.java
================================================
@Autowired
private CityDao cityDao;
public PageInfo findCityByName(String cityName) {
PageHelper.startPage(1, 10);
PageInfo<City> page = new PageInfo(cityDao.findByName(cityName));
return page;
}
发表评论
-
springboot:condition
2017-07-26 11:10 360public class LinuxCondition imp ... -
springboot:tomcat启动
2017-07-20 15:02 9201.在pom.xml里设置 <packaging> ... -
springboot:shiro
2017-07-13 15:52 964第一次学习系统学习shiro 并将shiro集成到sprin ... -
springboot:upload
2017-07-06 10:25 750FileUploadConfiguration.java == ... -
springboot:servlet
2017-07-06 10:17 504Application.java ============== ... -
springboot:freemarker
2017-07-05 17:33 556pom.xml ======================= ... -
springboot:task
2017-07-05 12:11 441TaskPool.java ================= ... -
springboot:热部署
2017-07-05 11:23 355pom.xml: ====================== ... -
springboot:注解
2017-07-04 11:36 624@EnableAutoConfiguration注解 excl ... -
springboot:server属性配置
2017-07-04 10:05 750server配置 ====================== ... -
springboot:memcached
2017-07-03 17:23 925pom.xml ======================= ... -
springboot:health
2017-07-03 16:43 396<dependency> ... -
springboot:mongodb
2017-07-03 15:38 1562pom.xml ======================= ... -
springboot:quartz集群
2017-07-02 20:40 998pom.xml ======================= ... -
springboot:ControllerAdvice
2017-07-02 14:09 372全局异常拦截 //@ControllerAdvice(anno ... -
springboot:dubbo
2017-07-02 10:40 500server: ======================= ... -
springboot:amq
2017-07-01 22:20 466pom.xml ======================= ... -
springboot:redis(jedis)
2017-07-01 14:10 904application.properties ======== ... -
springboot:logback
2017-06-30 16:20 553=============================== ... -
springboot:interceptor
2017-06-30 14:04 630IncpConfig.java 增加拦截器config 继承W ...
相关推荐
在本文中,我们将深入探讨如何使用SpringBoot、Mybatis、Druid和PageHelper来实现多数据源和分页功能。首先,SpringBoot是基于Spring框架的简化版本,它旨在简化微服务开发,提供了自动配置、内嵌式Web服务器以及...
《SpringBoot、MyBatis与Druid整合应用详解》 在现代Java开发中,SpringBoot以其简化配置、快速启动的特点,成为了许多项目首选的框架。而MyBatis作为一款轻量级的持久层框架,提供了灵活的SQL操作,极大地提高了...
在IT行业中,SpringBoot、MyBatis和Druid都是广泛使用的框架和技术,它们共同构建了高效、便捷的Java开发环境。下面将详细讲解这三个技术以及它们如何与分页插件结合使用。 首先,SpringBoot是Spring框架的一个轻量...
在SpringBoot和MyBatis项目中,通常会使用一些分页插件,如PageHelper或Mybatis-Plus,它们可以简化SQL的分页编写,提高代码的可读性和可维护性。 **代码生成器** 51bang项目中可能包含了代码生成工具,如MyBatis的...
在本文中,我们将深入探讨如何使用Spring Boot、MyBatis、Druid和PageHelper来实现多数据源分页。Spring Boot作为一个快速开发框架,简化了Java应用的搭建和配置过程,而MyBatis则是一个轻量级的ORM(对象关系映射)...
在本实例中,我们将深入探讨如何整合SpringBoot、MyBatis、Druid、PageHelper以及FreeMarker这五大组件,创建一个高效且易于管理的Web应用。这些技术在现代Java开发中占据着重要的地位,尤其在构建后端服务时,它们...
计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料计算机技术、IT咨询、人工智能AI理论介绍,学习参考资料计算机技术、IT咨询、人工智能AI理论介绍,学习...
在本项目中,我们主要探讨的是一个基于Spring Boot框架,结合MyBatis、PageHelper分页插件、Druid连接池以及Generator逆向工程插件的整合应用。这些技术都是现代Java开发中常用且重要的组件,下面将逐一详细介绍它们...
springBoot_mybatis_druid springBoot集成mybatis并使用druid连接池与PageHelper分页插件,项目模板(使用generator插件自动生成dao,mapper)1.准备数据库: CREATE TABLE tb_user( id int(32) PRIMARY KEY AUTO_...
在SpringBoot项目中,整合Mybatis-Plus并实现多数据源的动态切换,同时支持分页查询是一项常见的需求。以下将详细阐述这个过程中的关键步骤和技术要点。 首先,我们需要引入必要的Maven依赖。这里提到了四个关键...
在本文中,我们将深入探讨如何在SpringBoot项目中整合MyBatis,并利用Druid作为数据源进行SQL执行监控。这是一项关键的技术实践,对于优化应用性能、排查问题以及提升数据库操作效率具有重要意义。 首先,...
在本示例项目"springboot-mybatis-pagehelper-jsp"中,主要展示了如何将Spring Boot、MyBatis、PageHelper和JSP技术整合在一起,构建一个功能完善的Web应用。下面将详细阐述这些技术及其整合的关键点。 首先,...
SpringBoot整合MyBatis使得数据访问更加简便和高效,通过Maven管理依赖,使用Druid连接池优化数据库连接,PageHelper插件实现动态分页。这样的集成方式大大简化了传统Java Web项目的复杂性,提升了开发效率。在实际...
在本文中,我们将深入探讨如何使用SpringBoot框架与Druid连接池和PageHelper分页插件进行集成,以及如何在MySQL数据库环境下实现MyBatis的高效分页查询。虽然提供的描述提到使用Oracle数据库,但考虑到MySQL是更常见...
springboot 2.0 集成 mybatis 环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 2.0.1.RELEASE jdk:1.8.0_40 maven:3.3.9 alibaba Druid 数据库连接池:1.1.9 额外功能: PageHelper 分页插件
综上所述,本例子展示了如何在SpringBoot环境下集成MyBatis作为持久层框架,使用Druid作为数据连接池,通过PageHelper实现分页查询,并且采用BaseDao的方式来简化数据库操作。此外,还使用了JSP技术进行前端展示。...
本项目主要讲述了如何将Spring Boot与MyBatis、Druid和PageHelper集成,同时提到了AOP(面向切面编程)以及事务管理,另外还提及了后续可能集成的Swagger和多数据源技术。 首先,Spring Boot与MyBatis的集成是为了...
1、基于yml 配置方式 ,实现springBoot+sharding-jdbc+mybatis-plus 实现分库分表,读写分离,以及全局表,子表的配置。 2、实现mybatis-plus 整合到springboot 详细使用请看 测试用例
spring-boot-seed项目介绍SpringBoot的种子框架项目,个人学习使用,集成一些常用的框架功能,方便快速开发。软件架构spring-boot-seed ├── src/main/java/com.dazzlzy | ├── common -- 通用代码包 | | ├─...