Spring集成MyBatis

还是使用maven工程来完成这次测试,这里也可以参考中文官方文档

1、利用Spring创建Mapper

2.1导入meven依赖

脚下留心:mybatis-spring的版本必须与Mybatis的版本要相匹配,否则就会报错!
在这里插入图片描述
2、配置xml文件

  • beans-datasource.xml(Spring生成mybatis对象 的xml文件)

Properties文件配置如下:

url=jdbc:mysql://localhost:3306/yimaiwang?useUnicode=true&amp&characterEncoding=utf-8
driver=com.mysql.jdbc.Driver
username2=root
password=123456

beans-datasource.xml 文件配置如下:

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">


	<!--1、加载数据库的配置信息 -->
    <context:property-placeholder location="database.properties"/>
	<!--2、datasource数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}" />
		<property name="url"  value="${url}"/>
		<property name="username"  value="${username2}"/>
		<property name="password" value="${password}"/>
	</bean>
	<!-- 3、sqlSessionFactory -->
	<bean id="sqlSessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
         <!-- 别名 -->
       <property name="typeAliasesPackage"  value="com.cc.model"></property>
       <!-- mapper  XML映射 -->
        <property name="mapperLocations"  value="classpath*:mapper/*Mapper.xml"></property>
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
	<!-- mapper包位置 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="mapper"></property>
    </bean>

    <!-- 5、事务管理 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
	
</beans>
  • mybatis-config.xml (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>
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="pojo"/>
        <package name="vo"/>
    </typeAliases>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
        </plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/yimaiwang?useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="mapper"/>
    </mappers>

</configuration>
  • Mapper.xml(mybatis接口文件)
    (使用Mybatis逆行工程或手动写sql语句的xml文件)

3、编写测试类:

/**
*@auther Nical
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-datasource.xml")
public class TestMyBatis {
 @Autowired 
 UserMapper userMapper;
  	@test
	public void testUser() {
	   List<User> users = userMapper.selectById(1);
	   System.out.println("users==>"+users);
	 }
}

脚下留心:

spring-database.xml中的${username}默认获取的是win操作系统的用户名
故:datasource.properties文件 username=" " 改为userName=" "

spring集成mybait的包的版本问题:
	mybatis-spring-1.2.2只能集成mybatis 3.2版本不能继承最新的 3.5.5
	需要maven仓库获取mybatis-spring-2.x的版本的jar 否则会报错!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值