我個人比較喜歡寫註釋,在工作中對註釋的重要性看的也比較高,所以大部分文字都在註釋中,代碼外的文字會寫的偏少,關鍵能懂就行 先看一下整合後的工程目錄(單工程,多工程以後會採用maven) 5個package分別對應 action, entity, mapper(也就是DAO,採用動態代理), serv ...
我個人比較喜歡寫註釋,在工作中對註釋的重要性看的也比較高,所以大部分文字都在註釋中,代碼外的文字會寫的偏少,關鍵能懂就行
先看一下整合後的工程目錄(單工程,多工程以後會採用maven)
5個package分別對應 action, entity, mapper(也就是DAO,採用動態代理), service, service 實現類
resources 下 相關資源文件:
db.properties: 資料庫連接參數
log4j.properties: 日誌
applicationContext-dao.xml: spring 和 mybatis 整合的一些配置(sqlSessionFactory,mapper 配置)
applicationContext-service.xml: 業務
applicationContext.xml: spring 核心配置文件(配置一些公用的內容,比如數據源,事務)
springmvc.xml: springmvc 全局配置文件
SqlMapConfig.xml: mybatis 全局配置文件
webapp結構簡單就不多說了
下麵先貼出maven的pom內容:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.lee</groupId> 6 <artifactId>ssm</artifactId> 7 <name>maven-ssm-hello</name> 8 <packaging>war</packaging> 9 <version>1.0.0-BUILD-SNAPSHOT</version> 10 <properties> 11 <java-version>1.7</java-version> 12 <org.springframework-version>3.2.3.RELEASE</org.springframework-version> 13 <org.aspectj-version>1.6.10</org.aspectj-version> 14 <org.slf4j-version>1.6.6</org.slf4j-version> 15 </properties> 16 17 <dependencies> 18 <!-- Spring --> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-context</artifactId> 22 <version>${org.springframework-version}</version> 23 <exclusions> 24 <!-- Exclude Commons Logging in favor of SLF4j --> 25 <exclusion> 26 <groupId>commons-logging</groupId> 27 <artifactId>commons-logging</artifactId> 28 </exclusion> 29 </exclusions> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework</groupId> 33 <artifactId>spring-webmvc</artifactId> 34 <version>${org.springframework-version}</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-jdbc</artifactId> 39 <version>${org.springframework-version}</version> 40 </dependency> 41 42 <!-- data source: dbcp --> 43 <dependency> 44 <groupId>commons-dbcp</groupId> 45 <artifactId>commons-dbcp</artifactId> 46 <version>1.4</version> 47 </dependency> 48 49 50 <!-- AspectJ --> 51 <dependency> 52 <groupId>org.aspectj</groupId> 53 <artifactId>aspectjrt</artifactId> 54 <version>${org.aspectj-version}</version> 55 </dependency> 56 57 <!-- Logging --> 58 <dependency> 59 <groupId>org.slf4j</groupId> 60 <artifactId>slf4j-api</artifactId> 61 <version>${org.slf4j-version}</version> 62 </dependency> 63 <dependency> 64 <groupId>org.slf4j</groupId> 65 <artifactId>jcl-over-slf4j</artifactId> 66 <version>${org.slf4j-version}</version> 67 <scope>runtime</scope> 68 </dependency> 69 <dependency> 70 <groupId>org.slf4j</groupId> 71 <artifactId>slf4j-log4j12</artifactId> 72 <version>${org.slf4j-version}</version> 73 <scope>runtime</scope> 74 </dependency> 75 <dependency> 76 <groupId>log4j</groupId> 77 <artifactId>log4j</artifactId> 78 <version>1.2.15</version> 79 <exclusions> 80 <exclusion> 81 <groupId>javax.mail</groupId> 82 <artifactId>mail</artifactId> 83 </exclusion> 84 <exclusion> 85 <groupId>javax.jms</groupId> 86 <artifactId>jms</artifactId> 87 </exclusion> 88 <exclusion> 89 <groupId>com.sun.jdmk</groupId> 90 <artifactId>jmxtools</artifactId> 91 </exclusion> 92 <exclusion> 93 <groupId>com.sun.jmx</groupId> 94 <artifactId>jmxri</artifactId> 95 </exclusion> 96 </exclusions> 97 <scope>runtime</scope> 98 </dependency> 99 100 <!-- @Inject --> 101 <dependency> 102 <groupId>javax.inject</groupId> 103 <artifactId>javax.inject</artifactId> 104 <version>1</version> 105 </dependency> 106 107 <!-- Servlet --> 108 <dependency> 109 <groupId>javax.servlet</groupId> 110 <artifactId>servlet-api</artifactId> 111 <version>2.5</version> 112 <scope>provided</scope> 113 </dependency> 114 <dependency> 115 <groupId>javax.servlet.jsp</groupId> 116 <artifactId>jsp-api</artifactId> 117 <version>2.1</version> 118 <scope>provided</scope> 119 </dependency> 120 <dependency> 121 <groupId>javax.servlet</groupId> 122 <artifactId>jstl</artifactId> 123 <version>1.2</version> 124 </dependency> 125 126 <!-- mybatis --> 127 <dependency> 128 <groupId>org.mybatis</groupId> 129 <artifactId>mybatis</artifactId> 130 <version>3.2.3</version> 131 </dependency> 132 <dependency> 133 <groupId>org.mybatis</groupId> 134 <artifactId>mybatis-spring</artifactId> 135 <version>1.2.3</version> 136 </dependency> 137 <dependency> 138 <groupId>mysql</groupId> 139 <artifactId>mysql-connector-java</artifactId> 140 <version>5.1.38</version> 141 </dependency> 142 143 <!-- Test --> 144 <dependency> 145 <groupId>junit</groupId> 146 <artifactId>junit</artifactId> 147 <version>4.7</version> 148 <scope>test</scope> 149 </dependency> 150 151 <dependency> 152 <groupId>com.alibaba</groupId> 153 <artifactId>druid</artifactId> 154 <version>1.0.18</version> 155 </dependency> 156 157 <dependency> 158 <groupId>org.aspectj</groupId> 159 <artifactId>aspectjweaver</artifactId> 160 <version>1.7.2</version> 161 </dependency> 162 <dependency> 163 <groupId>com.alibaba</groupId> 164 <artifactId>fastjson</artifactId> 165 <version>1.1.34</version> 166 </dependency> 167 <dependency> 168 <groupId>com.fasterxml.jackson.core</groupId> 169 <artifactId>jackson-core</artifactId> 170 <version>2.2.2</version> 171 </dependency> 172 <dependency> 173 <groupId>com.fasterxml.jackson.core</groupId> 174 <artifactId>jackson-databind</artifactId> 175 <version>2.2.2</version> 176 </dependency> 177 178 </dependencies> 179 180 <build> 181 <plugins> 182 <plugin> 183 <artifactId>maven-eclipse-plugin</artifactId> 184 <version>2.9</version> 185 <configuration> 186 <additionalProjectnatures> 187 <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 188 </additionalProjectnatures> 189 <additionalBuildcommands> 190 <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 191 </additionalBuildcommands> 192 <downloadSources>true</downloadSources> 193 <downloadJavadocs>true</downloadJavadocs> 194 </configuration> 195 </plugin> 196 197 <plugin> 198 <groupId>org.apache.maven.plugins</groupId> 199 <artifactId>maven-compiler-plugin</artifactId> 200 <version>2.5.1</version> 201 <configuration> 202 <source>1.7</source> 203 <target>1.7</target> 204 <compilerArgument>-Xlint:all</compilerArgument> 205 <showWarnings>true</showWarnings> 206 <showDeprecation>true</showDeprecation> 207 </configuration> 208 </plugin> 209 210 <plugin> 211 <groupId>org.codehaus.mojo</groupId> 212 <artifactId>exec-maven-plugin</artifactId> 213 <version>1.2.1</version> 214 <configuration> 215 <mainClass>org.test.int1.Main</mainClass> 216 </configuration> 217 </plugin> 218 </plugins> 219 </build> 220 </project>
相關資源文件:
SqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!-- 定義別名 --> 8 <typeAliases> 9 <!-- 批量別名定義 10 name:指定包名,將包下邊的所有pojo定義別名 ,別名為類名(首字母大寫或小寫都行) 11 --> 12 <package name="com.mybatis.bean"/> 13 </typeAliases> 14 15 </configuration>
applicationContext-dao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> 7 <!-- 數據源 --> 8 <property name="dataSource" ref="dataSource" /> 9 <!-- 配置SqlMapConfig.xml --> 10 <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> 11 </bean> 12 13 <!-- 14 使用mapper 批量掃描器掃描mapper介面 15 不需要id,規則:mapper.xml 與 mapper.java 同名並且在同一個包下 16 掃描出來的mapper會自動向spring容器中註冊,bean的id就是類名(首字母小寫) 17 --> 18 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 19 <!-- 配置會話工廠 --> 20 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> 21 <!-- 22 掃描包的路徑 23 如果有多個mapper的包,那就用半形的 ',' 來隔開 24 --> 25 <property name="basePackage" value="com.lee.ssm.dao.mapper"></property> 26 </bean> 27 28 </beans>
applicationContext-service.xml(如果喜歡使用註釋來配置,這個文件可以不用)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- 用戶管理 --> 7 <!-- <bean id="userService" class="com.lee.ssm.service.impl.UserServiceImpl" /> --> 8 </beans>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"