- 整合spring系列的框架,核心都是将类的创建和加载交给spring去管理。
- 便于理解,我这里把spring整合的配置文件分开管理。
- 使用工具idea
- 模板地址:https://github.com/zyp-3997/SSM.git
1.Maven坐标导入。
<dependencies>
<!--junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--servlet和jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</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和SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE </version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
静态资源整合问题:
因为maven默认解析的配置文件是在resource目录下,如果我们想在其他目录使用配置文件,需要在pom文件中设置配置如下
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.创建包的目录结构
java目录下主要写我们的代码。
controller包//控制层,负责页面之间的跳转
dao包//持久成,负责和数据的增删改查操作
entity包//实体类,实体类javaBean,一般用于封装数据。
service包//逻辑层,封装dao包中的方法,并且一般在这一层处理事务操作。
resource目录下主要写配置文件,在java/com/zyp/dao/UserMapper.xml中也写入了配置文件,默认idea是不识别的,但是使用了上面的静态资源代码那块配置,就可以识别了。
applicationContext//spring主配置文件
database.properties//配置数据库连接
mybatis-config.xml//mybatsi配置文件
spring-dao.xml//spring整合mybatis配置文件
spring-mvc.xml//springmvc配置文件
spring-service//spring整合service层配置文件
3.spring整合mybatis
1.写mapper映射文件
在里面写sql语句,对数据库的增删改查。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mybatis的主配置文件 -->
<mapper namespace="com.zyp.dao.UserMapper" >
<insert id="add" parameterType="User">
insert into user (userid, username, password)VALUES (#{userid},#{username},#{password});
</insert>
<delete id="delete" parameterType="int">
DELETE from user where userid=#{id};
</delete>
<update id="update" parameterType="User">
update user set
username=#{username},password=#{password}
where userid=#{userid};
</update>
<select id="findById" resultType="User" parameterType="int">
select * from user where userid=#{userId};
</select>
<select id="findAll" resultType="User">
select * from user;
</select>
</mapper>
2.配置mybatis-config.xml文件
里面的核心是注入Mapper映射文件,这些都可以直接交给spring。
<?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="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<!--起别名-->
<package name="com.zyp.entity"/>
</typeAliases>
<mappers>
<package name="com.zyp.dao"></package>
</mappers>
</configuration>
3.配置数据库连接文件database.properties
jdbc.user=root
jdbc.password=3997
jdbc.url=jdbc:mysql://localhost:3306/spring_student?serverTimezone=UTC&useSSL=false
jdbc.driver=com.mysql.cj.jdbc.Driver
4.配置spring-dao.xml文件
- 关联步骤3的数据库连接文件
- 连接池配置这里用的c3p0
- 配置SQLSessionFactory,在里面注入数据源,以及绑定步骤2mybatis的配置文件
- 配置dao接口扫描包
<?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">
<!--1关联数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--2连接池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.user}"/>
<property name="password" value="${jdbc.password}"/>
<!--最大连接数量-->
<property name="maxPoolSize" value="30"/>
<!--最小连接数量-->
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动提交-->
<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"/>
<!--绑定mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--4配置dao接口扫描包,动态的实现了dao接口可以注入到spring容器中-->
<bean id="mappserScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--配置要扫描的dao包-->
<property name="basePackage" value="com.zyp.dao"/>
</bean>
</beans>
4.spring整合springmvc
1.配置web.xml。
这里主要配置了启动服务器时加载spring主配置applicationContext.xml文件
配置SpringMVC的前端控制器
配置字符过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SSH</display-name>
<!-- Spring的配置文件applicationContext.xml在web.xml中加载,Spring IoC容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 设置关于Spring IoC的监听器监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC过滤器-字符集过滤器,解决中文乱码 -->
<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>
<!-- DispatcherServlet前端控制器配置 -->
<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:spring-mvc.xml</param-value>
</init-param>
<!-- 表示容器在启动时立即加载servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.配置SpringMVC的配置文件
- 配置处理映射器和处理适配器
- 配置静态资源过滤
- 给包增加注解扫描功能
- 配置视图解析器
<?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/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1自 动加载 RequestMappingHandlerMapping (处理映射器) 和
RequestMappingHandlerAdapter ( 处 理 适 配 器 ) -->
<mvc:annotation-driven/>
<!--2静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3扫描包:controller-->
<context:component-scan base-package="com.zyp.controller"/>
<!--4视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.配置spring-service配置文件
- 给service包下添加注解扫描
- 提供事务支持
<?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">
<!--1扫描service下的包-->
<context:component-scan base-package="com.zyp.service"/>
<!--2将所有的业务类,注入到spring-->
<!--<bean id="userService" class="com.zyp.service.UserServiceImpl">
<property name="userMapper" ref="userMapper"/>
</bean>-->
<!--3事务配置-->
<bean id="transaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--4 aop事务支持!-->
<!--配置事务通知:-->
<tx:advice id="txAdvice" transaction-manager="transaction">
<!--给哪些方法配置事务-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务注入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.zyp.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
4.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
5.测试
以下是运行结果(没有用前端技术,所以很丑):
点击查询所有
修改和删除后
添加
按ID查询