零、Mybatis与Mysql连接池使用
一、代码
1、POM依赖
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
compile group: 'com.alibaba', name: 'druid', version: '1.2.4'
2、application.properties配置数据库
#数据库
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://1.2.3.4:3306/TestDBMysql?allowMultiQueries=true
jdbc.username=admin
jdbc.password=123456
jdbc.maxActive=10
jdbc.maxIdle=10
jdbc.minIdle=2
druid配置文件 resources/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath*:*.properties"/>
<context:component-scan base-package="com.longze">
</context:component-scan>
<!-- <context:component-scan base-package="springfox.documentation" use-default-filters="true">-->
<!-- </context:component-scan>-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="redirectHttp10Compatible" value="false" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 数据池中最大连接数和最小连接数-->
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池
给 sqlsessionFactory 的属性 dataSource 赋值
ref="引用该 spring容器 中的另一个 bean的id"
-->
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 注入 映射文件 mapper
给 sqlsessionFactory 的属性 mapperLocation 赋值
value="映射文件 XXXmapper.xml 的相对路径"
-->
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<!-- 3) 获取 SqlSession 对象,并交由 spring 管理 用SqlSessionTemplate得到的SqlSession可以不用我们自己操心事务的管理,以及关闭操作-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 给 SqlSessionTemplate 的构造函数赋值-->
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
Mybatis的配置文件resources/mybatis-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>
<settings>
<!-- STDOUT_LOGGING-->
<!-- <setting name="logImpl" value="LOG4J2" />-->
<!-- <setting name="logImpl" value="STDOUT_LOGGING" />-->
<setting name="callSettersOnNulls" value="true"/>
<setting name="jdbcTypeForNull" value="NULL" />
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置分页插件-->
<!-- <plugins>-->
<!-- <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>-->
<!-- </plugins>-->
</configuration>
3、MainApplication.java启动类配置
@SpringBootApplication
@ImportResource({"classpath:applicationContext.xml"})
public class SecurityApplication {
public static void main(String[] args) {SpringApplication.run(SecurityApplication.class, args);}
}
4、Mybatis线程池工具类
com.longze.util.MyBatisUtil.java
package com.longze.security.util;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
@Component
public class MyBatisUtil {
private static WebApplicationContext context;
public static WebApplicationContext getContext() {
return context;
}
@Autowired
public void setContext(WebApplicationContext context) {
setContextStatic(context);
}
public static SqlSession getSqlSession() {
return (SqlSession) context.getBean("sqlSession");
}
public static void setContextStatic(WebApplicationContext context){
MyBatisUtil.context=context;
}
}
5、使用线程池工具类
@Service
public class MyBatisTestServiceImpl implements MyBatisTestService{
@Override
public int save(UserDTO dto){
try {
//使用线程池
int i = MyBatisUtil.getSqlSession().insert("com.longze.dao.testDao.insertUser", dto);
return i;
} catch (Exception e) {
throw e;
}
}
}
二、多数据源应用
|