spring与mybatis

spring当然也提供了对mybatis的支持

第一步需要添加jar包
mybatis-3.3.0.jar
spring-jdbc-4.2.0.RELEASE.jar--spring链接jdbc的jar
mybatis-spring-1.2.3.jar---非常重要的jar包
mysql-connector-java-5.1.15-bin.jar--数据库链接的
spring-tx-4.2.0.RELEASE.jar

第二部,在src目录下建立

SqlMapConfig.xml------mybatis配置文件
datasource.properties---数据源(可以省略)
applicationContext.xml---spring配置文件

SqlMapConfig.xml
这个文件不需要写以前那么多了,很多不支持的,或者在spring里边已经写完了只需要写
<typeAliases>
<package name="com.wode.pojo"/>
</typeAliases>
就可以了


applicationContext.xml这个配文文件的内容和我们刚才的jdbc差不多
<context:property-placeholder location="datasource.properties" />--引入我们的数据库配置文件
配置datasource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

配置SqlSessionFactoryBean-并且读取我们的SqlMapConfig.xml文件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
</bean>


让自己去找mapper文件
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wode.mapper"></property>
</bean>

别的东西和我们的以前写的一模一样,直接注入接口就可以了

二:配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=admin
<?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>
    <typeAliases>
        <package name="com.wode.pojo"/>
    </typeAliases>
    
</configuration>
<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <context:component-scan base-package="com.wode">
    </context:component-scan>
    <context:property-placeholder location="datasource.properties" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        >
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wode.mapper"></property>
    </bean>
</beans>
原文地址:https://www.cnblogs.com/xieshunjin/p/5797708.html