SSM整合基本架构

编程

一. 项目准备

01-需求

02-数据库脚本

create database ssm;

create table account(

id int primary key auto_increment,

name varchar(100),

money double(7,2),

);

INSERT INTO account(NAME,money)VALUES("小雪",1000);

INSERT INTO account(NAME,money)VALUES("丽丽",1000);

03-项目结构

04-Maven依赖

pom.xml文件中添加依赖

  <properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.9</maven.compiler.source>

<maven.compiler.target>1.9</maven.compiler.target>

<spring.version>5.2.3.RELEASE</spring.version>

<slf4j.version>1.6.6</slf4j.version>

<log4j.version>1.2.12</log4j.version>

<mybatis.version>3.4.5</mybatis.version>

<mysql.version>5.1.6</mysql.version>

</properties>

<dependencies>

<!--【Junit】-->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>compile</scope>

</dependency>

<!--【Spring】-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<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-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-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<!--【MySql】-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.version}</version>

</dependency>

<!--【Servlet】-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<!--【JSP】-->

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version>

<scope>provided</scope>

</dependency>

<!--【JSTL】-->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<!--【Log】-->

<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>

<!--【Mybatis】-->

<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>

<!--【c3p0】-->

<dependency>

<groupId>c3p0</groupId>

<artifactId>c3p0</artifactId>

<version>0.9.1.2</version>

<type>jar</type>

<scope>compile</scope>

</dependency>

</dependencies>

二.Java源代码

01-domain-实体类

Account实体类

package cn.lpl666.domain;

import java.io.Serializable;

public class Account implements Serializable {

private int id;

private String name;

private double money;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getMoney() {

return money;

}

public void setMoney(double money) {

this.money = money;

}

@Override

public String toString() {

return "Account{" +

"id=" + id +

", name="" + name + """ +

", money=" + money +

"}";

}

}

02-dao-持久层

IAccountDao接口

package cn.lpl666.dao;

import cn.lpl666.domain.Account;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("accountDao")

public interface IAccountDao {

/**

* 查询所有账户信息

* @return

*/

List<Account> findAll();

/**

* 添加账户

* @param account

*/

void saveAccount(Account account);

/**

* 查询账户

* @param id

* @return

*/

Account findOne(int id);

/**

* 更新账户

* @param account

*/

void update(Account account);

}

03-Service-服务层

IAccountService接口

package cn.lpl666.service;

import cn.lpl666.domain.Account;

import java.util.List;

public interface IAccountService {

/**

* 查询所有账户信息

* @return

*/

List<Account> findAll();

/**

* 添加账户

* @param account

*/

void saveAccount(Account account);

/**

* 转账

* @param oneId

* @param twoId

*/

void transfer(int oneId,int twoId,double money);

}

AccountServiceImpl实现类

package cn.lpl666.service.impl;

import cn.lpl666.dao.IAccountDao;

import cn.lpl666.domain.Account;

import cn.lpl666.service.IAccountService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("accountService")

public class AccountServiceImpl implements IAccountService {

@Autowired

private IAccountDao accountDao = null;

@Override

public List<Account> findAll() {

System.out.println("查询方法执行了");

List<Account> list = accountDao.findAll();

return list;

}

@Override

public void saveAccount(Account account) {

accountDao.saveAccount(account);

}

@Override

public void transfer(int oneId, int twoId,double money) {

Account one = accountDao.findOne(oneId);

Account two=accountDao.findOne(twoId);

one.setMoney(one.getMoney()-money);

two.setMoney(two.getMoney() + money);

accountDao.update(one);

accountDao.update(two);

}

}

04-controller-控制器

AccountController

package cn.lpl666.controller;

import cn.lpl666.domain.Account;

import cn.lpl666.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 java.util.List;

@Controller

@RequestMapping("account")

public class AccountController {

@Autowired

private IAccountService accountService = null;

/**

* 查询所有账户,跳转到用户列表页面(list.jsp),并显示数据

* @param model

* @return

*/

@RequestMapping("findAll")

public String findAll(Model model){

System.out.println("执行了查询用户列表");

List<Account> list = accountService.findAll();

model.addAttribute("list",list);

return "list";

}

/**

* 添加账户,接收用户的参数,添加成功后,转发查询用户列表

* @param account

* @param request

* @return

*/

@RequestMapping("save")

public String save(Account account, HttpServletRequest request){

accountService.saveAccount(account);

System.out.println("保存成功");

return "forward:/account/findAll";

}

/**

* 转账,转账成功后,转发查询用户列表

* @param oneId

* @param twoId

* @param money

* @return

*/

@RequestMapping("transfer")

public String transfer(int oneId,int twoId,double money){

accountService.transfer(oneId,twoId,money);

System.out.println("转账成功!");

return "forward:/account/findAll";

}

}

三.Resources配置资源

01-MySQL连接信息配置

配置文件:jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm

jdbc.username=root

jdbc.password=root

02-日志记录配置

配置文件: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=info, 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} %-6r [%15.15t] %-5p %30.30c %x - %m

