java主流技术栈SSM+springboot学习:项目o2o
第一步创建项目,搭架子。
- 使用idea,create new project->maven->groupID:com.chaoxi;artifactID->o2o
导入基本依赖,后续根据需求会继续添加依赖:
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
配置静态资源过滤,此处暂时这样配置:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
根据数据分析,在MySQL中创建对应的数据表单
CREATE TABLE `tb_area` (
`area_id` int NOT NULL AUTO_INCREMENT,
`area_name` varchar(200) NOT NULL,
`priority` int NOT NULL DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `UK_AREA` (`area_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
CREATE TABLE `tb_head_line` (
`line_id` int NOT NULL AUTO_INCREMENT,
`line_name` varchar(1000) DEFAULT NULL,
`line_link` varchar(2000) NOT NULL,
`line_img` varchar(2000) NOT NULL,
`priority` int DEFAULT NULL,
`enable_status` int NOT NULL DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
PRIMARY KEY (`line_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `tb_local_auth` (
`local_auth_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`username` varchar(128) NOT NULL,
`password` varchar(128) NOT NULL,
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
PRIMARY KEY (`local_auth_id`),
UNIQUE KEY `uk_local_profile` (`username`),
KEY `fk_localauth_profile` (`user_id`),
CONSTRAINT `fk_localauth_profile` FOREIGN KEY (`user_id`) REFERENCES `tb_person_info` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `tb_person_info` (
`user_id` int NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`profile_img` varchar(1024) DEFAULT NULL,
`email` varchar(1024) DEFAULT NULL,
`gender` varchar(2) DEFAULT NULL,
`enable_status` int NOT NULL DEFAULT '0' COMMENT '0:禁止使用本商城,1:允许使用本商城',
`user_type` int NOT NULL DEFAULT '1' COMMENT '1:顾客,2:店家,3:超级管理员',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
CREATE TABLE `tb_product` (
`product_id` int NOT NULL AUTO_INCREMENT,
`product_name` varchar(100) NOT NULL,
`product_desc` varchar(2000) DEFAULT NULL,
`img_addr` varchar(2000) DEFAULT '',
`normal_price` varchar(100) DEFAULT NULL,
`promotion_price` varchar(100) DEFAULT NULL,
`priority` int NOT NULL DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
`enable_status` int NOT NULL DEFAULT '0',
`product_category_id` int DEFAULT NULL,
`shop_id` int NOT NULL DEFAULT '0',
PRIMARY KEY (`product_id`),
KEY `fk_product_procate` (`product_category_id`),
KEY `fk_product_shop` (`shop_id`),
CONSTRAINT `fk_product_procate` FOREIGN KEY (`product_category_id`) REFERENCES `tb_product_category` (`product_category_id`),
CONSTRAINT `fk_product_shop` FOREIGN KEY (`shop_id`) REFERENCES `tb_shop` (`shop_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `tb_product_category` (
`product_category_id` int NOT NULL AUTO_INCREMENT,
`product_category_name` varchar(100) NOT NULL,
`priority` int DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`shop_id` int NOT NULL DEFAULT '0',
PRIMARY KEY (`product_category_id`),
KEY `fk_procate_shop` (`shop_id`),
CONSTRAINT `fk_procate_shop` FOREIGN KEY (`shop_id`) REFERENCES `tb_shop` (`shop_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `tb_product_img` (
`product_img_id` int NOT NULL AUTO_INCREMENT,
`img_addr` varchar(2000) NOT NULL,
`img_desc` varchar(2000) DEFAULT NULL,
`priority` int DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`product_id` int DEFAULT NULL,
PRIMARY KEY (`product_img_id`),
KEY `fk_proimg_product` (`product_id`),
CONSTRAINT `fk_proimg_product` FOREIGN KEY (`product_id`) REFERENCES `tb_product` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `tb_shop` (
`shop_id` int NOT NULL AUTO_INCREMENT,
`owner_id` int NOT NULL COMMENT '店铺创建人',
`area_id` int DEFAULT NULL,
`shop_category_id` int DEFAULT NULL,
`shop_name` varchar(256) NOT NULL,
`shop_desc` varchar(1024) DEFAULT NULL,
`shop_addr` varchar(200) DEFAULT NULL,
`phone` varchar(128) DEFAULT NULL,
`shop_img` varchar(1024) DEFAULT NULL,
`priority` int DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
`enable_status` int NOT NULL DEFAULT '0',
`advice` varchar(255) DEFAULT NULL,
PRIMARY KEY (`shop_id`),
KEY `fk_shop_area` (`area_id`),
KEY `fk_shop_profile` (`owner_id`),
KEY `fk_shop_shopcate` (`shop_category_id`),
CONSTRAINT `fk_shop_area` FOREIGN KEY (`area_id`) REFERENCES `tb_area` (`area_id`),
CONSTRAINT `fk_shop_profile` FOREIGN KEY (`owner_id`) REFERENCES `tb_person_info` (`user_id`),
CONSTRAINT `fk_shop_shopcate` FOREIGN KEY (`shop_category_id`) REFERENCES `tb_shop_category` (`shop_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8
CREATE TABLE `tb_shop_category` (
`shop_category_id` int NOT NULL AUTO_INCREMENT,
`shop_category_name` varchar(100) NOT NULL DEFAULT '',
`shop_category_desc` varchar(1000) DEFAULT '',
`shop_category_img` varchar(2000) DEFAULT NULL,
`priority` int NOT NULL DEFAULT '0',
`create_time` datetime DEFAULT NULL,
`last_edit_time` datetime DEFAULT NULL,
`parent_id` int DEFAULT NULL,
PRIMARY KEY (`shop_category_id`),
KEY `fk_shop_category_self` (`parent_id`),
CONSTRAINT `fk_shop_category_self` FOREIGN KEY (`parent_id`) REFERENCES `tb_shop_category` (`shop_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
CREATE TABLE `tb_wechat_auth` (
`wechat_auth_id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`open_id` varchar(1024) NOT NULL,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`wechat_auth_id`),
UNIQUE KEY `open_id` (`open_id`),
KEY `fk_wechatauth_profile` (`user_id`),
CONSTRAINT `fk_wechatauth_profile` FOREIGN KEY (`user_id`) REFERENCES `tb_person_info` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
数据库中生成的表单:
第二步,搭建三层架构。配置三层xml文件
database.properties
我使用的是Mysql8.0+:
jdbc.driver=com.mysql.jdbc.Driver
#如果使用MySQL8.0+,需要增加一个时区的配置
jdbc.url=jdbc:mysql://localhost:3306/o2o?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=697394
mybatis-config.xml
<?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>
<!--使用jdbc的getGeneratedKeys获取数据自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列标签替换列别名。默认为true-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰命名装换-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.chaoxi.pojo"/>
</typeAliases>
<!--<mappers>
<mapper resource="com/chaoxi/dao/AreaDao.xml"/>
</mappers>-->
</configuration>
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置整合mybatis -->
<!-- 1.关联数据库文件 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半自动化操作 不能自动连接
c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描pojo包,使用别名-->
<property name="typeAliasesPackage" value="com.chaoxi.pojo"/>
<!--扫描sql配置文件,mapper需要的xml文件,这里暂时不配置,将mapper.xml文件注入mybaits-config中-->
<!--<property name="mapperLocations" value="classpath:mapper/"/>-->
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
<!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.chaoxi.dao"/>
</bean>
</beans>
spring-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:tx="http://www.springframework.org/schema/tx" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描service相关的bean/扫描service下所有使用使用注解的类型 -->
<context:component-scan base-package="com.chaoxi.service" />
<!--配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置基于注解的声明式事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置SpringMVC -->
<!-- 1.开启SpringMVC注解驱动 -->
<mvc:annotation-driven />
<!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js,gif,png
(2)允许使用"/"做整体映射
-->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 显示ViewResolver视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
</bean>
<!-- 4.扫描web相关的bean -->
<context:component-scan base-package="com.chaoxi.controller"/>
<!--5.文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!--1024*1024*20=20M-->
<property name="maxUploadSize" value="20971520"/>
<property name="maxInMemorySize" value="20971520"/>
</bean>
</beans>
三层文件整合: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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
logback日志配置:logback.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!--定义参数常量-->
<!--TRACE<DEBUG<INFO<WARN<ERROR-->
<!--logger.trace("msg") logger.debug...-->
<property name="log.level" value="debug"/>
<property name="log.maxHistory" value="30"/>
<property name="log.filePath" value="${catalina.base}/logs/webapps"/>
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread]%-5level%logger{50}-%msg%n"/>
<!--控制台输出-->
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!--DEBUG-->
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<!--历史保存最大数量-->
<maxHistory>${log.maxHistory}</maxHistory>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--INFO-->
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<!--历史保存最大数量-->
<maxHistory>${log.maxHistory}</maxHistory>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--ERROR-->
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<!--历史保存最大数量-->
<maxHistory>${log.maxHistory}</maxHistory>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="com.chaoxi" level="${log.level}" additivity="true">
<appender-ref ref="debugAppender"/>
<appender-ref ref="infoAppender"/>
<appender-ref ref="errorAppender"/>
</logger>
<root level="info">
<appender-ref ref="consoleAppender"/>
</root>
</configuration>
文件夹建立:项目总框架
总结,以上就基本搭建好了SSM框架。万变不离其中!
下一把编写,dao层逻辑,做好dao层与数据库的连接和映射。