springboot整合使用JdbcTemplate
pom文件引入:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<!-- jdbcTemplate 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.properties新增配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
UserService类
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void createUser(String name, Integer age) {
jdbcTemplate.update("insert into users values(null,?,?);", name, age);
}
}
启动类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
这里的@SpringBootApplication注解就相当于@ComponentScan(basePackages = { "com.itmayiedu.controller", "com.itmayiedu.service" })
和@EnableAutoConfiguration
springboot整合使用mybatis
pom文件引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置文件application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mapper代码和实体类通过mybatis的逆向工程获得。
启动类:
@MapperScan("com.springboot.mapper")
//@EnableAutoConfiguration
@SpringBootApplication
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class, args);
}
}
此处注意MapperScan注解的扫包,扫描mapper文件所在的包
Mybatis整合分页插件
Page-helper 是一款好用的开源免费的 Mybatis 第三方物理分页插件
物理分页
支持常见的 12 种数据库。Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等
支持多种分页方式
支持常见的 RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用
maven依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 整合 pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
在application.propeties配置文件中引出pagehelper的配置文件
##pagehelper
mybatis.configLocation=classpath:mybatis/mybatis-config.xml
mybats-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置pageHelper分页查询插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
实体类:
public class User {
private Integer id;
private String name;
private Integer age;
对应的get set方法;
}
Mapper文件:
public interface UserMapper {
@Select("SELECT * FROM USERS ")
List<User> findUserList();
}
Service层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
/**
* page 当前页数<br>
* size 当前展示的数据<br>
*
* @param page
* @param size
* @return
*/
public PageInfo<User> findUserList(int page, int size) {
// 开启分页插件,放在查询语句上面
PageHelper.startPage(page, size);
List<User> listUser = userMapper.findUserList();
// 封装分页之后的数据
PageInfo<User> pageInfoUser = new PageInfo<User>(listUser);
return pageInfoUser;
}
}
Controller层
@RestController
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("/findUserList")
public PageInfo<User> findUserList(int page, int size) {
return userService.findUserList(page, size);
}
}
启动项目
@MapperScan("com.itmayiedu.mapper")
@SpringBootApplication
public class PageHelper {
public static void main(String[] args) {
SpringApplication.run(PageHelper.class, args);
}
}
springboot整合多数据源
application.properties配置文件:
###datasource1
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
###datasource2
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root
配置两个数据源
//DataSource01
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.springboot.test01", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
/**
*
* @methodDesc: 功能描述:(配置test1数据库)
* @param: @return
* @returnType:@return DataSource
*/
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
*
* @methodDesc: 功能描述:(test1 sql会话工厂)
* @param: @parm
* dataSource
* @param: @return
* @param: @throws
* Exception
* @createTime:2017年9月17日 下午3:17:08
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
*/
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
}
/**
*
* @methodDesc: 功能描述:(test1 事物管理)
* @param: @param
* dataSource
* @param: @return
* @param: @throws
* Exception
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
*/
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
dataSource2
//DataSource2
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.springboot.test02", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
/**
*
* @methodDesc: 功能描述:(配置test2数据库)
* @param: @return
* @createTime:2017年9月17日 下午3:16:44
* @returnType:@return DataSource
*/
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
*
* @methodDesc: 功能描述:(test2 sql会话工厂)
* @param: @param
* dataSource
* @param: @return
* @param: @throws
* Exception
* @createTime:2017年9月17日 下午3:17:08
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
*/
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
}
/**
*
* @methodDesc: 功能描述:(test2 事物管理)
* @param: @param
* dataSource
* @param: @return
* @param: @throws
* Exception
* @createTime:2017年9月17日 下午3:17:08
* @returnType:@param dataSource
* @returnType:@return
* @returnType:@throws Exception SqlSessionFactory
*/
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
上面两个配置数据源的文件没有必要掌握,到时候拿来用即可。
创建分包Mapper
public interface User1Mapper {
@Insert("insert into users values(null,#{name},#{age});")
public int addUser(@Param("name") String name, @Param("age") Integer age);
}
public interface User2Mapper {
@Insert("insert into users values(null,#{name},#{age});")
public int addUser(@Param("name") String name, @Param("age") Integer age);
}
service层
@Service
@Slf4j
public class UserServiceTest01 {
@Autowired
private UserMapperTest01 userMapperTest01;
@Transactional(transactionManager = "test1TransactionManager")
public int insertUser(String name, Integer age) {
int insertUserResult = userMapperTest01.insert(name, age);
log.info("######insertUserResult:{}##########", insertUserResult);
int i = 1 / age;
// 怎么样验证事务开启成功!~
return insertUserResult;
}
}
//在多数据源的情况下,使用@Transactional注解时,应该指定事务管理者
//@Transactional(transactionManager = "test2TransactionManager")
controller层:
@RestController
public class MybatisMultilDataSourceController {
@Autowired
private UserServiceTest01 userServiceTest01;
@Autowired
private UserServiceTest02 userServiceTest02;
@RequestMapping("/insertUserTest1")
public Integer insertUserTest1(String name, Integer age) {
return userServiceTest01.insertUser(name, age);
}
@RequestMapping("/insertUserTest2")
public Integer insertUserTest2(String name, Integer age) {
return userServiceTest02.insertUser(name, age);
}
}
启动项目:
@SpringBootApplication
@MapperScan(basePackages = { "com.springboot.mapper" })
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}