java开发需要掌握的最基础知识--SSM框架整合
SSM框架整合
你好!作为一名java开发人员SSM框架是需要掌握的最基础的知识了
spring
轻量级企业框架,是java开发的行业标准
springMVC
spring web MVC,基于spring实现的MVC设计模式框架。
mybatis
当前主流的持久层(ORM)框架,完成JDBCd的封装,持久层的解决方案。
springMVC负责实现MVC设计模式。
Mybatis负责数据持久层交互。
spring管理是springMVC和Mybatis:loc负责springMVC和Mybatis相关对象的创建和依赖注入,
AOP 责事务管理
项目目录结构 (先把目录文件夹建立起来,内容具体文件再创建)
注意:按照目录分别右击 java、resources文件夹选中
目录结构
开发步骤
1.创建Maven Web工程,选择webapp模板,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>com.wgk.SSM</groupId>
<artifactId>com.wgk.SSM</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com.wgk.SSM 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.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.2.3.RELEASE</spring.version>
</properties>
<dependencies>
<!--springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring AOP-->
<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>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!-- Mybatis整合Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- druid 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- jstl jsp标签库-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servletAPI-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- **不建议使用 lombok @Data注解 自动生成getter setter toString 方法 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<!-- 配置maven文件读取目录 默认是 src/main/resources-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
<finalName>com.wgk.SSM</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.web.xml配置开启Spring(监听),SpringMVC(DispatcherServlet),设置字符编码过滤器,加载静态资源
<!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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 拦截器 -->
<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-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
3.配置对应XML文件
数据库相关配置: jdbcConfig.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/wgk?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
jdbc.user=root
jdbc.password=123456
jdbc.initialSize=1
#最小连接池数
jdbc.minIdle=1
#最小连接池数
jdbc.maxActive=20
#获取连接等待的超时时间
jdbc.maxWait=30000
#间隔多久才进行一次检测,检测要关闭的空闲连接,单位毫秒
#jdbc.timeBetweenEvictionRunsMillis=60000
#一个连接池中最小生存的时间,(毫秒)
#jdbc.minEvictableIdleTimeMillis=300000
#配置监控统计拦截的filters
#jdbc.filters=stat
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: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 http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 加载外部文件 jdbc参数配置-->
<context:property-placeholder location="classpath:jdbcConfig.properties"/>
<!-- 数据源 druid连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 配置连接池属性 -->
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.jdbcUrl}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置连接池私有属性 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
</bean>
<!-- sqlSessionFactory对象 ,Spring和MyBatis整合,不需要mybatis的配置映射文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定Mapper.xml-->
<property name="mapperLocations" value="classpath:com/wgk/ssm/mapper/*.xml"/>
<!-- 扫描包 使用别名 -->
<property name="typeAliasesPackage" value="com.wgk.ssm.vo"/>
<!-- Mybatis全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-context.xml"/>
</bean>
<!-- 扫描MyBatis Mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 扫描DAO接口包-->
<property name="basePackage" value="com.wgk.ssm.dao"/>
</bean>
<context:component-scan base-package="com.wgk.ssm.service"/>
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务切入-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 开启基于注解的事物 -->
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.wgk.ssm.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
</beans>
springmvc.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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 注解驱动启动-->
<mvc:annotation-driven/>
<!-- controller扫描器-->
<context:component-scan base-package="com.wgk.ssm.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/views/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
mybatis-context.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 4.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" />
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
</configuration>
4.按照目录创建对应的类以及接口和映射文件
具体文件内容(按照开发流程创建)
目录vo文件夹下创建实体对象:User.java
package com.wgk.ssm.vo;
import lombok.Data;
import lombok.RequiredArgsConstructor;
/**
* @Description:
* @Author: XXX
* @Date: 2020/2/4 3:25
*/
@Data // 此注解为lombok插件,项目运行时自动生成getter setter 方法
@RequiredArgsConstructor //此注解为lombok插件,自动生成有参的构造方法
public class User {
private Long id;
private String name;
private Integer age;
private String password;
}
目录dao文件夹下创建接口:IUserDao.java
package com.wgk.ssm.dao;
import com.wgk.ssm.vo.User;
/**
* @Description:
* @Author: XXX
* @Date: 2020/2/4 20:41
*/
public interface IUserDao {
User findById(Long id);
}
目录mapper文件夹下创建IUserDao的映射文件:UserMapper.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="com.wgk.ssm.dao.IUserDao">
<select id="findById" parameterType="java.lang.Long" resultType="User">
select id,name,age,password from user_t t where t.id = #{id}
</select>
</mapper>
目录service文件夹下创建业务逻辑接口:IUserService.java
package com.wgk.ssm.service;
import com.wgk.ssm.vo.User;
/**
* @Description:
* @Author: XXX
* @Date: 2020/2/4 22:02
*/
public interface IUserService {
User finById(Long id);
}
目录service.impl文件夹下创建实现业务逻辑接口的类:UserService.java
package com.wgk.ssm.service.impl;
import com.wgk.ssm.dao.IUserDao;
import com.wgk.ssm.service.IUserService;
import com.wgk.ssm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Description:
* @Author: XXX
* @Date: 2020/2/4 22:00
*/
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public User finById(Long id) {
User user = this.userDao.findById(id);
return user;
}
}
目录controller文件夹下创建和前端交互的控制层: UserHandler.java
package com.wgk.ssm.controller;
import com.wgk.ssm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description:
* @Author: XXX
* @Date: 2020/2/4 19:54
*/
@Controller // MVC模式,返回值是对象model的显示层
// @RestController // 返回值是以JSON 格式返回
public class UserHandler {
@Autowired
private IUserService userService;
// @GetMapping("/hello")
@RequestMapping("/hello")
public String index() {
System.out.println("index...");
System.out.println(userService.finById(1L));
return "index"; // index 对应试图层的index.jsp
}
}
代码结束-----------------------------------------------------------------------------------------------------------------------------------
配置Tomcat
运行项目
1:是正常启动
2:断电调试模式