IDEA整合SSM
什么是ssm?
Spring + SpringMVC + Mybatis
1.整合工程
1.创建Maven工程
方式一:使用Web模板
方式二:
1.在main目录下新建目录webapp
2.建立web工程
注意路径问题
3.配置 Tomcat
基本目录结构就搭建完毕
4.Maven导入相关依赖
Spring
SpringMVC
Mybatis
数据库连接池,数据库连接驱动
日志,测试单元
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>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/central</url>
<layout>default</layout>
<!-- 是否开启发布版构件下载 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 是否开启快照版构件下载 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<groupId>com.august.ssm</groupId>
<artifactId>SSM_01_CRUD</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<version.spring>5.3.15</version.spring>
</properties>
<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>
<resource>
<directory>src/main/resources/mapper</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
<dependencies>
<!-- SpringMVC依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${version.spring}</version>
</dependency>
<!-- Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${version.spring}</version>
</dependency>
<!-- spring面向切片-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${version.spring}</version>
</dependency>
<!-- spring单元测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${version.spring}</version>
<scope>test</scope>
</dependency>
<!-- Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- Mybatis-spring适配包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- 数据库连接池,驱动-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- 其他 (servlet,junit)-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
<!-- json处理-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- 日志-->
<!-- <dependency>-->
<!-- <groupId>ch.qos.logback</groupId>-->
<!-- <artifactId>logback-classic</artifactId>-->
<!-- <version>1.2.3</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
</dependencies>
</project>
5.创建MVC模型
bean、dao、service、controller、utils、test
6.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!-- 1.启动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>
<!-- 2.SpringMVC的前端控制器,拦截所有请求-->
<servlet>
<servlet-name>DispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3.字符编码过滤器,一定放在所有过滤器之前-->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 4.Rest风格过滤器 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
注意:web.xml引用到Spring、SpringMVC的xml文件,所以在类路径下创建applicationContext.xml和spring-mvc.xml
7.配置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 https://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.配置扫描包-->
<context:component-scan base-package="com.august" use-default-filters="false">
<!-- 只扫描控制器-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2.两个标准配置-->
<!-- 将SpringMVC不能处理的请求交给Tomcat,动态静态资源都能访问成功-->
<mvc:default-servlet-handler/>
<!-- 注解驱动-->
<mvc:annotation-driven/>
</beans>
8.配置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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Spring的配置文件,只要配置和业余相关的,(数据源,事务控制) -->
<context:component-scan base-package="com.august">
<!--排除controller控制器的包-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2.引入配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->
<property name="initialSize" value="${jdbc.initialSize}"/>
<!--连接池中保留的最大连接数。默认值: 15 -->
<property name="maxActive" value="${jdbc.maxActive}"/>
<!-- 连接池中保留的最小连接数,默认为:3-->
<property name="minIdle" value="${jdbc.minIdle}"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->
<property name="maxWait" value="${jdbc.maxWait}"/>
<!--每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<!-- 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 -->
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
</bean>
<!-- 3.mybatis整合spring-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置Mybatis的全局配置文件位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis的Mapper配置文件位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--配置扫描器, mybatis接口的实现到ioc容器-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描dao下的实现-->
<property name="basePackage" value="com.august.dao"/>
</bean>
<!--4.事物控制配置-->
<bean id="sourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启基于注解或者xml(常用)配置形式的事物-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.august.service..*(..))"/>
<!-- 配置事物增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 配置事物增强,如何切入-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事物方法-->
<tx:method name="*"/>
<!--所有get开头都是事物方法-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
注意:applicationContext.xml引用到jdbc.properties和mybatis-config.xml文件,所以在类路径下创建.
jdbc.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmcrud?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456
jdbc.initialSize=5
jdbc.maxActive=20
jdbc.minIdle=3
jdbc.maxWait=0
jdbc.timeBetweenEvictionRunsMillis=0
jdbc.minEvictableIdleTimeMillis=0
9.配置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">
<!--MyBatis的配置文件-->
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
<typeAliases>
<package name="com.august.bean"/>
</typeAliases>
</configuration>
基本框架搭建结束.
2.编写OPM数据库和Java类关系映射
1.创建SQL
员工类Sql
CREATE TABLE `tab_emp` (
`emp_id` int NOT NULL AUTO_INCREMENT,
`emp_name` varchar(32) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`email` varchar(32) DEFAULT NULL,
`d_id` int DEFAULT NULL,
PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
部门类Sql
CREATE TABLE `tab_dept` (
`dept_id` int NOT NULL AUTO_INCREMENT,
`dept_name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
2.创建对应JavaBean
public class Employees {
private Integer empId;
private String empName;
private String gender;
private String email;
private Integer dId;
private Department dept;
}
public class Department {
private Integer deptId;
private String deptName;
}
3.创建对应的dao接口和对应的SQL映射文件
EmployeesMapper.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.august.dao.EmployeesMapper">
<!-- List<Employees> queryAllEmployees();-->
<select id="queryAllEmployees" resultType="employees">
SELECT
<include refid="empFieldSql"/>
FROM tab_emp
</select>
<!-- List<Employees> queryAllEmployeesWithDepartment();-->
<resultMap id="empResultMap" type="employees">
<id property="empId" column="emp_id"/>
<result property="empName" column="emp_name"/>
<result property="email" column="email"/>
<result property="gender" column="gender"/>
<result property="deptId" column="dept_id"/>
<!-- 其他类的映射使用 association-->
<association property="deptName" javaType="department">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
</association>
</resultMap>
<select id="queryAllEmployeesWithDepartment" resultMap="empResultMap">
SELECT
<include refid="empWithFieldSql"/>
FROM tab_emp e
LEFT JOIN tab_dept d
ON e.d_id = d.dept_id
</select>
<!--Employees queryEmployeesById(Integer id);-->
<select id="queryEmployeesById" resultType="employees">
SELECT
<include refid="empFieldSql"/>
FROM tab_emp where emp_id = #{empId}
</select>
<!-- Employees queryEmployeesByIdWithDepartment(Integer id); -->
<select id="queryEmployeesByIdWithDepartment" resultMap="empResultMap">
SELECT
<include refid="empWithFieldSql"/>
FROM tab_emp e
LEFT JOIN tab_dept d
ON e.d_id = d.dept_id
WHERE e.emp_id =#{empId}
</select>
<!--int deleteEmployeesById(Integer id);-->
<delete id="deleteEmployeesById">
DELETE
FROM tab_emp
WHERE emp_id = #{empId}
</delete>
<!--int addEmployees(Employees emp);-->
<insert id="addEmployees" parameterType="employees" useGeneratedKeys="true" keyProperty="empId">
INSERT INTO tab_emp(emp_name, email, gender, d_id)
VALUES (#{empName}, #{email}, #{gender}, #{dId})
</insert>
<!-- sql重用语句 -->
<sql id="empFieldSql">
emp_id
,emp_name,email,gender,d_id
</sql>
<sql id="empWithFieldSql">
e.emp_id,e.emp_name,e.email,gender,d.dept_id,d.dept_name
</sql>
</mapper>
DepartmentMapper.xml同理
4.搭建前端框架
结束!