使用idea建立maven多模塊開(kāi)發(fā)以及ssm整合
-
建立主要工程項(xiàng)目,建立pom.xml,該pom用于連接各個(gè)模塊之間的pom
先建立一個(gè)整體項(xiàng)目my-shop的pom,用于包含各個(gè)模塊pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.funtl</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>my-shop-dependencies</module>
<module>my-shop-commons</module>
<module>my-shop-domain</module>
<module>my-shop-web-admin</module>
<module>my-shop-web-ui</module>
<module>my-shop-web-api</module>
</modules>
</project>
```
建立依賴模塊dependencies,項(xiàng)目中所有的依賴包都在該模塊下管理
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>my-shop-dependencies</artifactId>
<packaging>pom</packaging>
<name>my-shop-dependencies</name>
<description></description>
<properties>
<!-- 環(huán)境配置 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 統(tǒng)一的依賴管理 -->
<commons-lang3.version>3.5</commons-lang3.version>
<jstl.version>1.2</jstl.version>
<log4j.version>1.2.17</log4j.version>
<servlet-api.version>3.1.0</servlet-api.version>
<slf4j.version>1.7.25</slf4j.version>
<spring.version>4.3.17.RELEASE</spring.version>
<durid.version>1.1.6</durid.version>
<mysql-conector-java.version>5.1.46</mysql-conector-java.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<spring-jdbc.version>4.3.17.RELEASE</spring-jdbc.version>
<spring-test.version>4.3.17.RELEASE</spring-test.version>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring End -->
<!-- Servlet Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Servlet End -->
<!-- Log Begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Log End -->
<!-- Commons Begin -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- Commons End -->
<!--Druid Begin-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${durid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-conector-java.version}</version>
</dependency>
<!--Druid End-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
<!--end-->
<!--junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-test.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- Compiler 插件, 設(shè)定 JDK 版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
<!-- 資源文件配置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
建立通用模塊commons,一下通用的類放在該模塊下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-shop-commons</artifactId>
<packaging>jar</packaging>
<name>my-shop-commons</name>
<description></description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>
建立實(shí)體類模塊
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-shop-domain</artifactId>
<packaging>jar</packaging>
<name>my-shop-domain</name>
<description></description>
</project>
建立后臺(tái)模塊
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-shop-web-admin</artifactId>
<packaging>war</packaging>
<name>my-shop-web-admin</name>
<description></description>
<dependencies>
<!-- Spring Begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- Spring End -->
<!-- Servlet Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Servlet End -->
<!-- Commons Begin -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-commons</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-domain</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!--Druid Begin-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Druid End-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
建立接口api模塊
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-shop-web-api</artifactId>
<packaging>war</packaging>
<name>my-shop-web-api</name>
<description></description>
<dependencies>
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-commons</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
建立ui模塊
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
<artifactId>my-shop-web-ui</artifactId>
<packaging>war</packaging>
<name>my-shop-web-ui</name>
<description></description>
<dependencies>
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-commons</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
在admin模塊整合ssm
在pom中添加代碼,引入上面的依賴包
建立以下的文件夾
1.在web.xml中配置spring自動(dòng)注冊(cè)容器
<!--配置spring自動(dòng)注冊(cè)容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在resource文件夾下建立spring配置文件spring-context.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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--除了controller注解,controller注解交給SpringMVC-->
<context:component-scan base-package="com.funtl.my.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
2.在web.xml添加SpringMVC的配置
<!--配置SpringMVC核心分發(fā)器-->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在resource文件夾下建立spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<description>Spring MVC Configuration</description>
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
<!-- 使用 Annotation 自動(dòng)注冊(cè) Bean,只掃描 @Controller -->
<context:component-scan base-package="com.funtl.my.shop" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 默認(rèn)的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 定義視圖文件解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${web.view.prefix}"/>
<property name="suffix" value="${web.view.suffix}"/>
</bean>
<!-- 靜態(tài)資源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
3.到目前為止就已經(jīng)把spring和springmvc整合到一起了,最后需要整合mybatis,mybatis需要連接池,雖然它有自帶的連接池,但是效率較低徘层,這里使用阿里的durid連接池,所以把durid連接池整合到spring,
其實(shí)整合原理就是把它們的bean創(chuàng)建起來(lái),而一般框架都有兩種設(shè)置配置的方式,一種是讀取xml峻呕,一直就是直接在new的對(duì)象中配置,所以才可以整合到spring,個(gè)人理解
spring整合durid
<?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 http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
<!-- 數(shù)據(jù)源配置, 使用 Druid 數(shù)據(jù)庫(kù)連接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 數(shù)據(jù)源驅(qū)動(dòng)類可不寫(xiě)利职,Druid默認(rèn)會(huì)自動(dòng)根據(jù)URL識(shí)別DriverClass -->
<property name="driverClassName" value="${jdbc.driverClass}"/>
<!-- 基本屬性 url、user瘦癌、password -->
<property name="url" value="${jdbc.connectionURL}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小猪贪、最小、最大 -->
<property name="initialSize" value="${jdbc.pool.init}"/>
<property name="minIdle" value="${jdbc.pool.minIdle}"/>
<property name="maxActive" value="${jdbc.pool.maxActive}"/>
<!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
<property name="maxWait" value="60000"/>
<!-- 配置間隔多久才進(jìn)行一次檢測(cè)讯私,檢測(cè)需要關(guān)閉的空閑連接热押,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="${jdbc.testSql}"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters -->
<property name="filters" value="stat"/>
</bean>
</beans>
這樣spring就已經(jīng)創(chuàng)建了數(shù)據(jù)源,durid還有servlet在web.xml配置servlet用于可視化監(jiān)控,可以設(shè)置可以不設(shè)置,可以在瀏覽器中輸入localhost:8080/durid/,前面根據(jù)你自己的域名
<!--配置durid視圖servlet-->
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
4.把durid 整合到 mybatis
mybatis的配置
<?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>
<!-- 全局參數(shù) -->
<settings>
<!-- 打印 SQL 語(yǔ)句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
<!-- 使全局的映射器啟用或禁用緩存斤寇。 -->
<setting name="cacheEnabled" value="false"/>
<!-- 全局啟用或禁用延遲加載桶癣。當(dāng)禁用時(shí),所有關(guān)聯(lián)對(duì)象都會(huì)即時(shí)加載娘锁。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 當(dāng)啟用時(shí)牙寞,有延遲加載屬性的對(duì)象在被調(diào)用時(shí)將會(huì)完全加載任意屬性。否則莫秆,每種屬性將會(huì)按需要加載间雀。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否允許單條 SQL 返回多個(gè)數(shù)據(jù)集 (取決于驅(qū)動(dòng)的兼容性) default:true -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 是否可以使用列的別名 (取決于驅(qū)動(dòng)的兼容性) default:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 允許 JDBC 生成主鍵。需要驅(qū)動(dòng)器支持镊屎。如果設(shè)為了 true惹挟,這個(gè)設(shè)置將強(qiáng)制使用被生成的主鍵,有一些驅(qū)動(dòng)器不兼容不過(guò)仍然可以執(zhí)行缝驳。 default:false -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 指定 MyBatis 如何自動(dòng)映射 數(shù)據(jù)基表的列 NONE:不映射 PARTIAL:部分 FULL:全部 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 這是默認(rèn)的執(zhí)行類型 (SIMPLE: 簡(jiǎn)單连锯; REUSE: 執(zhí)行器可能重復(fù)使用prepared statements語(yǔ)句;BATCH: 執(zhí)行器可以重復(fù)執(zhí)行語(yǔ)句和批量更新) -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!-- 使用駝峰命名法轉(zhuǎn)換字段党巾。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 設(shè)置本地緩存范圍 session:就會(huì)有數(shù)據(jù)的共享 statement:語(yǔ)句范圍 (這樣就不會(huì)有數(shù)據(jù)的共享 ) defalut:session -->
<setting name="localCacheScope" value="SESSION"/>
<!-- 設(shè)置 JDBC 類型為空時(shí),某些驅(qū)動(dòng)程序 要指定值, default:OTHER萎庭,插入空值時(shí)不需要指定類型 -->
<setting name="jdbcTypeForNull" value="NULL"/>
</settings>
</configuration>
spring整合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"
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">
<!-- 配置 SqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 用于配置對(duì)應(yīng)實(shí)體類所在的包,多個(gè) package 之間可以用 ',' 號(hào)分割 -->
<property name="typeAliasesPackage" value="com.funtl.my.shop.entity"/>
<!-- 用于配置對(duì)象關(guān)系映射配置文件所在目錄 -->
<property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>
<!-- 掃描 Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.funtl.my.shop.web.admin.dao" />
</bean>
</beans>
最后是整體項(xiàng)目的配置參數(shù),也可以把這些參數(shù)移出去單獨(dú)配置,這里是放在一起好管理
myshop.properties
\#============================#
\#==== JDBC settings ====#
\#============================#
\# JDBC
\# MySQL 8.x: com.mysql.cj.jdbc.Driver
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/myshop?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
\# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
\# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL
\#============================#
\#==== Framework settings ====#
\#============================#
\# 設(shè)置springmvc的前綴和后綴齿拂,這樣在使用的時(shí)候就不需要寫(xiě)路徑和#后綴了
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp
測(cè)試
非常ok