zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    用于主题检测的临时日志(594fb726-af0b-400d-b647-8b1d1b477d72
    返璞归真vc++之字符类型
    DIV居中
    程序员职业生涯
    枚举进程句柄
    不使用mutex设计模式解决并发访问cache
    服务器权重分配算法
    xmemecached中的一致性hash算法
    安卓课堂练习
    pythonPTA---分支循环与集合7-1 jmu-python-韩信点兵 (20分) 7-2 打印数字矩形 (10分) 7-3 成绩统计 (10分) 7-4 找列表中最大元素的下标 7-5 删除列表中的重复值
  • 原文地址:https://www.cnblogs.com/xieshunjin/p/5797708.html
Copyright © 2011-2022 走看看