当前位置: 代码迷 >> J2EE >> mybatis spring 整合的一点有关问题
  详细解决方案

mybatis spring 整合的一点有关问题

热度:243   发布时间:2016-04-22 01:37:21.0
mybatis spring 整合的一点问题
----------spring配置文件----------------

<?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:p="http://www.springframework.org/schema/p"
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">


<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="classpath:mybatis.xml" />
</bean>

<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.insigma.mappers.Mapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<bean id="smapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.insigma.mappers.SMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<bean id="test" class="com.insigma.test.Tes">
<property name="mapper" ref="mapper">
</property>
<property name="id" value="12"></property>
</bean>

</beans>  

----------mybatis配置文件----------------
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>

<typeAliases>
<typeAlias type="com.insigma.model.Student" alias="student" />
</typeAliases>

<mappers>
<mapper resource="com/insigma/mappers/Mapper.xml" />
<mapper resource="com/insigma/mappers/SMapper.xml" />
</mappers>
</configuration>

----------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">

<mapper namespace="com.insigma.mappers.Mapper">

<select id="getAll" resultType="student">
select * from student2 
</select>

</mapper> 

----------mapper接口----------------
package com.insigma.mappers;

import java.util.List;

import org.springframework.stereotype.Component;

import com.insigma.model.Student;

public interface Mapper {

public List<Student> getAll();
}
  相关解决方案