第一步
- 在pom.xml添加package格式war
<packaging>war</packaging>
- 在pom.xml添加Spring-webmvc依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
-
将工程变成web工程
打开project Structure–>Modules–>Web–>双击下方地址栏,点击确定,创建文件夹–>点击上方+号,生成web.xml文件(注意地址需要修改在src/main/java/webapp/WEB-INF/web.xml)–>apply–>OK
-
创建controller、dao、domain、service包
-
创建ApplicationConfig.xml,扫描除Controller以外的包
<?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.kca.meeting" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
- 创建spring-servlet.xml,扫描Controller包
<?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 https://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.kca.meeting" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
</beans>
- 在web.xml内监听两个配置文件,和乱码过滤
<?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">
<!-- 监听ApplicationConfig.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationConfig.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 监听spring-servlet.xml-->
<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-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 过滤乱码-->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 在pom.xml中添加servlet依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
- 在controller中创建一个测试类,hello
package com.kca.meeting.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class hello {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
- 配置Tomcat
- 防止出错,统一改成8
第一步完成,鼓掌,第一次什么错都没报(。ì _ í。)
第二步
- 在pom.xml添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<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>
- 创建db.properties
- 输入数据库信息
db.username=root
db.password=XXXXXXX
db.url=jdbc:mysql://localhost:3306/meeting?useSSL=false&useUnicode=true&characterEncoding=utf8
- 在applicationConfig.xml文件内配置mybatis
<!-- 配置路径-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="url" value="${db.url}"/>
</bean>
<!-- 配mybatis-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.kca.meeting.domain"/>
<property name="mapperLocations">
<value>
classpath*:com/kca/meeting/dao/*.xml
</value>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="basePackage" value="com.kca.meeting.dao"/>
</bean>
- 存在问题,Mapper的xml与class文件会在一起,在pom.xml添加配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
- 搭建测试框架
6.1 创建department类
6.2 定义对象名
6.3 生成getter and setter方法
6.4 生成toString方法
6.5 创建DepartmentMapper接口
6.6 创建DepartmentService类
6.7 创建DepartmentController类 - 填写测试内容
7.1 Controller
package com.kca.meeting.controller;
import com.kca.meeting.domain.Department;
import com.kca.meeting.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DepartmentController {
@Autowired
DepartmentService departmentService;
@GetMapping("/dep")
public void getDepById(Integer id){
Department dep = departmentService.getDepById(id);
System.out.println(dep.toString());
}
}
7.2 Service
package com.kca.meeting.service;
import com.kca.meeting.dao.DepartmentMapper;
import com.kca.meeting.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DepartmentService {
@Autowired
DepartmentMapper departmentMapper;
public Department getDepById(Integer id) {
return departmentMapper.getDepById(id);
}
}
7.3 Mapper接口
package com.kca.meeting.dao;
import com.kca.meeting.domain.Department;
public interface DepartmentMapper {
Department getDepById(Integer id);
}
7.4 mapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kca.meeting.dao.DepartmentMapper">
<select id="getDepById" resultType="com.kca.meeting.domain.Department">
select * from department where departmentid=#{id};
</select>
</mapper>
- 成功了
- ApplicationConfig.xml配置事务
<!-- 配置事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="add*"/>
<tx:method name="delete*"/>
<tx:method name="update*"/>
<tx:method name="insert*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc1" expression="execution(* com.kca.meeting.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/>
</aop:config>
- 在pom.xml添加aspectj
<!-- 事务配置aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.8</version>
</dependency>
呀吼,成功了
第三步
整合freemarker
- 在pom.xml添加依赖
<!-- freemarker依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
- 创建freemarker-var.properties
root=/
- 在spring-servlet里配置视图解析器
<!-- 配置试图解析器-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:freemarker-var.properties</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="WEB-INF/ftl"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="freemarkerVariables">
<map>
<entry key="root" value="${root}"/>
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">ZH_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">#.####</prop>
</props>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="suffix" value=".ftl"/>
<property name="allowRequestOverride" value="true"/>
<property name="allowSessionOverride" value="true"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="contentType" value="text/html;charset=utf-8"/>
</bean>
- 测试
4.1 在WEB-INF下创建一个ftl文件夹
4.2 创建hello.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>
4.3 controller中修改为
@Controller
public class hello {
@GetMapping("/hello")
public String hello(Model model){
model.addAttribute("hello", "hello java");
return "hello";
}
}
成功,吼吼!