SSM项目配置(全齐)
1.maven的坐标
<properties>
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>1.7
</maven.compiler.source>
<maven.compiler.target>1.7
</maven.compiler.target>
<spring.version>5.0.2.RELEASE
</spring.version>
<slf4j.version>1.6.6
</slf4j.version>
<log4j.version>1.2.12
</log4j.version>
<mysql.version>5.1.6
</mysql.version>
<mybatis.version>3.4.5
</mybatis.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-web
</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-test
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-tx
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-jdbc
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-test
</artifactId>
<version>${spring.version}
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>${mysql.version}
</version>
</dependency>
<dependency>
<groupId>javax.servlet
</groupId>
<artifactId>javax.servlet-api
</artifactId>
<version>3.1.0
</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp
</groupId>
<artifactId>jsp-api
</artifactId>
<version>2.0
</version>
</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>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis-spring
</artifactId>
<version>1.3.0
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis
</artifactId>
<version>${mybatis.version}
</version>
</dependency>
<dependency>
<groupId>c3p0
</groupId>
<artifactId>c3p0
</artifactId>
<version>0.9.1.2
</version>
<type>jar
</type>
<scope>compile
</scope>
</dependency>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.11
</version>
<scope>test
</scope>
</dependency>
</dependencies>
2.web.xml
<!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>
<display-name>Archetype Created Web Application
</display-name>
<context-param>
<param-name>contextConfigLocation
</param-name>
<param-value>
classpath*:applicationContext.xml
</param-value>
</context-param>
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter
</filter-name>
<url-pattern>/*
</url-pattern>
</filter-mapping>
<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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet
</servlet-name>
<url-pattern>/
</url-pattern>
</servlet-mapping>
</web-app>
3.applicationcontext.xml(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"
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:component-scan base-package="com.chiguozi.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssmdemo"/>
<property name="user" value="chiguozi"/>
<property name="password" value="123456"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.chiguozi.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.chiguozi.service.Impl.*ServiceImpl.*(..))"/>
</aop:config>
</beans>
4.springmvc.xml的配置(springmvc)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.chiguozi">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="internalResourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:annotation-driven/>
</beans>
5.ssm整合
controller层
package com
.chiguozi
.controller
;
import com
.chiguozi
.domain
.User
;
import com
.chiguozi
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.ui
.Model
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import java
.util
.List
;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService
;
@RequestMapping("/findAll")
public String
findAll(Model model
){
List
<User> userList
= userService
.findAll();
model
.addAttribute("list",userList
);
System
.out
.println("controller执行");
return "list";
}
@RequestMapping("/saveUser")
public String
saveUser(User user
){
userService
.addUser(user
);
return "list";
}
}
service层
package com
.chiguozi
.service
.Impl
;
import com
.chiguozi
.domain
.User
;
import com
.chiguozi
.service
.UserService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Service
;
import com
.chiguozi
.dao
.Userdao
;
import java
.util
.List
;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private Userdao userdao
;
@Override
public void addUser(User user
)
{
userdao
.saveUser(user
);
System
.out
.println("保存了"+user
);
}
@Override
public void delUser(User user
) {
}
@Override
public void updateUser(User user
) {
}
@Override
public User
getUserbyName(String name
) {
System
.out
.println("查询了"+name
);
return null
;
}
@Override
public List
<User> findAll() {
System
.out
.println("查询所有");
return userdao
.findAll();
}
}
dao层
package com
.chiguozi
.dao
;
import com
.chiguozi
.domain
.User
;
import org
.apache
.ibatis
.annotations
.Insert
;
import org
.apache
.ibatis
.annotations
.Select
;
import org
.springframework
.stereotype
.Repository
;
import java
.util
.List
;
@Repository
public interface Userdao {
@Select("select * from user")
public List
<User> findAll();
@Insert("insert into user(username,userpassword) values(#{username},#{userpassword})")
public void saveUser(User user
);
}
jsp
<%--
Created by IntelliJ IDEA.
User: 13710795181
Date: 2020/9/2
Time: 14:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="user/findAll">测试</a>
<form action="user/saveUser">
用户名<input type="text" name="username">
密码<input type="text" name="userpassword">
<input type="submit">
</form>
</body>
</html>