如何在Eclipse中使用MyBatis连接到SQLserver

时间:2018-12-20 09:06:40

标签: java sql-server model-view-controller mybatis

因为我开始学习SQLserver,所以我想将SQLserver用作项目的数据库,但是当我使用mybatis配置数据源时,我不知道如何选择数据驱动程序。 所以我想知道SQLserver是否可以连接到SSM项目?

我想将SQLserver用作spring + springMVC + mybatis项目的数据库。我该怎么办?

--This is db.properties
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test
--This is applicationContext.xml
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

1 个答案:

答案 0 :(得分:0)

我使用一个单独的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>
        <!-- changes from the defaults -->
       <setting name="lazyLoadingEnabled" value="false" />
       <!-- Mybatis logging -->
<!--        <setting name="logImpl" value="LOG4J2"/> -->
    </settings>
    <typeAliases>
        <package name="com.mydomain.package"/>
    </typeAliases>
    <environments default="development">
      <environment id="development">
         <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
               <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
               <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test"/>
               <property name="username" value="username"/>
               <property name="password" value="password"/>
            </dataSource>
      </environment>
   </environments>

   <mappers>
      <mapper resource="mappers/ExampleMapperFile.xml"/>

   </mappers>
</configuration>

然后在application.properties中引用它:

mybatis.config=classpath:MybatisConfig.xml

其他mybatis属性是:

#mybatis.mapperLocations=classpath*:**/mappers/*.xml
#mybatis.typeAliasesPackage=com.mydomain.package
#mybatis.typeHandlersPackage=handler's package
#mybatis.check-config-location=check the mybatis configuration exists
#mybatis.executorType=mode of execution. Default is SIMPLE