SSM基本流程
tomcat配置
mysql配置
maven配置
intellij idea配置
一. 新建工程
1.选择maven中的模板创建, 一般选择maven-archetype-webapp
2.这一步随便起个名就行
3.这一步是配置maven设置, 添加的字段是阻止每次maven的自更新
4.起项目名, 然后就等待maven的构建项目过程, 第一次的话会稍微久一点, 之后就会比较快了.
二. 建包
1.上面的步骤完成之后会是下面这样.
2. 首先先在main文件夹下建立新的文件夹java, 然后右键选择java设置为源代码文件夹.
3. 之后就在java文件夹下建包,建包之后会是这样
三. 设置maven
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lzhr</groupId>
<artifactId>ssm_setup</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm_setup Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.6.RELEASE</spring.version>
<spring-data.version>1.11.0.RELEASE</spring-data.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</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>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-dao -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-dao</artifactId>
<version>2.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SSMSample</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
-Spring配置
创建applicationContext.xml
-
在resource文件夹中创建xml, 流程如图:
-
配置xml, 现阶段就一句话, 具体配置如图:
<context:component-scan base-package="com.lzhr"/>
是自动扫描包及子包中的组件, 这样就不需要配置<bean>
标签, 直接使用注解就可以了.
-mybatis配置
1.创建jdbc.properties配置文件
在resource文件夹中创建空白File, 后缀为jdbc.properties, 内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stu?useSSL=true
username=root
password=lzhr
#定义初始连接数
initialSize=0
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
如图:
2.使用generator生成mapper和xml文件
- 创建generatorConfig.xml配置文件
需要填的内容如下:
classPathEntry
: 驱动包路径
jdbcConnection
: 数据配置
javaModelGenerator
: 表对象的模型设置
sqlMapGenerator
: mapper的xml文件设置
javaClientGenerator
: mapper的接口文件设置
table
: 表设置(可以设置多个表)
具体配置如下:
<generatorConfiguration>
<!-- !!!! Driver Class Path !!!! -->
<!-- !!!! 设置MySQL驱动路径 !!!! -->
<classPathEntry location="/Users/lizhongren1/maven/LocalWarehouse
/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"/>
<context id="context" targetRuntime="MyBatis3">
<!-- !!!! 是否生成注释, 一般都是false, 自动生成的注释完全不能看, 还耽误git同步 !!!! -->
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="false"/>
</commentGenerator>
<!-- !!!! Database Configurations !!!! -->
<!-- !!!! 配置数据库数据 !!!! -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/stu?useSSL=true"
userId="root" password="lzhr"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<!-- !!!! 设置生成的JavaBean所在的包 !!!! -->
<javaModelGenerator targetPackage="com.lzhr.domain"
targetProject="./src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<!-- !!!! 配置Mapper的XML配置文件位置, 也可以设置到resource文件夹 !!!! -->
<sqlMapGenerator targetPackage="com.lzhr.mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<!-- !!!! 配置Mapper接口文件位置 !!!! -->
<javaClientGenerator targetPackage="com.lzhr.mapper"
targetProject="./src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- !!!! Table Configurations !!!! -->
<!-- !!!! 配置MySQL下的表设置 !!!! -->
<table tableName="student"
enableCountByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
- 创建执行类文件.
public class MybatisTest {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
MybatisTest generatorSqlmap = new MybatisTest();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
执行main方法, 结构如下图
3. spring和batis整合
在applicationContext中配置Mybatis
设置说明:
propertyConfigurer
: 读取数据库设置信息
dataSource
: 配置连接池
sqlSessionFactory
: 配置session工厂, 此步节省了mybatis本身应该有的xml配置文件
org.mybatis.spring.mapper.MapperScannerConfigurer
: 自动扫描mapper接口文件
transactionManager
: 事务管理
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<!--<property name="maxActive" value="${maxActive}"></property>-->
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<!--<property name="maxWait" value="${maxWait}"></property>-->
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lzhr.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
4. 完成后的项目结构
如图:
-springMVC配置
- 创建xxx-servlet.xml调度文件
- 配置web.xml
- 创建controller类
- 创建jsp文件
- 部署配置
1.创建xxx-servlet.xml文件
同样在resources文件夹中创建, 命名规范一般是xxx-servlet.xml.
说明:
mvc:annotation-driven
: 注解驱动
<context:component-scan base-package="com.lzhr"/>
: 自动扫描组件包
mvc:default-servlet-handler
: 处理servlet资源, servlet在找不到页面的时候会去找静态的内容。
jspViewResolver
: 视图解析器基本配置, 例如下面的例子就是识别:/WEB-INF/pages/*.jsp
基本配置如下:
<?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:mvc="http://www.springframework.org/schema/mvc"
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/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">
<mvc:annotation-driven/>
<context:component-scan base-package="com.lzhr"/>
<mvc:default-servlet-handler/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2. 配置web.xml
找到在WEB-INF文件夹下的web.xml. 这里本来应该配置各种servlet. 有了springmvc可以取代一大堆重复的配置标签.
- 更新web-app.
- 配置context-param: 这里主要配置spring的xml配置文件的地址:
- 配置监听器: 这里很重要, 不然会匹配不到mapper等接口文件,导致spring的注入失败.
- 配置servlet和servlet-mapping: 配置的结果就是把所有的servlet都交给springmvc来处理.
- 配置filter: 可以解决乱码问题.
详细配置如下:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<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>AServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SSM-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SpringEncoding</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>SpringEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3. 创建Controller类
在controller包中创建controller类, 负责处理servlet.
代码如下, 含义是处理"/"的URL, 显示index.jsp页面:
@Controller
public class MainController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String frontPage(){
return "index";
}
}
4. 在WEB-INF文件夹中创建jsp文件
可以自己定义目录归纳整理jsp