# 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} %-6r [%15.15t] %-5p %30.30c %x - %m

03-SpringMVC配置

配置文件: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"

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">

<!--配置spring创建容器时要扫描的包,只扫描controller控制器-->

<context:component-scan base-package="cn.lpl666.controller"></context:component-scan>

<!--配置视图解析器-->

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--路径-->

<property name="prefix" value="/WEB-INF/pages/"></property>

<!--文件-->

<property name="suffix" value=".jsp"></property>

</bean>

<!-- 设置静态资源不过滤 -->

<mvc:resources location="/css/" mapping="/css/**"/>

<mvc:resources location="/images/" mapping="/images/**"/>

<mvc:resources location="/js/" mapping="/js/**"/>

<!--<mvc:default-servlet-handler/>-->

<!--配置SpringMvc开启Mvc注解的支持-->

<mvc:annotation-driven enable-matrix-variables="true"/>

</beans>

04-SSM整合配置

配置文件: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">

<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架

去管理 -->

<context:component-scan base-package="cn.lpl666">

<!-- 配置要忽略的注解 -->

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<!--【Mybatis整合】-->

<!-- 加载数据库连接信息配置文件 -->

<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置 MyBatis 的 Session 工厂 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!--Mybatis子配置返回值别名配置-->

<property name="typeAliasesPackage" value="cn.lpl666.domain"/>

<!-- 数据库连接池 -->

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 配置数据源 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driver}"></property>

<property name="jdbcUrl" value="${jdbc.url}"></property>

<property name="user" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property>

</bean>

<!-- 配置 Mapper 扫描器 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="cn.lpl666.dao"/>

</bean>

<!--【配置事务】-->

<!--配置事务管理器-->

<bean id="transferManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!--配置事务通知-->

<tx:advice id="txAdivce" transaction-manager="transferManger">

<!--配置事务属性-->

<tx:attributes>

<!--propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。-->

<tx:method name="*" propagation="REQUIRED" read-only="false"/>

<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>

</tx:attributes>

</tx:advice>

<!--配置AOP-->

<aop:config>

<!--配置管理包的表达式-->

<aop:pointcut id="servicePath" expression="execution(* cn.lpl666.service.impl.*.*(..))"></aop:pointcut>

<!--建立切入点表达式和事务通知的对应关系-->

<aop:advisor advice-ref="txAdivce" pointcut-ref="servicePath"></aop:advisor>

</aop:config>

</beans>

05-Mybatis操作Sql配置

配置包:cn.lpl666.dao

配置项:IAccountDao.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.lpl666.dao.IAccountDao">

<!--查询所有账户列表-->

<select id="findAll" resultType="Account">

SELECT * FROM account

</select>

<!--保存账户-->

<update id="saveAccount" parameterType="Account">

INSERT INTO account(NAME,money)VALUES(#{name},#{money})

</update>

<!--根据id查询账户-->

<select id="findOne" parameterType="int" resultType="Account">

select * from account where id=#{id}

</select>

<!--更新账户-->

<update id="update" parameterType="Account">

UPDATE account SET name=#{name},money=#{money} WHERE id=#{id}

</update>

</mapper>

四.Web

01-目录结构

02-服务器启动后相关配置

配置文件:WEB-INF目录下的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>

<!--【配置过滤器解决中文乱码问题】-->

<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>

<!--【配置核心控制器】-->

<servlet>

<servlet-name>dispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--配置servlet初始化参数,调用springMvc配置文件,创建spring容器-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<!--请求项目中任何路径都会经过DispatcherServlet核心控制器-->

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- 【配置 spring 提供的监听器,用于启动服务时加载容器 。

该间监听器只能加载 WEB-INF 目录中名称为 applicationContext.xml 的配置文件】 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<!-- 手动指定 spring 配置文件位置 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

</web-app>

03-首页操作JSP页面

页面文件:index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<title>Title</title>

</head>

<body>

<h2>查询账户列表</h2>

<p><a href="account/findAll">findAll</a></p>

<h2>添加账户</h2>

<form action="account/save" method="post">

<p>用户名:<input type="text" name="name"></p>

<p>金额:<input type="text" name="money"></p>

<p><input type="submit" value="提交"></p>

</form>

<h2>转账</h2>

<form action="account/transfer" method="post">

<p>转账人id:<input type="text" name="oneId"></p>

<p>收账人id:<input type="text" name="twoId"></p>

<p>转账金额:<input type="text" name="money"></p>

<p><input type="submit" value="提交"></p>

</form>

</body>

</html>

04-展示账户列表页面

<%@ 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>

<h2>账户列表</h2>

<%--遍历对象--%>

<c:forEach items="${list}" var="value">

账户ID号:${value.id}<br>

账户姓名:${value.name}<br>

账户金额:${value.money}<br>

<hr>

</c:forEach>

</body>

</html>

五.SSM基本架构代码下载

SSM整合基本架构

以上是 SSM整合基本架构 的全部内容, 来源链接: utcz.com/z/513878.html

回到顶部