spring-mybatis
(1) 依赖:
1.mysql-driver
2.mybatis
3.spring
4.spring-jdbc
5.spring-mybatis
mybatis的原理:
spring的原理:
spring框架与mybatis框架整合,首先先从jdbc(java.sql包下提供的一套接口,statement)开始,从底层往上走,结果集怎么映射成java的对象
(2) 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<!--引入context模块-->
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!--组件扫描控制model(模型层)层-->
<context:component-scan base-package="cn.kgc.kb08"/>
<!--启动注解配置-->
<context:annotation-config/>
<!--引入数据配置文件 xxx.properties-->
<context:property-placeholder location="classpath:datasource.properties"/>
<!--数据源实体bean(spring-jdbc 提供)-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driver}"/>驱动
<property name="url" value="${mysql.url}"/>连接字符串
<property name="username" value="${mysql.username}"/>账号
<property name="password" value="${mysql.password}"/>密码
</bean>
<!--数据源事务管理bean(spring-jdbc 提供)-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>数据源关联依赖
</bean>
<!--SQL会话工厂bean(mybatis-jdbc 提供)-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>数据源关联依赖
<property name="typeAliasesPackage" value="cn.kgc.kb08.entity"/>别名包
<property name="mapperLocations" value="classpath:mapping/*Mapper.xml"/>mapper.xml关联
</bean>
<!--Mapper接口扫描bean(mybatis-jdbc 提供)-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>SQL会话工厂关联
<property name="basePackage" value="cn.kgc.kb08.mapper"/>mapper接口关联
</bean>
<bean id="vssService" class="cn.kgc.kb08.service.VStuScoreService"/>
</beans>