1. Maven父子模块
1. 1 项目目录
ssm_web模块是maven-web项目,其他父子模块普通的maven项目即可 父模块只负责maven依赖的jar包配置pom.xml,实际功能交给子模块
1.2 父模块的jar包依赖信息配置pom.xml
<?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.duson</ groupId>
< artifactId> ssm</ artifactId>
< packaging> pom</ packaging>
< version> 1.0-SNAPSHOT</ version>
< modules>
< module> ssm_domain</ module>
< module> ssm_dao</ module>
< module> ssm_service</ module>
< module> ssm_utils</ module>
< module> ssm_web</ module>
</ modules>
< properties>
< spring.version> 5.0.2.RELEASE</ spring.version>
< slf4j.version> 1.6.6</ slf4j.version>
< log4j.version> 1.2.12</ log4j.version>
< oracle.version> 10.2.0.4.0</ oracle.version>
< mybatis.version> 3.4.5</ mybatis.version>
< spring.security.version> 5.0.1.RELEASE</ spring.security.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.6.8</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aop</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context-support</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-orm</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-beans</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-core</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-tx</ artifactId>
< version> ${spring.version}</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet.jsp</ groupId>
< artifactId> jsp-api</ artifactId>
< version> 2.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> jstl</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> ${log4j.version}</ version>
</ dependency>
< 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.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> ${mybatis.version}</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.0</ version>
</ dependency>
< dependency>
< groupId> c3p0</ groupId>
< artifactId> c3p0</ artifactId>
< version> 0.9.1.2</ version>
< type> jar</ type>
< scope> compile</ scope>
</ dependency>
< dependency>
< groupId> com.github.pagehelper</ groupId>
< artifactId> pagehelper</ artifactId>
< version> 5.1.2</ version>
</ dependency>
< dependency>
< groupId> org.springframework.security</ groupId>
< artifactId> spring-security-web</ artifactId>
< version> ${spring.security.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework.security</ groupId>
< artifactId> spring-security-config</ artifactId>
< version> ${spring.security.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework.security</ groupId>
< artifactId> spring-security-core</ artifactId>
< version> ${spring.security.version}</ version>
</ dependency>
< dependency>
< groupId> org.springframework.security</ groupId>
< artifactId> spring-security-taglibs</ artifactId>
< version> ${spring.security.version}</ version>
</ dependency>
< dependency>
< groupId> com.oracle</ groupId>
< artifactId> ojdbc14</ artifactId>
< version> ${oracle.version}</ version>
</ dependency>
< dependency>
< groupId> javax.annotation</ groupId>
< artifactId> jsr250-api</ artifactId>
< version> 1.0</ version>
</ dependency>
</ dependencies>
</ project>
1.3 子模块间的相互依赖
子模块间要添加依赖才能访问其他模块的包和类 idea中调用其他模块的类的时,类名会变红,按alt+enter提示选择第一个选项即可自动添加依赖信息到自己模块中的pom.xml中
< dependency>
< groupId> com.duson</ groupId>
< artifactId> ssm_domain</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< scope> compile</ scope>
</ dependency>
< dependency>
< groupId> com.duson</ groupId>
< artifactId> ssm_service</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< scope> compile</ scope>
</ dependency>
2. ssm主配置文件
2.1 配置文件存放位置
ssm所有配置文件都要放到web项目的resources资源目录中
2.2 日志记录log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j. rootCategory= INFO, CONSOLE debug info warn error fatal
log4j. rootCategory= debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j. logger. org. apache. axis. enterprise= FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout .
log4j. appender. CONSOLE= org. apache. log4j. ConsoleAppender
log4j. appender. CONSOLE. layout= org. apache. log4j. PatternLayout
log4j. appender. CONSOLE. layout. ConversionPattern= % d{ ISO8601} % - 6 r [ % 15.15 t] % - 5 p % 30.30 c % x - % m\n
# LOGFILE is set to be a File appender using a PatternLayout .
# log4j. appender. LOGFILE= org. apache. log4j. FileAppender
# log4j. appender. LOGFILE. File= d: \axis. log
# log4j. appender. LOGFILE. Append= true
# log4j. appender. LOGFILE. layout= org. apache. log4j. PatternLayout
# log4j. appender. LOGFILE. layout. ConversionPattern= % d{ ISO8601} % - 6 r [ % 15.15 t] % - 5 p % 30.30 c % x - % m\n
2.3 数据库连接信息db.properties
db. driver= oracle. jdbc. driver. OracleDriver
db. url= jdbc: oracle: thin: @localhost : 1521 : orcl
db. username= ssm
db. password= ssm
2.4 spring mvc配置文件springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: aop= " http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
" >
< context: component-scan base-package = " com.duson.controller" >
</ context: component-scan>
< bean id = " viewResolver"
class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /page/" />
< property name = " suffix" value = " .jsp" />
</ bean>
< mvc: resources location = " /css/" mapping = " /css/**" />
< mvc: resources location = " /img/" mapping = " /img/**" />
< mvc: resources location = " /js/" mapping = " /js/**" />
< mvc: resources location = " /plugin/" mapping = " /plugin/**" />
< mvc: resources location = " /upload/" mapping = " /upload/**" />
< mvc: annotation-driven />
< aop: aspectj-autoproxy proxy-target-class = " true" />
</ beans>
2.5 spring配置文件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"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: tx= " http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: property-placeholder location = " classpath:db.properties" />
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " ${db.driver}" />
< property name = " jdbcUrl" value = " ${db.url}" />
< property name = " user" value = " ${db.username}" />
< property name = " password" value = " ${db.password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " configLocation" value = " classpath:mybatis.xml" />
</ bean>
< bean id = " mapperScanner" class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.duson.dao" />
</ bean>
< 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 = " find*" read-only = " true" />
< tx: method name = " get*" read-only = " true" />
< tx: method name = " query*" read-only = " true" />
< tx: method name = " *" />
</ tx: attributes>
</ tx: advice>
< aop: config >
< aop: pointcut expression = " execution(* com.duson.service.impl.*.*(..))" id = " servicePointcut" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " servicePointcut" />
</ aop: config>
< context: component-scan base-package = " com.duson.service" >
</ context: component-scan>
< context: component-scan base-package = " com.duson.dao" >
</ context: component-scan>
</ beans>
2.6 mybatis配置文件mybatis.xml
原先mybatis配置都放到了spring中了,这个文件可以不要
<?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 = " lazyLoadingEnabled" value = " true" />
< setting name = " aggressiveLazyLoading" value = " false" />
</ settings>
</ configuration>
2.7 web项目配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns = " http://xmlns.jcp.org/xml/ns/javaee"
xsi: schemaLocation= " http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version = " 3.1" >
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:applicationContext*.xml</ param-value>
</ context-param>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< servlet>
< servlet-name> dispatcherServlet</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:springmvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> dispatcherServlet</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< filter>
< filter-name> characterEncodingFilter</ filter-name>
< filter-class> org.springframework.web.filter.CharacterEncodingFilter</ filter-class>
< init-param>
< param-name> encoding</ param-name>
< param-value> UTF-8</ param-value>
</ init-param>
< init-param>
< param-name> forceEncoding</ param-name>
< param-value> true</ param-value>
</ init-param>
</ filter>
< filter-mapping>
< filter-name> characterEncodingFilter</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
</ web-app>
3. 项目例子
3.0 概述
目录结构参考上面1.1 上面ssm配置都不用改,只管添加各层代码即可(因为注解不用改配置文件)
3.1 数据表创建
create table ACCOUNT
(
id NUMBER not null ,
username VARCHAR2( 20 ) ,
age NUMBER
) ;
create sequence SEQ_ACCOUNT
minvalue 1
maxvalue 999999999999
start with 1
increment by 1
cache 20 ;
insert into ACCOUNT ( ID, USERNAME, AGE)
values ( SEQ_ACCOUNT. nextval, '张三' , 17 ) ;
insert into ACCOUNT ( ID, USERNAME, AGE)
values ( SEQ_ACCOUNT. nextval, '李四' , 20 ) ;
insert into ACCOUNT ( ID, USERNAME, AGE)
values ( SEQ_ACCOUNT. nextval, '王五' , 22 ) ;
3.2 实体类Account
package com. duson. domain ;
public class Account {
private Integer id;
private String username;
private Integer age;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getUsername ( ) {
return username;
}
public void setUsername ( String username) {
this . username = username;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
'}' ;
}
}
3.3 数据层IAccountDao
package com. duson. dao ;
import com. duson. domain. Account ;
import org. apache. ibatis. annotations. Insert ;
import org. apache. ibatis. annotations. Select ;
import java. util. List ;
public interface IAccountDao {
@Select ( "select * from account" )
List < Account > findAll ( ) ;
@Insert ( "insert into account values (seq_account.nextval,#{username},#{age})" )
void save ( Account account) ;
}
3.4 业务层IAccountService与AccountServiceImpl
package com. duson. service ;
import com. duson. domain. Account ;
import java. util. List ;
public interface IAccountService {
List < Account > findAll ( ) ;
void save ( Account account) ;
}
package com. duson. service. impl ;
import com. duson. dao. IAccountDao ;
import com. duson. domain. Account ;
import com. duson. service. IAccountService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. util. List ;
@Service
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public List < Account > findAll ( ) {
return accountDao. findAll ( ) ;
}
public void save ( Account account) {
accountDao. save ( account) ;
}
}
3.5 控制器AccountContoller
package com. duson. controller ;
import com. duson. domain. Account ;
import com. duson. service. IAccountService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Controller ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. servlet. ModelAndView ;
import java. util. List ;
@Controller
@RequestMapping ( "account" )
public class AccountController {
@Autowired
private IAccountService accountService;
@RequestMapping ( "list" )
public ModelAndView list ( ) {
List < Account > list= accountService. findAll ( ) ;
return new ModelAndView ( "list" , "accountList" , list) ;
}
@RequestMapping ( "add" )
public String add ( Account account) {
accountService. save ( account) ;
return "redirect:/account/list" ;
}
}
3.6 视图页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<! DOCTYPE html >
< html>
< head>
< base href = " <%=basePath%>" >
< meta charset = " UTF-8" >
< title> </ title>
</ head>
< body>
< form action = " account/add" method = " post" >
账户名:< input name = " username" /> < br>
年龄:< input name = " age" /> < br>
< input type = " submit" value = " 提交" >
</ form>
</ body>
</ html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<! DOCTYPE html >
< html>
< head>
< base href = " <%=basePath%>" >
< meta charset = " UTF-8" >
< title> </ title>
</ head>
< body>
< c: forEach items = " ${accountList}" var = " account" >
${account.id} --- ${account.username} --- ${account.age}< br/>
</ c: forEach>
</ body>
</ html>