本文使用最新版本(4.1.5)的springmvc+spring+mybatis,採用最間的配置方式來進行搭建。java
1. web.xmlmysql
咱們知道springmvc是基於Servlet: DispatcherServlet來處理分發請求的,因此咱們須要先在web.xml文件中配置DispatcherServlet,而Spring的啓動則是使用了監聽器,因此須要配置spring的監聽器:web
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>sp</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> </web-app>
servlet下面的init-param中的指定了springmvc的dispatcherServlet的配置文件:config/spring-mvc.xml,全部springmvc相關的都在該文件中進行配置。在DispatcherServlet(其父類)中使用:getServletConfig().getInitParameter("paramName"); 能夠訪問到init-param中指定的參數,從而能夠讀取到config/spring-mvc.xml文件。load-on-startup值爲1指定了dispatcherServlet隨servlet容器啓動。spring
ContextLoaderListener是spring監聽servlet容器的啓動的,在servlet容器啓動時,就初始化bean工廠,對bean進行初始化等等操做。context-param指定了spring的配置文件config/applicationContext.xml,能夠使用: getServletContext().getInitParameter("paraName"); 讀取到值。sql
注意:init-param 和 context-param 的區別,從名字上就能夠看得出,後者是相對於整個web應用的,而前者是針對單個servlet的。數據庫
2. springmvc.xmlapache
下面咱們看一下springmvc.xml該如何配置:spring-mvc
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="net.aazj.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> // ... ... </beans>
啓用註解驅動來掃描controller,並指定control的包路徑,還有指定了視圖解析器,so easy。緩存
3. applicationContext.xmlmybatis
spring中相關bean掃描,事物的配置,以及和mybatis的結合配置以下所示:
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="net.aazj.service" /> <!-- 引入屬性文件 --> <context:property-placeholder location="classpath:config/db.properties" /> <!-- 配置數據源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化鏈接大小 --> <property name="initialSize" value="0" /> <!-- 鏈接池最大使用鏈接數量 --> <property name="maxActive" value="20" /> <!-- 鏈接池最大空閒 --> <property name="maxIdle" value="20" /> <!-- 鏈接池最小空閒 --> <property name="minIdle" value="0" /> <!-- 獲取鏈接最大等待時間 --> <property name="maxWait" value="60000" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" /> </bean> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用annotation定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="net.aazj.mapper" /> </bean> </beans>
一樣相關service bean也使用基於註解的掃描方式:context:component-scan,事務也使用註解來驅動:tx:annotation-driven,因此須要在serviceImpl相關類上和方法上使用@Transanctional註解類配置事物。
sqlSessionFactory的配置至關重要,configLocation指定了mybatis的配置文件,若是須要在mybatis配置文件中配置好比<settings>, <typeAliases>, <mappers>則,須要在這裏指定,若是不須要就沒有必要指定值了。mapperLocations指定了mapper接口映射sql語句的xml文件的位置。MapperScannerConfigurer指定了mapper接口所在的包路徑。
4. mybatis-config.xml
spring和mybatis的接口,其實能夠不須要mybatis-config.xml文件的存在,只有在須要配置<settings>, <typeAliases>, <mappers>(其實mapper也一併也是在applicationContext.xml中進行配置)才須要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> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <typeAliases> <package name="net.aazj.pojo"/> </typeAliases> </configuration>
<settings>指定了數據庫操做相關的設置,typeAliases指定了能夠給數據庫表對應的類所在的包路徑,能夠在sql的xml使用它們的別名:
package net.aazj.pojo; import org.apache.ibatis.type.Alias; @Alias("User") public class User { private Integer id; private String name; // ... ... }
@Alias("User")註解了該pojo的別名,因此能夠在xml文件中使用別名 User 來代替:net.aazj.pojo.User
<?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="net.aazj.mapper.UserMapper"> <cache /> <select id="getUser" resultType="User"> select * from user where id = #{id} </select> <select id="addUser" parameterType="string"> insert into user(name) values(#{name}) </select> </mapper>
這裏 resultType="User" 不須要使用全限定類名。<cache />啓用了基於namespace="net.aazj.mapper.UserMapper"的全局緩存。
5. generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <classPathEntry location="D:\java_libs\repository\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" /> <context id="MySQLTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="root" password="xxxxx"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="net.aazj.pojo" targetProject="sp\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="config.mappers" targetProject="sp\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="net.aazj.mapper" targetProject="sp\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table schema="sy" tableName="tbug" domainObjectName="Bug" > <property name="useActualColumnNames" value="false"/> <generatedKey column="id" sqlStatement="mysql" identity="true" /> <!-- <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> <table schema="sy" tableName="user" domainObjectName="User" > <property name="useActualColumnNames" value="false"/> <generatedKey column="id" sqlStatement="mysql" identity="true" /> </table> </context> </generatorConfiguration>
上面是Mybatis generator的配置文件:
1)classPathEntry 指定驅動位置;
2)jdbcConnection 指定數據庫鏈接信息;
3)javaModelGenerator 指定生成的pojo類的位置;
4)sqlMapGenerator 指定指定生成的sql xml文件的位置;
5)javaClientGenerator 指定 mapper 接口的位置;
6)table 指定將數據庫中哪些表進行處理;<generatedKey column="id" sqlStatement="mysql" identity="true" /> 用於指定主鍵;
核心技術:Maven,Springmvc mybatis shiro, Druid, Restful, Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx
1. 項目核心代碼結構截圖
項目模塊依賴
特別提醒:開發人員在開發的時候能夠將本身的業務REST服務化或者Dubbo服務化
2. 項目依賴介紹
2.1 後臺管理系統、Rest服務系統、Scheculer定時調度系統依賴以下圖:
2.2 Dubbo獨立服務項目依賴以下圖:
3. 項目功能部分截圖:
zookeeper、dubbo服務啓動
dubbo管控臺
REST服務平臺