applicationContext.xml(和mybatis集成后)
db.properties
一般数据库的主要的配置放在代码外的配置文件中即这里的db.properties,如下:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root
# 下面是:
# 配置Druid的最大链接数
# 连接池中最大的活跃连接数
jdbc.pool.maxActive=20
注:Druid连接池是阿里巴巴开源的数据库连接池项目,这里舍弃了spring中的连接池,因为Druid更好用
Druid v1.0 使用手册
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"
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">
<!--加载配置文件-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:jdbc.properties"/>
<!--声明数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.connectionURL}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.pool.maxActive}"/>
</bean>
<!--创建会话工厂,有mybatis提供 org.mybatis.spring.SqlSessionFactoryBean-->
<!--需要主配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--通过setter注入将数据库的连接池赋给sqlSessionFactory,告诉会话工厂数据库的位置-->
<property name="dataSource" ref="dataSource"/>
<!--mybatis 的主配置文件的位置 value="classpath:mybatis-config.xml" 在spring的配置中指定其他文件的路径要使用classpath:-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--创建dao对象 使用mybatis提供的,在内部自动的去调用getMapper()生成每个dao接口的代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定sqlsessionfactory的对象id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--指定dao所在的包名,MapperScannerConfigurer会把包中的每个接口都执行一次getMapper()方法
得到接口的dao对象,创建好的对象放在spring的容器中
-->
<property name="basePackage" value="com.yfs1024.dao"/>
</bean>
</beans>
以上为自己学习的时候所用的配置,可能不是很全,还是希望给您带来帮助!
最后分享一个句子:(兄弟们加油,未来可期!)
鲜衣 怒马 功名 意中人 我鞭长莫及 也马不停蹄

浙公网安备 33010602011771号