项目结构
步骤
1 新建空工程 设置jdk版本和配置本地maven
2 新建maven模块 在pom.xml添加依赖 将打包方式改为war 在src/main添加webapp包 在webapp包下添加WEB-INF,在WEB-INF包下添加web.xml,在test目录下添加resource包
3 添加mybatis配置文件 spring接管mybatis 目前mybatis配置文件里只需配置日志输出
<?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>
<!--设置日志输出语句,显示相应操作的sql语名-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
4 添加数据源配置文件jdbc.properties,添加mapper.xml文件
5 添加spring配置文件application.xml
(1)mapper层配置
a、读取属性文件
b、配置数据源(德鲁伊数据连接池)
c、配置sqlSessionFactoryBean
d、注册mapper.xml文件
(2)service层配置
a、添加包扫描
b、添加事务管理器
c、配置事务切面
d、配置切入点和绑定
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--mapper层-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
<property name="typeAliasesPackage" value="com.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"></property>
</bean>
<!--service层-->
<context:component-scan base-package="com"></context:component-scan>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*select*" read-only="true"/>
<tx:method name="*insert*" propagation="REQUIRED"></tx:method>
<tx:method name="*delete*" propagation="REQUIRED"></tx:method>
<tx:method name="*update*" propagation="REQUIRED"></tx:method>
<tx:method name="*" propagation="SUPPORTS"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.service.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
6 添加springmvc配置文件spring.xml
a、添加包扫描
b、添加注解驱动(ajax发送json)
c、(视图解析器,前后端分离 不需要)
<?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">
<context:component-scan base-package="com.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
7 配置web.xml
a、添加中文编码过滤器
b、注册springmvc框架 (注册前端控制器DispatcherServlet)
c、注册spring框架(注册监听器)
<?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">
<!--添加中文编码过滤器-->
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--注册springmvc框架-->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--注册spring框架 目的是启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
</web-app>
8 编写mapper层
注意点:namespce为接口全限定名用Copy Reference,mapper接口和配置文件放在一目录下
9 编写service层
注意点:添加@Service注解 在Service层用@Autowire注入mapper
10 编写controller层
注意点:
0、添加@CrossOrigin 在服务端支持跨域访问
a、添加@Controller注解 在Controller层用@Autowie注入service
b、在方法添加@ResponseBody注解 返回的数据为json
c、在方法添加@RequestMapping 映射访问路径
d、(用@RestController 方法中全是ajax请求 方法上不用写@ResponseBody)
11 spring集成junit测试
a、在test目录下新建类,在类上添加@RunWith(SpringJUnit4ClassRunner.class),添加@ContextConfiguration加载spring配置文件
遇到的错误
a、不小心把resultType写成resultMap
b、在计算总行数时要用select count(*) from user ,count和(*)不能有空格否则