1、Mybatis究竟在做什么?
SqlSessionFactoryBuilder—>SqlSessionFactory—>SqlSession—>操作数据库
2、Spring做什么?
管理bean,管理bean之间的关系
1、Spring管理SqlSessionFactory,bean
2、Spring对于数据库操作的支持:从Spring管理的Factory中得到SqlSession
3、SqlSession数据操作——由mybatis完成
4、有哪些bean:
a)、DataSource;
b)、SqlSessionFactory;
c)、xxxDao:数据操作,后续service层要找Spring要dao的bean
1、dataSource:Spring的核心配置文件中配置,是数据库的相关配置
2、sessionFactory:build读取mybatis的核心配置文件(数据库环境/注册映射文件)。依赖于dataSource这个bean,mybatis的核心配置文件要提供注册的映射文件
3、dao:使用sqlSession来完成数据库操作,sqlSession来自sessionFactory
案例:
代码结构:
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>
<mappers>
<!-- 加载映射文件 -->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
片
spring核心配置文件
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 引入db.preperties -->
<context:property-placeholder location="classpath:db.properties" />
<!--
配置数据源:bean,Spring会通过配置文件将该对象实例化,Apache提供DataSource支持类
Spring可以实例化出DataSource数据源对象
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- 配置sqlsessionfactory:spring提供的管理,提供了一个类 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源的配置,依赖于上面的数据源配置 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="mybatis/config.xml"></property>
</bean>
<!--
Spring要管理的实例bean:dao
实现数据操作:使用sqlsession,来自sqlsessionFactory
-->
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
UserMapper.xml
<?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="dao.UserDao">
<select id="findByid" parameterType="int" resultType="entity.User" >
select * from user where id = #{
id}
</select>
</mapper>
UserDao
public interface UserDao {
public User findByid(int id);
}
UserDaoImpl继承daoSuppport之后,得到factory
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
@Override
public User findByid(int id) {
//继承daoSuppport之后,得到factory
SqlSession session = this.getSqlSession();
User user = session.selectOne("dao.UserDao.findByid",id);
return user;
}
}
测试
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
UserDao dao = (UserDao) ac.getBean("userDao");
User user = dao.findByid(1);
System.out.println(user);
}
}
1、在Spring核心配置文件中配置dataSource和sessionFactory
2、在Spring中配置dao代理类的bean
案例:
代码结构
spring核心配置文件
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 引入db.preperties -->
<context:property-placeholder location="classpath:db.properties" />
<!--
配置数据源:bean,Spring会通过配置文件将该对象实例化,Apache提供DataSource支持类
Spring可以实例化出DataSource数据源对象
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- 配置sqlsessionfactory:spring提供的管理,提供了一个类 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源的配置,依赖于上面的数据源配置 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>
</beans>
UserMapper.xml与上面代码相同,UserMapper接口与上面UserDao代码相同
测试类
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
UserMapper dao = (UserMapper) ac.getBean("userMapper");
User user = dao.findByid(1);
System.out.println(user);
}
}