1.mapper文件配置
<?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 标识映射文件 -->
<mapper namespace="com.jt.manage.mapper.UserMapper">
<!--查询用户信息 配置别名XXX-->
<select id="findAll" resultType="User">
select * from user
</select>
</mapper>
2.配置spring mvc
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启MVC注解 -->
<mvc:annotation-driven/>
<!--配置视图解析器 内部资源视图解析器
添加前缀和后缀
/WEB-INF/views/userList.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--放行静态资源 -->
<mvc:default-servlet-handler/>
</beans>
3.配置web.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启MVC注解 -->
<mvc:annotation-driven/>
<!--配置视图解析器 内部资源视图解析器
添加前缀和后缀
/WEB-INF/views/userList.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--放行静态资源 -->
<mvc:default-servlet-handler/>
</beans>
4.配置spring配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--1.配置包扫描 -->
<context:component-scan base-package="com.jt"/>
<!--2.引入外部配置文件 -->
<context:property-placeholder
location="classpath:/properties/*.properties"/>
<!--2.管理数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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.配置事务 -->
<tx:annotation-driven/>
<!--3.1 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--3.2 配置事务策略
事务传播属性
propagation="REQUIRED"; 默认值 必须添加事务
propagation="REQUIRES_NEW" 新的事务
propagation="NEVER" 从不添加事务
propagation="SUPPORTS" 事务支持的 新增(事务) 查询(公用一个事务)
查询 查询 不需要事务
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--3.3配置事务切面AOP
通知类型:
能否控制目标方法执行?
前置通知: 目标方法执行之前
后置通知: 目标方法执行之后
异常通知: 目标方法执行之后抛出异常时执行
最终通知: 目标方法执行之后,最后执行的通知
记录程序执行状态
环绕通知: 目标方法执行之前之后都要执行.
public void around(ProceedingJoinPoint joinPoint){
//业务控制
String str = null;
if(str == null){
System.out.println("没有权限");
}else{
joinPoint.proceed();//执行目标方法
}
}
within(包名.类名):按类匹配 配置的类中的方法执行时,
都要执行通知
粗粒度 实际中不用
execution(返回值类型 包名.类名.方法名(参数列表))
细粒度控制
execution(* com.jt.manage.service..*.*(..))
AOP总结:
在不改变原有代码的条件下,对方法进行扩展.
切入点 + 通知方法
-->
<aop:config>
<aop:pointcut
expression="execution(* com.jt.manage.service..*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
5.配置springmybatis
<!--1.整合Mybatis -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:/mybatis/mappers/*.xml"/>
<!--添加别名包 -->
<property name="typeAliasesPackage" value="com.jt.manage.pojo"/>
</bean>
<!--2.为Mapper接口生成代理 根据包扫描的形式为接口创建代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jt.manage.mapper"/>
</bean>
6.编辑mybatis配置文件
<configuration>
<settings>
<!-- 开启驼峰自动映射
案例讲解:
对象 user{userId,userName,userAddr}
表 tb_user(user_id,user_name,user_addr)
如果按照上述的配置,则使用RestType不能自动映射.
需要使用restMap(必须手动映射)
驼峰映射规则:
采用resultType时将字段中的"_"自动去除,
并且之后的首字母大写.转化完成后,实现对象映射.
-->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!--Mybatis缓存
1.一级缓存 默认开启
sqlSession? 将数据库链接进行扩展(实现orm)
在sqlSession内实现数据共享
一个用户
2.二级缓存 默认关闭的
只要是一个sqlSessionFacotry生产的sqlSession
那么实现数据共享
多用户
因为Mybatis自身的二级缓存性能较低.实际开发中不用.
实际开发中使用redis
-->
</settings>
</configuration>