Mybatis数据源的创建

来源:互联网 发布:河南大学二本专业知乎 编辑:程序博客网 时间:2024/05/20 16:45

代码中是由service 调用 dao的接口实现,然后通过dao的全类名关联到配置的.xml文件,通过方法的名字查询该文件对应的SQL 语句。


些时,就开始由sql session Factory 创建一个实例,该实例注入对应的连接池的实例,事务会通过监听器的方法自动调用,最终能够执行相应的SQL语句。


--Dao:

@Repository
public interface TestDao {


  public Integer getName(Integer id);


-- Service:


  public Boolean getUserName(RestType restType) {
    
    Integer id= testDao.getUserId();


--映射文件:

<mapper namespace="com.TestDao">

<select id="getName" resultType="Integer" parameterType="Integer">


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath*:com/dao/*.xml" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />


0 0
原创粉丝点击