一、框架搭建前期环境准备
jdk8.0下载地址:/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlapache-maven-3.5.0-alpha-1下载地址:/dist/maven/maven-3/3.5.0/binaries/中安装,然后要进行仓库和阿里镜像的配置参考/weixin_42035152/article/details/80035373;apache-tomcat-7.0.90;下载地址:/download-70.cgi中标题为Core:下载,,前两个为mac系统的下载;mysql-5.6.41-macos10.13-x86_64,下载地址:/downloads/mysql/5.7.html,可以选择版本进行下载这里安装完毕以后,都需要配置环境变量,这里就不再进行说明了,可以参考其他的博客进行配置;开发工具去官网下载:/idea/download/#section=mac二、搭建框架
打开intellij idea.出现如下的界面,这时候我们先对Maven的地址进行配置一下吧,点击Configure我们选择preference;这时候会直接跳转到Maven配置界面如下图所示,这是我们主要配置三个地方,Maven home directory请选择你maven的安装路径,User setting file这是我们前面提到的仓库和阿里镜像的配置文件,Local repository是配置仓库的地址配置完毕我们选择apply然后Ok;
然后点击下一步,我们看到下面的界面 ,GroupID是项目组织唯一的标识符, 比如我的项目叫test001 那么GroupID应该是 com.lixiaoming.test001 域名.公司名.项目名,ArtifactID就是项目的唯一的标识符, 一般是 项目名-xxx 比如test001-model 继续下一步,如下图显示项目名称和你的项目本地地址。
点击完成,项目结构算是搭建起来了
三、搭建目录结构
四.文件配置内容
首先我们要在pox.xml中引进我们需要的jar包,在地址:/里面搜素自己需要的jar包,因为要进行下载会有点慢,需要耐心等待一下,这时我们配置SSM其他的文件。
<!-- /artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.13</version></dependency><!-- /artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!-- /artifact/javax.servlet.jsp/javax.servlet.jsp-api --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.2.1</version><scope>provided</scope></dependency><!-- /artifact/com.alibaba/fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><!-- /artifact/taglibs/standard --><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><!-- /artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.1.2</version></dependency><!-- /artifact/commons-dbcp/commons-dbcp --><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><!-- /artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!-- /artifact/org.springframework/spring-aop --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-aspects --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-beans --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-context-support --><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-expression --><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-tx --><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.3.RELEASE</version></dependency><!-- /artifact/org.mybatis/mybatis-spring --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.2</version></dependency><!-- /artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- /artifact/aopalliance/aopalliance --><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><!-- /artifact/org.aspectj/aspectjweaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>RELEASE</version><scope>compile</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency>
我们引进的jar包可以在坐下侧External Librarres栏进行查看如下图,如果pox.xml部分文件显示红色,可能是jar 包没有下载到仓库中,可能没有从仓库中引入你的jar 包,这是你需要进行刷新。
applicationContext.xml 主要是对Spring和mybatis进行整合,对数据库连接。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-2.5.xsd /schema/aop /schema/aop/spring-aop-2.5.xsd /schema/tx /schema/tx/spring-tx-2.5.xsd /schema/context/schema/context/spring-context.xsd"><!--扫描service,dao包中所有的类,自动注入到IOC容器--><context:component-scan base-package="cn.service"/><!--开启自动注解的配置 mapper中的东西 @Componet 不需要实现类 | Spring MVC也是基于Annotation实现的配置 --><context:annotation-config /><!--获取proterties的位置 为数据源做准备的--><context:property-placeholder location="classpath:dataBase.properties"/><!-- JNDI获取数据源(使用dbcp连接池) --><!--使用的单例模式 不会有数据源的冲突--><bean id="dataSource" class="mons.dbcp.BasicDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 配置mybatis SqlSessionFactoryBean --><bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入数据库连接池 --><property name="dataSource" ref="dataSource"/><!-- 扫描sql配置文件:mapper需要的xml文件 --><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--扫描所有的Mapper.xml配置文件--> <!-- 配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 给出需要扫描Dao接口包 --><property name="basePackage" value="cn.mapper"/></bean></beans>
jdbc.properties 数据库连接的配置
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCjdbc.username=rootjdbc.password=root
mybatis-cfg.xml mybtisde设置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-////DTD Config 3.0//EN""/dtd/mybatis-3-config.dtd"><configuration><settings><!-- 是否启用懒加载 --><setting name="lazyLoadingEnabled" value="false" /></settings><!--<typeAliases><!–这里给实体类取别名,方便在mapper配置文件中使用–><package name="cn.guanpin.bean" /></typeAliases>--></configuration>
log4.properties 日志输出设置
log4j.rootLogger=DEBUG,CONSOLE,file#log4j.rootLogger=ERROR,.smbms.dao=.ibatis=debug .mon.jdbc.SimpleDataSource=debug .mon.jdbc.ScriptRunner=debug .ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug log4j.logger.java.sql.ResultSet=debug .tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug####################################################################################### Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e######################################################################################log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.Threshold=errorlog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n####################################################################################### DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log-09-11######################################################################################log4j.appender.file=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.DatePattern=yyyy-MM-ddlog4j.appender.file.File=log.loglog4j.appender.file.Append=truelog4j.appender.file.Threshold=errorlog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%.opensymphony.xwork2=error
springmvc.xml spring和Springmvc的整合
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:mvc="/schema/mvc"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/context/schema/context/spring-context.xsd/schema/mvc/schema/mvc/spring-mvc.xsd"><!-- 配置处理器映射HandlerMapping --><!-- <bean name="/index.html" class="cn.controller.IndexController"></bean> --><mvc:annotation-driven/><context:component-scan base-package="cn.controller"/><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.StringHttpMessageConverter"/><bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/></mvc:message-converters></mvc:annotation-driven></beans>
web.xml springmvc 配置文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="/2001/XMLSchema-instance"xmlns="/xml/ns/javaee"xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>student</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 核心控制器的配置DispatchServlet --><!--web.xml的作用其实就是启动servlet的时候加载所需要的一些xml文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.js</url-pattern></servlet-mapping><!-- 编码过滤器 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
然后自己建一下视图层
五、连接数据库
输入密码进入数据库,然后创建数据库,如果显示创建的数据库,那么我们继续下一步;
CREATE DATABASE chatRotbot;SHOW DATABASE;
现在我们要在用intellij idea连接数据库,进入设置mysql数据库的页面。
输入下面的命令行,插入数据时,请吧xxxx换成具体数据;
CREATE TABLE `flower` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,`anothername` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,`property` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,`price` float(10, 2) NOT NULL,`production` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;-- ------------------------------ Records of flower-- ----------------------------INSERT INTO `flower` VALUES (****, '****', '****', '****', ****, '****');SET FOREIGN_KEY_CHECKS = 1;
如果觉得《IntelliJ IDEA搭建SSM框架并实现页面登录功能连接数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!