首先创建一个动态web项目:
然后右键点击webapp文件夹复制项目路径,在tomcat的server.xml里进行配置项目
webapp下面新建index.jsp
然后
开始集成spring+springmvc:
首先导入21个包:
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: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
">
<!-- 组件扫描-- >
<context:component-scan base-package="com.xxx.ssme" />
<import resource="classpath:applicationContext-mvc.xml" />
</beans>
applicationContext-mvc.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
">
<!-- 开启spring对springmvc的注解支持 -->
<mvc:annotation-driven />
<!-- 静态资源被拦截:原因是在tocmat的web.xml里面有一个专门处理静态的资源的servlet,它配置的url-pattern也是/ ,造成图片等静态资源不能成功被访问,所以要进行放行-->
<!-- 对于静态资源(图片,css,js)进行放行 -->
<mvc:default-servlet-handler />
<!-- 设置视图路径的前后缀,该配置可以让我们写视图路径的时候更简单。 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀: jsp在当前工程文件夹的路径 -->
<property name="prefix" value="/WEB-INF/views/" />
<!--后缀:扩展名 -->
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<!-- spring el写法:5MB -->
<value>#{1024*1024*5}</value>
</property>
</bean>
</beans>
web.xml:
<?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>ssme</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 解决post请求的中文问题 有一个过滤器 -->
<filter>
<filter-name>characterEncoding</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置核心处理 DispatcherServlet ,这里的spring容器是通过springmvc的核心控制器来启动的,spring启动之后,加载mvc的配置文件-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
启动项目,能正常启动,这里spring+springmvc就集成好了
然后
集成MyBatis环境:
导入相关的9个jar包(核心包,依赖包,数据库连接包):
框架集成核心
如果你的项目中,用到了Spring框架,那么其他框架主要就是和Spring集成!!
和Spring集成的顺序
1.把当前框架的核心类,交给Spring管理
2.如果框架有事务,那么事务也要统一交给Spring管理
Spring配置文件
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: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
">
<!-- 使用注解要进行扫描 -->
<context:component-scan base-package="cn.xxx.ssme" />
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- jdbc.properties->dataSource->sqlSessionFactory->mapper(dao)->service->controller(action) -->
<!-- 数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 依赖注入连接池需要的属性 -->
<!-- property name="是BasicDataSource的set方法,本质属性" -->
<!-- property value="是jdbc.properties配置文件的key" -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="150" /><!-- 这些参数最好写到jdbc.properties中 -->
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="5" />
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="30" />
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10" />
<!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
<property name="maxWait" value="1000" />
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
</bean>
<!-- SSJ集成时候需要entityManagerFactory
SSM集成时候需要sqlSessionFactory
集成jpa的时候需要一个EntityManagerFactory对象 ->
需要一个FactoryBean来完成功能 需要用到SqlSessionFactoryBean
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用datasource -->
<property name="dataSource" ref="dataSource" />
<!-- 读取核心配置文件(写sql的xml文件) -->
<property name="mapperLocations" value="classpath:cn/xxx/ssme/mapper/*Mapper.xml" />
<!-- 配置别名 -->
<!-- 配置mybatis 类型别名 -->
<!-- <property name="typeAliasesPackage"> -->
<!-- <value> -->
<!-- cn.xxx.ssme.domain -->
<!-- cn.xxx.ssme.query -->
<!-- </value> -->
<!-- </property> -->
</bean>
<!-- MapperFactoryBean:对应到我们的Mapper (Mapper接口) -->
<!-- 引用sqlSessoinFactory -->
<!-- 指向对应的接口 -->
<!-- 一劳永逸(不要有id) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.xxx.ssme.mapper" />
</bean>
<!-- 引入 mvc的配置 -->
<import resource="classpath:plugins/applicationContext-mvc.xml" />
</beans>
(除了一劳永逸的办法,还有一个办法是Mapperj接口一个一个地配,不采用这种方式:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="mapperInterface" value="cn.xxx.ssme.mapper.UserMapper" />
........
</bean>)
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssme
jdbc.username=root
jdbc.password=admin
applicationContext-mvc.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
">
<!-- 只要配置了<url-pattern>/</url-pattern>,就必需配置下面两个 -->
<!-- 支持访问静态资源 -->
<mvc:default-servlet-handler />
<!-- 支持MVC的注解 -->
<mvc:annotation-driven />
<!-- 配置视图解析器:InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" /> <!-- 配置前缀 -->
<property name="suffix" value=".jsp" /> <!-- 配置后缀 -->
</bean>
<!-- 如果你要完成文件上传,需要配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<!-- spring el写法:5MB -->
<value>#{1024*1024*5}</value>
</property>
</bean>
</beans>
这里spring+springmvc+mybatis就集成完成了
接下来进行测试:
1.新建数据库ssme,新建表t_user,插入两条数据:
2.写controller层,service层,mapper层(及配置文件)进行测试,(注意:我测试为了方便没用使用service层):
package cn.xxx.ssme.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xxx.ssme.mapper.UserMapper;
UserController :
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/user/list")
public String list(Model model){
System.out.println("9999************");
model.addAttribute("user", userMapper.getOne(1L));
model.addAttribute("users", userMapper.getAll());
return "list";
}
}
UserMapper :
package cn.xxx.ssme.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import cn.xxx.ssme.domain.User;
public interface UserMapper {
@Select("select * from t_user")//这种注解方式不用写sql到xml,不建议这样使用
List<User> getAll();
User getOne(Long id);
}
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="cn.xxx.ssme.mapper.UserMapper">
<select id="getOne" parameterType="long" resultType="cn.xxx.ssme.domain.User">
select *
from t_user where id = #{id}
</select>
</mapper>
list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${user}</br>
${users}
</body>
</html>
代码下载:https://download.csdn.net/download/qq_33526760/11274684
基于SSM架构的CRM系统(二):项目改进&BaseMapper&BaseService&集成easyui : https://blog.csdn.net/qq_33526760/article/details/94606749