1,pom.xml文件:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
2,配置controller和请求结合:第一步先更改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>ssmas</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ssmas</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring位置</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
3,创建上一步的spring.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">
<!-- ================================================================================-->
<!-- 配置springmvc -->
<!-- 开启springmvc注解模式 -->
<!-- 简化配置:
1.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
2.默认提供一系列功能:数据绑定,数字和日期的format @NumberFormat,@DataTimeFormat,
xml,json默认读写支持.
-->
<!--开启springMVC的注解模式-->
<mvc:annotation-driven/>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 静态资源处理 -->
<mvc:resources mapping="/imgs/**" location="/imgs/"></mvc:resources>
<!-- 扫描bean spring框架用的 可以在这里或者spring-dao文件里配置-->
<context:component-scan base-package="扫描spring注解的包"/>
<!-- spring开启支持注解-->
<context:annotation-config></context:annotation-config>
<!-- 配置jsp 显示ViewResolver -->
<!-- 这里就是为了直接返回字符串index而不是index.html -->
<!-- 方便于编码 这里的意思大体就是找web-inf下的jsp文件下的名字叫index的以jsp结尾的文件 -->
<!-- ///这里有问题用下面的试图处理器//-->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
<!-- <property name="prefix" value="/WEB-INF/jsp/"/>-->
<!-- <property name="suffix" value=".jsp"/>-->
<!-- </bean>-->
<!-- 视图处理器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4,创建好了之后 测试是 使用@Controller和@Autowired等注解方式 判断请求和响应
还有返回值为string的时候 会按照配置的第三步的试视图处理器的路径自动找 注意更改
========================================================================
下面开始Mybatis交给Spring的配置:
1,创建Dao的mapper文件夹 用于查询的sql:
<?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">
<!-- namespace指定和哪个Dao映射器接口对应 -->
<mapper namespace="XXXDao路径">
<!-- 定义SQL语句 根据账号和密码 进行查询-->
<select id="selectCar" resultType="Domain包下的Car路径" >
select * from car where aid = #{aid};
</select>
</mapper>
2,创建好了之后 可以新建 也可以在上面的spring.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">
<!-- 创建DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/数据库名称?useSSL=true"></property>
<property name="username" value="用户名"></property>
<property name="password" >
<value>密码</value>
</property>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:存放sql的mapper文件"></property>
</bean>
<!-- MapperScannerConfigurer 批量生产Mapper 实现类
这里运用组件扫描技术 在这里,只需要指定接口的位置,它可以自动为你的接口生产出对应的实现类
在这里不需要传递sqlSessionFactory是因为他自动把他给指定到了上面-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Dao接口的包"></property>
</bean>
</beans>
完成ssm配置
gitee码云下载地址:
https://gitee.com/aster123/ssm2020.git