SSM框架整合
一、基础环境搭建
环境要求
- IDEA
- MySQL 5.5
- Tomcat 9
- Maven 3.6.1
数据库设计
库的创建
create database ssm
表的创建
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `realname` varchar(50) NOT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
数据的插入
INSERT INTO `user` VALUES ('1', '老张', '999', '张三', '23'); INSERT INTO `user` VALUES ('2', '里斯', '26436', '李四', '40'); INSERT INTO `user` VALUES ('3', '老王', '355', '王科达', '30'); INSERT INTO `user` VALUES ('4', '老七', 'gds', '洪七公', '78'); INSERT INTO `user` VALUES ('5', '朱重八', '1636', '朱元璋', '45');
后台
核心jar包依赖:
1 <!-- Spring、SpringMVC --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-webmvc</artifactId> 5 <version>5.0.2.RELEASE</version> 6 </dependency> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-jdbc</artifactId> 10 <version>5.0.2.RELEASE</version> 11 </dependency> 12 <!-- mybatis、mybatis适配spring包 --> 13 <dependency> 14 <groupId>org.mybatis</groupId> 15 <artifactId>mybatis</artifactId> 16 <version>3.5.2</version> 17 </dependency> 18 <dependency> 19 <groupId>org.mybatis</groupId> 20 <artifactId>mybatis-spring</artifactId> 21 <version>2.0.6</version> 22 </dependency> 23 <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> 24 <!-- 数据源 c3p0、mysql驱动 --> 25 <dependency> 26 <groupId>com.mchange</groupId> 27 <artifactId>c3p0</artifactId> 28 <version>0.9.5.5</version> 29 </dependency> 30 <dependency> 31 <groupId>mysql</groupId> 32 <artifactId>mysql-connector-java</artifactId> 33 <version>5.1.47</version> 34 </dependency>
扩展jar包依赖:
1 <!-- JSTL、servlet-api、jsp-api、junit --> 2 <dependency> 3 <groupId>javax.servlet</groupId> 4 <artifactId>jstl</artifactId> 5 <version>1.2</version> 6 </dependency> 7 <dependency> 8 <groupId>taglibs</groupId> 9 <artifactId>standard</artifactId> 10 <version>1.1.2</version> 11 </dependency> 12 <dependency> 13 <groupId>javax.servlet</groupId> 14 <artifactId>servlet-api</artifactId> 15 <version>2.3</version> 16 </dependency> 17 <dependency> 18 <groupId>javax.servlet</groupId> 19 <artifactId>jsp-api</artifactId> 20 <version>2.0</version> 21 </dependency> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>4.12</version> 26 </dependency>
前端
引入semantic-ui前端框架,前提是去github上把semantic-ui的源码给download到本地
1 <link rel="stylesheet" type="text/css" href="semantic/dist/semantic.min.css">
2 <script
3 src="https://code.jquery.com/jquery-3.1.1.min.js"
4 integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
5 crossorigin="anonymous"></script>
6 <script src="semantic/dist/semantic.min.js"></script>
二、配置文件的编写
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 7 <!-- 配置DispatcherServlet --> 8 <servlet> 9 <servlet-name>DispatcherServlet</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:applicationContext.xml</param-value> 14 </init-param> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>DispatcherServlet</servlet-name> 19 <url-pattern>/</url-pattern> 20 </servlet-mapping> 21 22 <!-- 配置字符编码过滤器 --> 23 <filter> 24 <filter-name>CharacterEncodingFilter</filter-name> 25 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 26 <init-param> 27 <param-name>encoding</param-name> 28 <param-value>UTF-8</param-value> 29 </init-param> 30 </filter> 31 <filter-mapping> 32 <filter-name>CharacterEncodingFilter</filter-name> 33 <url-pattern>/*</url-pattern> 34 </filter-mapping> 35 </web-app>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123
mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <!-- configuration核心配置文件 --> 6 <configuration> 7 8 <!-- 别名 --> 9 <typeAliases> 10 <package name="com.lzp.pojo"/> 11 </typeAliases> 12 13 <!-- 注册Mapper --> 14 <mappers> 15 <package name="com.lzp.mapper"/> 16 </mappers> 17 18 </configuration>
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.lzp.mapper.UserMapper"> 6 7 <!-- 这取User中的属性时,可以直接使用实体类的属性名,不需要通过实体类的类名去点出来 --> 8 <insert id="addUser" parameterType="User"> 9 insert into user (username, password, realname, age) 10 values (#{userName}, #{password}, #{realName}, #{age}) 11 </insert> 12 13 <delete id="deleteUser" parameterType="_int"> 14 delete 15 from user 16 where id = #{id} 17 </delete> 18 19 <update id="updateUser" parameterType="User"> 20 update user 21 set 22 username = #{userName}, 23 password = #{password}, 24 realname = #{realName}, 25 age = #{age} 26 where id = #{id} 27 </update> 28 29 <select id="queryUserById" parameterType="_int" resultType="User"> 30 select * 31 from user 32 where id = #{id}; 33 </select> 34 35 <select id="queryUserList" resultType="User"> 36 select * 37 from user 38 </select> 39 </mapper>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <import resource="spring-dao.xml"/> 8 <import resource="spring-service.xml"/> 9 <import resource="spring-mvc.xml"/> 10 11 </beans>
spring-dao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!-- 引入db.properties文件 --> 11 <context:property-placeholder location="classpath:db.properties"/> 12 13 <!-- 14 配置c3p0数据源,这里就是让Spring来控制事务,所以才在Spring配置文件中 15 来配置数据源 16 --> 17 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 18 <property name="driverClass" value="${jdbc.driver}"/> 19 <property name="jdbcUrl" value="${jdbc.url}"/> 20 <property name="user" value="${jdbc.username}"/> 21 <property name="password" value="${jdbc.password}"/> 22 <!-- c3p0私有属性 --> 23 <property name="initialPoolSize" value="10"/> 24 <property name="maxIdleTime" value="30"/> 25 <property name="maxPoolSize" value="100"/> 26 <property name="minPoolSize" value="10"/> 27 </bean> 28 29 <!-- 配置sqlSessionFactory --> 30 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 31 <!-- 将其注入数据源 --> 32 <property name="dataSource" ref="dataSource"/> 33 <!-- 绑定mybatis核心配置文件 --> 34 <property name="configLocation" value="classpath:mybatis-config.xml"/> 35 </bean> 36 37 <!-- 配置自动扫描mapper中的接口,并通过反射创建其实现类,最后注入Spring容器中 --> 38 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 39 <property name="basePackage" value="com.lzp.mapper"/> 40 <!-- 将其注入sqlSessionFactory --> 41 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 42 </bean> 43 44 </beans>
spring-service.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!-- 配置自动扫描service包 --> 11 <context:component-scan base-package="com.lzp.service"/> 12 13 <!-- 配置注解驱动支持 --> 14 <context:annotation-config/> 15 16 <!-- 配置事务管理 --> 17 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 18 <property name="dataSource" ref="dataSource"/> 19 </bean> 20 21 22 </beans>
spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <!-- 配置自动扫描controller包 --> 14 <context:component-scan base-package="com.lzp.controller"/> 15 16 <!-- 配置静态资源过滤问题 --> 17 <mvc:default-servlet-handler/> 18 19 <!-- 配置SpringMVC的注解支持 --> 20 <mvc:annotation-driven/> 21 22 <!-- 配置视图解析器 --> 23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 24 <!-- 前缀 --> 25 <property name="prefix" value="/WEB-INF/jsp/"/> 26 <!-- 后缀 --> 27 <property name="suffix" value=".jsp"/> 28 </bean> 29 30 </beans>