数据库配置 如果把配置写到xml配置文件中会显得很乱, 而且不好改, 如果有多个数据库的话, 会非常不方便, 那么可以单独写到一个配置文件中, 这样如果有多个数据库配置的话, 就可以直接切换配置文件了。
下面是我的数据库配置文件(db.properties):
1 2 3 4 5 6 7 8 9 10 11 jdbc.driver =com.mysql.cj.jdbc.Driver jdbc.url =jdbc:mysql://localhost:3306/taotao?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC jdbc.username =littleboy jdbc.password =970711dhz jdbc.pool.init =1 jdbc.pool.minIdle =3 jdbc.pool.maxActive =20 web.view.prefix =/WEB-INF/views/ web.view.suffix =.html
Spring 配置 Spring 配置主要分为三个,
dao层配置, 也就是数据库配置
service层配置
事务配置
dao层配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" > <context:property-placeholder location ="classpath:conf/db.properties" /> <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" destroy-method ="close" > <property name ="url" value ="${jdbc.url}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> <property name ="driverClassName" value ="${jdbc.driver}" /> <property name ="maxActive" value ="${jdbc.pool.maxActive}" /> <property name ="minIdle" value ="${jdbc.pool.minIdle}" /> </bean > <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> <property name ="configLocation" value ="classpath:mybatis/sqlMapConfig.xml" /> <property name ="mapperLocations" value ="classpath*:mapper/*.xml" /> </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="basePackage" value ="com.littleboy.dao" /> </bean > </beans >
这里用的数据库连接池是阿里开发的druid, 可自行百度
这里有一个Mapper扫描器和配置mybaits的东西, 这里涉及到了mybatis的自动生成实体类和Mapper, 这篇讲到了这个。
service 层配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?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:dubbo ="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" > <context:component-scan base-package ="com.littleboy.service.**" /> <dubbo:application name ="manager" /> <dubbo:registry protocol ="zookeeper" address ="127.0.0.1:2182" /> <dubbo:protocol name ="dubbo" port ="20080" /> <dubbo:service interface ="com.littleboy.service.ItemService" ref ="itemServiceImpl" timeout ="600000" /> <dubbo:service interface ="com.littleboy.service.ItemCatService" ref ="itemCatServiceImpl" timeout ="600000" /> </beans >
这里除了必要的自动扫描之外, 还有dubbo的配置, 首先声明了服务的工程名, 接着是配置了注册中心的地址, 这里的地址是你的zookeeper的地址。
最后配置了dubbo的端口和服务接口
translation 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <?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:tx ="http://www.springframework.org/schema/tx" xmlns:aop ="http://www.springframework.org/schema/aop" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" /> </bean > <tx:advice id ="txAdvice" transaction-manager ="transactionManager" > <tx:attributes > <tx:method name ="insert*" propagation ="REQUIRED" /> <tx:method name ="update*" propagation ="REQUIRED" /> <tx:method name ="edit*" propagation ="REQUIRED" /> <tx:method name ="save*" propagation ="REQUIRED" /> <tx:method name ="add*" propagation ="REQUIRED" /> <tx:method name ="new*" propagation ="REQUIRED" /> <tx:method name ="set*" propagation ="REQUIRED" /> <tx:method name ="remove*" propagation ="REQUIRED" /> <tx:method name ="delete*" propagation ="REQUIRED" /> <tx:method name ="change*" propagation ="REQUIRED" /> <tx:method name ="get*" propagation ="REQUIRED" read-only ="true" /> <tx:method name ="find*" propagation ="REQUIRED" read-only ="true" /> <tx:method name ="load*" propagation ="REQUIRED" read-only ="true" /> <tx:method name ="*" propagation ="REQUIRED" read-only ="true" /> </tx:attributes > </tx:advice > <aop:config > <aop:advisor advice-ref ="txAdvice" pointcut ="execution(* com.littleboy.service..*.*(..))" /> </aop:config > </beans >
没啥可说的, 配置了事务, 并使用aop将事务切入到service中, 可以看看数据库方面的书来补充一下事务方面的知识。
web.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app id ="WebApp_ID" version ="3.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" > <display-name > manager</display-name > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring/application*.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > </web-app >
配置了Spring的监听器, 加载Spring容器
下一篇讲web工程的配置。