6.3.4 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> <!– Spring 整合 MyBatis 后, MyBatis中配置数据源,事务等一些配置都可以 迁移到Spring的整合配置中。MyBatis配置文件中只需要配置与MyBatis相关 的即可。 –> <!– settings: 包含很多重要的设置项 –> <settings> <!– 映射下划线到驼峰命名 –> <setting name=“mapUnderscoreToCamelCase” value=“true”/> <!– 设置Mybatis对null值的默认处理 –> <setting name=“jdbcTypeForNull” value=“NULL”/> <!– 开启延迟加载 –> <setting name=“lazyLoadingEnabled” value=“true”/> <!– 设置加载的数据是按需还是全部 –> <setting name=“aggressiveLazyLoading” value=“false”/> <!– 配置开启二级缓存 –> <setting name=“cacheEnabled” value=“true”/> </settings> </configuration> |
- SQL映射文件配置
|
<?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”> <mapper namespace=“com.atguigu.ssm.mapper.EmployeeMapper”> <!– public List<Employee> getAllEmps(); –> <select id=“getAllEmps” resultMap=“myEmpsAndDept” > select e.id eid, e.last_name,e.email,e.gender, d.id did, d.dept_name from tbl_employee e ,tbl_dept d where e.d_id = d.id </select> <resultMap type=“com.atguigu.ssm.beans.Employee” id=“myEmpsAndDept”> <id column=“eid” property=“id”/> <result column=“last_name” property=“lastName”/> <result column=“email” property=“email”/> <result column=“gender” property=“gender”/> <association property=“dept” javaType=“com.atguigu.ssm.beans.Department”> <id column=“did” property=“id”/> <result column=“dept_name” property=“departmentName”/> </association> </resultMap> </mapper> |
6.3.5 Spring 整合MyBatis 配置
|
<!– Spring 整合 Mybatis –> <!–1. SqlSession –> <bean class=“org.mybatis.spring.SqlSessionFactoryBean”> <!– 指定数据源 –> <property name=“dataSource” ref=“dataSource”></property> <!– MyBatis的配置文件 –> <property name=“configLocation” value=“classpath:mybatis-config.xml”></property> <!– MyBatis的SQL映射文件 –> <property name=“mapperLocations” value=“classpath:mybatis/mapper/*.xml”></property> <property name=“typeAliasesPackage” value=“com.atguigu.ssm.beans”></property> </bean> <!– Mapper接口 MapperScannerConfigurer 为指定包下的Mapper接口批量生成代理实现类.bean的默认id是接口名首字母小写. –> <bean class=“org.mybatis.spring.mapper.MapperScannerConfigurer”> <property name=“basePackage” value=“com.atguigu.ssm.mapper”></property> </bean> <!– <mybatis-spring:scan base-package=”com.atguigu.ssm.mapper”/> –> |
6.4 整合测试
- 编写页面,发送请求:http://localhost:8888/ssm/employees
- 编写Handler,处理请求,完成响应
- 在页面中获取数据,显示数据
上一篇: MyBatis框架
下一篇: SpringMVC框架

