一、搭建环境
1、创立数据库表和表构造
create table account(
id INT identity(1,1) primary key,
name varchar(20),
[money] DECIMAL
)
2、创立 maven 的工程 SSM,在 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/m…d”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mingqi</groupId>
<artifactId>SSM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SSM Maven Webapp</name>
<!– FIXME change it to the project’s website –>
<url>http://www.example.com</url>
<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>
<!-- spring -->
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</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>
<!-- log start -->
<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>
<!-- log end -->
<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>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>SSM</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、部署 ssm_web 的我的项目,只有把 ssm_web 我的项目加java 培训入到 tomcat 服务器中即可
4、编写实体类
package com.mingqi.domain;
import java.io.Serializable;
public class Account implements Serializable {
public void setId(Integer id) {this.id = id;}
public void setName(String name) {this.name = name;}
public void setMoney(Double money) {this.money = money;}
public Integer getId() {return id;}
public String getName() {return name;}
public Double getMoney() {return money;}
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
5、编写 dao 接口
package com.mingqi.dao;
import com.mingqi.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface IAccountDao {
@Insert(value="insert into account (name,money) values (#{name},#{money})")
public void saveAccount(Account account);
@Select("select * from account")
public List<Account> findAll();
}6、编写 service 接口和实现类
package com.mingqi.service;
import com.mingqi.domain.Account;
import java.util.List;
public interface IAccountService {
public void saveAccount(Account account);
public List<Account> findAll();
}package com.mingqi.service.impl;
import com.mingqi.dao.IAccountDao;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(“accountService”)
public class AccountImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public void saveAccount(Account account) {accountDao.saveAccount(account);
}
@Override
public List<Account> findAll() {System.out.println("业务层:查问所有账户...");
List<Account> accounts=accountDao.findAll();
return accounts;
//return null;
}
}
二、Spring 框架代码的编写
-
搭建和测试 Spring 的开发环境
在 webapp 我的项目中创立 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.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…d”>
<!– 开启注解扫描,要扫描的是 service 和 dao 层的注解,要疏忽 web 层注解,因为 web 层让 SpringMVC 框架
去治理 –>
<context:component-scan base-package=”com.mingqi”><!-- 配置要疏忽的注解 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!– 配置 C3P0 的连接池对象 –>
<bean id=”dataSource”class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/> <property name="username" value="sa"/> <property name="password" value="123sa"/>
</bean>
<!– 配置 SqlSession 的工厂 –>
<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”><property name="dataSource" ref="dataSource" />
</bean>
<!– 配置扫描 dao 的包 –>
<bean id=”mapperScanner” class=”org.mybatis.spring.mapper.MapperScannerConfigurer”><property name="basePackage" value="com.mingqi.dao"/>
</bean>
<!– 配置 Spring 框架申明式事务管理 –>
<!– 配置事务管理器 –>
<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=”*” isolation=”DEFAULT”/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref=”txAdvice” pointcut=”execution(public com.mingqi.service..ServiceImpl.(..))”/>
</aop:config>
</beans>
三、Spring 整合 SpringMVC 框架
1、搭建和测试 SpringMVC 的开发环境 -
在 web.xml 中配置 DispatcherServlet 前端控制器
<!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>
<!– 配置 Spring 的监听器,默认只加载 WEB-INF 目录下的 applicationContext.xml 配置文件 –>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!– 设置配置文件的门路 –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name/>
<param-value/>
</context-param>
<!– 配置前端控制器 –>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载 springmvc.xml 配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 启动服务器,创立该 servlet-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、测试 SpringMVC 的框架搭建是否胜利
- 编写 index.jsp 和 list.jsp 编写,超链接
<%@ page contentType=”text/html;charset=UTF-8″ language=”java” %>
<html>
<body>
<h2> 查问所有!</h2>
查问所有
测试查问
<h3> 测试包 </h3>
<form action=”account/save” method=”post”>
姓名:<input type="text" name="name" /><br/>
金额:<input type="text" name="money" /><br/>
<input type="submit" value="保留"/><br/>
</form>
</body>
</html><%@ page contentType=”text/html;charset=UTF-8″ language=”java” isELIgnored=”false” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3> 查问所有的帐户 </h3>
<c:forEach items=”${list}” var=”account”>
${account.name}
</c:forEach>
</body>
</html>
2、创立 AccountController 类,编写办法,进行测试
package com.mingqi.controller;
import com.mingqi.domain.Account;
import com.mingqi.service.IAccountService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping(“/account”)
public class AccountController {
@Autowired
private IAccountService accountService;
/**
* 查问所有数据
* @return
*/
@RequestMapping("/findAll")
public String findAll(Model model)
{System.out.println("体现层查问所有");
accountService.findAll();
List<Account> list = accountService.findAll();
model.addAttribute("list",list);
return "list";
}
@RequestMapping("/save")
public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException
{accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}
}
配置 tomcat,启动我的项目,进行测试