SSM整合基本架构:第二篇
一. 项目准备
1.1-需求
实现对订单的查询(根据id查询)和更改
1.2-数据库脚本
CREATE DATABASE IF NOT EXISTS ssm;DROP TABLE IF EXISTS `items`;
CREATE TABLE `items` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) DEFAULT NULL,
`price` FLOAT(10,0) DEFAULT NULL,
`pic` VARCHAR(40) DEFAULT NULL,
`createTime` DATETIME DEFAULT NULL,
`detail` VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
INSERT INTO `items` VALUES ("1", "华为P30手机", "1000", NULL, "2018-03-13 09:29:30", "国货杠杠的");
SELECT * FROM items;
UPDATE items SET NAME="华为Mate30",price=9999,pic=NULL,createTime="2018-03-13 09:29:30",detail="杠杠的" WHERE id=1;
1.3-项目结构
1.4-Maven依赖
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>cn.lpl666</groupId>
<artifactId>MavenPro_SSM_Basic</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>MavenPro_SSM_Basic 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.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
<springmvc.version>5.0.5.RELEASE</springmvc.version>
<myatis.version>3.4.5</myatis.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<!--【锁定jar版本】-->
<dependencyManagement>
<dependencies>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${myatis.version}</version>
</dependency>
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</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>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</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>
</dependencies>
</dependencyManagement>
<!--【引入依赖】-->
<dependencies>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!--springMVC核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--【Log】-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<!--【build】-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
二. Java源代码
2.1-pojo-实体类
package cn.lpl666.pojo;import java.util.Date;
public class Items {
private int id;
private String name;
private float price;
private String pic;
private String createTime;
private String detail;
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name="" + name + """ +
", price=" + price +
", pic="" + pic + """ +
", createTime=" + createTime +
", detail="" + detail + """ +
"}";
}
}
2.2-dao-持久层
接口
package cn.lpl666.dao;import cn.lpl666.pojo.Items;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemsMapper {
/**
* 根据id查找指定的订单项
* @param id
* @return
*/
Items findOne(int id);
/**
* 更新订单
* @param items
*/
void update(Items items);
}
2.3-service-服务层
接口
package cn.lpl666.service;import cn.lpl666.pojo.Items;
public interface ItemsService {
/**
* 根据id查找指定的订单项
* @param id
* @return
*/
Items findOne(int id);
/**
* 更新订单
* @param items
*/
void update(Items items);
}
实现类
package cn.lpl666.service.impl;import cn.lpl666.dao.ItemsMapper;
import cn.lpl666.pojo.Items;
import cn.lpl666.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional // 事务注解
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper = null;
@Override
public Items findOne(int id) {
return itemsMapper.findOne(id);
}
@Override
public void update(Items items) {
itemsMapper.update(items);
}
}
2.4-controller-控制器
package cn.lpl666.controller;import cn.lpl666.pojo.Items;
import cn.lpl666.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("showItems/{id}")
public String findOne(@PathVariable int id, Model model){
Items item = itemsService.findOne(id);
model.addAttribute("item",item);
return "item";
}
}
三. Resources配置资源
3.1-MySql数据库连接配置
配置文件:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/ssm
jdbc.username=root
jdbc.password=root
3.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=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
3.3-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>
3.4-Spring核心配置
Spring整合dao层配置
配置文件:applicationContext-dao.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:jdbc.properties" />
<!--【配置数据源信息,使用druid连接池】-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--【配置Spring整合Mybatis框架的SqlSessionFactoryBean】-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--Mybatis子配置返回值别名配置-->
<property name="typeAliasesPackage" value="cn.lpl666.pojo"/>
<!--数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--【配置 Mapper 扫描器,用于产生代理对象】 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.lpl666.dao"/>
</bean>
</beans>
Spring整合service层配置
配置文件:applicationContext-service.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-->
<context:component-scan base-package="cn.lpl666.service"/>
<!--配置事务管理器-->
<bean id="transferManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解驱动-->
<tx:annotation-driven transaction-manager="transferManger" />
</beans>
3.5-Mybatis操作Sql配置
配置包:cn.lpl666.dao
配置项:ItemsMapper.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.ItemsMapper">
<!--根据id查询订单-->
<select id="findOne" parameterType="int" resultType="Items">
select * from items where id=#{id}
</select>
<!--更新订单-->
<update id="update" parameterType="Items">
UPDATE items SET NAME=#{name},price=#{price},pic=#{pic},createTime=#{createTime},detail=#{detail} WHERE id=#{id};
</update>
</mapper>
四. test测试
测试文件:MybatisTest.java , 测试业务层代码是否有效
import cn.lpl666.pojo.Items;import cn.lpl666.service.ItemsService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-dao.xml","classpath:applicationContext-service.xml"})
public class MybatisTest {
@Autowired
private ItemsService itemsService;
@Test
public void test01(){
Items items = itemsService.findOne(1);
System.out.println(items);
}
@Test
public void test02(){
Items items = itemsService.findOne(1);
items.setPrice(99999);
itemsService.update(items);
}
}
五. Web
5.1-服务器启动后相关配置
配置文件: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>
<!-- 手动指定 spring 配置文件位置 -->
<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>
<!-- 【配置 spring 提供的监听器,用于启动服务时加载容器 。
该间监听器只能加载 WEB-INF 目录中名称为 applicationContext.xml 的配置文件】 -->
<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>
<!--配置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>
</web-app>
5.2-首页JSP页面
页面文件:index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html>
<head>
<title>Title</title>
</head>
<body>
<a href="items/showItems/1">查询id为1的订单</a>
</body>
</html>
5.3-订单项展示页
页面文件:items.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html>
<head>
<title>Title</title>
</head>
<body>
<h1>订单项</h1>
${item}
</body>
</html>
5.4-SSM整合基本架构源码下载
https://gitee.com/lpl666/SSM.git
code/SSM2
以上是 SSM整合基本架构:第二篇 的全部内容, 来源链接: utcz.com/z/514019.html