1、环境要求以及JAR包导入
环境要求
- jdk1.8
- maven3.6.1
- tomcat9
- mysql8
jar导入
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
其他设置
<!--2.资源过滤以及其他配置-->
<build>
<!--1,资源过滤-->
<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>
<!--相关插件导入 -->
<!--设置编译器,以及字节码的版本-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2、Dao层整合
步骤
-
在resources目录下创建连接数据库的外部配置文件db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/stx?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root
-
在resources目录下创建mybatis主配置文件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"> <configuration> <typeAliases> <package name="com.stx.pojo"/> </typeAliases> <mappers> <package name="com.stx.mapper"/> </mappers> </configuration>
-
在resources目录下创建mybatis,mapper映射文件存放文件夹
<?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"> <mapper namespace="com.lehu.dao.UserMapper"> <select id="getAll" resultType="T_user"> select * from t_user </select> </mapper>
-
在resources目录下创建spring-dao.xml配置文件来整合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 https://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:db.properties"/> <!--2.创建数据库连接池对象--> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3.获取SqlSessionFactoryBean对象-> <bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:/mybatis/UserMapper.xml"/> </bean> <!--4.1.方法1:配置dao/mapper接口扫描包,动态的实现Dao接口注入到spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="configurer"> <!--注入SqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--扫描dao/mapper包--> <property name="basePackage" value="com.stx.dao"/> </bean> <!--4.2.方法2.使用SqlSessionTemplate--> <!-- <bean class="org.mybatis.spring.SqlSessionTemplate" name="SqlSession">--> <!-- <constructor-arg ref="sqlSessionFactory"/>--> <!-- </bean>--> <!--注册使用SqlSessionTemplate完成相关操作的对象--> <!-- <bean class="..." id="mapper">--> <!-- <property name="sessionTemplate" ref="SqlSession"/>--> <!-- </bean>--> </beans>
在com.stx.pojo中创建对应实体类
3、Service层整合
步骤
-
在com.stx.service分别编写UserService接口以及对应的实现类
-
接口
package com.stx.service; import com.stx.pojo.User; import java.util.List; @Service public interface UserService { List<User> getAll(); }
-
实现类
package com.stx.service; import com.stx.pojo.User; import java.util.List; @Service public class UserServiceImp implements UserService { @Override public List<User> getAll() { return null; } }
-
-
在resources目录下编写spring-service.xml文件整合service层,将Service层对象交给spring容器托管
<?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"> <context:component-scan base-package="com.stx.service"/> </beans>
-
使用注解的方式将service层接口交给容器托管,需要在service层接口/类上加上@Service注解,接口实现类使用@Autowired注解引用Dao层对象
package com.stx.service; import com.stx.dao.UserMapper; import com.stx.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImp implements UserService { @Autowired UserMapper userMapper; @Override public List<User> getAll() { return userMapper.getAll(); } }
注意
-
dao层引入对象创建失败问题
六月 17, 2020 4:56:10 下午 org.springframework.context.support.AbstractApplicationContext refresh 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImp': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.stx.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这是因为service层对象使用@Autowired注解引用dao层对象时,无法在spring-service.xml找到dao层对象实例,只需要将spring-dao.xml和spring-service关联起来即可,在spring-service.xml中使用<import resource="spring-dao.xml"/>引入spring-dao.xml的所有配置信息。
4、Controller层整合
步骤
-
创建spring-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: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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--1.导入mvc独用的注解驱动,不用导入普通的<context:annotation-config/>--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <!--设置使用Jackson方式来转换json格式,使用前提是必须导入Jackson这个依赖--> <!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">--> <!-- <property name="objectMapper">--> <!-- <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">--> <!-- <property name="failOnEmptyBeans" value="false"/>--> <!-- </bean>--> <!-- </property>--> <!-- </bean>--> </mvc:message-converters> </mvc:annotation-driven> <!--2.对静态的资源进行过滤--> <mvc:default-servlet-handler/> <!--3.扫描进行注解配置装配的包--> <context:component-scan base-package="com.stx.controller"/> <!--4.导入视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!--配置文件类型解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxInMemorySize" value="10485760"/> </bean> </beans>
-
将所有的spring配置文件整合到applicaitonContex.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-dao.xml"/> <import resource="spring-service.xml"/> <import resource="spring-mvc.xml"/> </beans>
-
在web.xml中配置前端控制器DispatcherServlet
<?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"> <!--0.配置applicationContext上下文加载监听器--> <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> <!--1.配置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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--2.设置过滤器:解决乱码问题--> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置Session过期时间--> <session-config> <session-timeout>14</session-timeout> </session-config> </web-app>
-
编写相关Controller实现类
package com.stx.controller; import com.stx.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Test01 { @Autowired UserService userService; @RequestMapping("/hi") String h1() { return "hello 双体系"; } @RequestMapping("/getusers") String getUsers() { return userService.getAll().toString(); } }
- 配置tomcat
- 运行测试
注意
-
必须在spring-mvc.xml配置文件中加入以下内容,因为spring默认的消息转换器的默认编码标准是ISO-8859-1,该编码是西欧编码,无法对中文编码
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> </mvc:message-converters> </mvc:annotation-driven>
-
关于在IDEA中jar包无法导入到web项目中问题解决
自行百度