mybatis
mybatis 配置文件,其主要配置在spring的配置文件中,spring整合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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 表中带下划线的字段自动转化为驼峰命名 -->
<!-- <setting name="mapUnderscoreToCameCase" value="true"/>-->
</settings>
<plugins>
<!--5.0版本pagehelper -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
spring
spring 配置文件applicationContext.xml,根据单一职责将spring配置文件分为:1、spring整合mybatis配置文件:applicationContext-dao.xml
2、spring本身配置文件:applicationContext.xml
3、spring 整合事务配置文件:applicationContext-tx.xml
spring 整合mybatis
主要配置在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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
配置数据库相关信息
-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="url" value="jdbc:mysql:///biquge?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="zhu980510"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="maxActive" value="100"/>
<property name="minIdle" value="5"/>
<property name="maxWait" value="5000"/>
</bean>
<!--
配置 sqlsessionfactory
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
</bean>
<!--
扫描 mapper
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--
不能放大扫描的范围,必须精确到具体的 mapper
-->
<property name="basePackage" value="com.qianfeng.ssm.mapper"/>
</bean>
</beans>
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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.qianfeng.ssm.service"/>
<!--
平台事务管理器
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--
这个dataSource必须和 sqlsessionfactory 保持一致
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
我们通过注解管理事务
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
spring 整合事务applicationContext-tx.xml
前面已经在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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--
平台事务管理器
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--
这个dataSource必须和 sqlsessionfactory 保持一致
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--
我们通过注解管理事务
-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
springMVC
<!-- 只扫描controller -->
<context:component-scan base-package="com.qianfeng.ssm.controller"/>
<!--配置的视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--或者静态资源交个servlet去处理
<mvc:default-servlet-handler/>
-->
<!--
配置了日期自定义的转换器
-->
<bean id="conversionService2" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- <bean class="com.qianfeng.springmvc.controller.convert.MyDateConvert"/>-->
<ref bean="myDateConvert"/>
</set>
</property>
</bean>
<!--
首页放行
-->
<mvc:resources mapping="index.html" location="/"/>
<mvc:interceptors>
<mvc:interceptor>
<!--拦截路径-->
<mvc:mapping path="/**"/>
<!--<mvc:mapping path="/test/**"/>-->
<!--不需要拦截的路径-->
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/index.html"/>
<!--指定拦截器-->
<ref bean="loginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!--
专门处理文件上传的解析器
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="5120000"></property>
<!-- <property name="maxUploadSizePerFile" value="1000"-->
</bean>
<!--开启SpringMVC注解的支持-->
<mvc:annotation-driven/>
springMVC 相关注解
1、@RestController:加在controller类上面,相当于@ResponseBody+@Controller,表示整个controller里面的方法都加上@ResponseBody注解
2、@RequestBody:加在形参前面,表示前端传过来的数据是json需要将数据进行封装
3、@ResponseBody:响应正文,如果是字符串原样返回,对象需要导入 jackson 依赖包,添加mvc:annotation-driven/ 则默认返回 json 格式
4、@PathVariable:接收请求路径中占位符的值。请求路径 localhost:8080/user/test8/zhangsan,Controller中方法接收@RequestMapping("/test8/{userName}"),形参写法:public void test8(@PathVariable String userName )
ssm整合依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mybatis整合spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--连接mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
<!--Druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.3</version>
</dependency>
<!-- jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- 事务相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!--springmvc相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<!--文件上传下载-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>