在spring mvc +javaweb项目中利用 tomcat和mybatis连接sqlserver

写java web应用时需要连接sqlserver数据库的步骤如下:

  1. 安装tomcat。
  2. 打开myeclipse,需要把自己安装的jdk与tomcat都引入,不要用自带的(这一步很重要)
  • jdk配置:
    • window --> preferences
    • Java --> Installed JREs
    • 先不管当前的配置,选择ADD
    • ADD之后选择standard VM,然后点击next
    • 在新弹出框中,这里就要用到Java home和jre name,这是配置Java环境是设置好了的
  • tomcat配置
    • window --> preferences
    • Servers->Tomcat,选择对应版本,选择本地路径即可。
  1. 下载sqljdbc4.jar。只需要这一个jar包就够了。
  2. 把sqljdbc4.jar放在对应的4个目录:1)/java/jre/lib/ext 2)/java/jdk/jre/lib/ext 3)/tomcat/lib 4)对应java web项目中的lib
  3. 在 mybatis.xml的配置文件中编写配置。其中,红色字体部分就是tomcat对应的Resource name。

  • 在mybatis配置中,dao包中的接口类与配置文件名字一定要相同。接口类需要加上@Repository
  • 在mybatis配置中,domain包中的对象类一定要进行序列化,即实现Serializable接口,并重写toString()函数。
  • <?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:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/jdbc 
         http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
      	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:/comp/env/sqlserver" />
    	</bean>
    
        
        <!-- 注解方式实现事务 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        <!-- 事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        
        <!-- define the SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="typeAliasesPackage" value="com.invoice.api.domain"/>
        </bean>
        
        <!-- scan for mappers and let them be autowired -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
            <property name="basePackage" value="com.invoice.service.dao" />
        </bean>
        
    </beans>
    


  1. 修改tomcat中的配置
  • 在/tomcat/conf/context.xml文件中,在标签<Context>下新增子标签<ResourceLink>
<ResourceLink name="sqlserver" global="sqlserver" type="javax.sql.DataSource" />
  • 在/tomcat/conf/server.xml文件中,在标签<GlobalNamingResources>下新增子标签<Resource>
    <Resource name="sqlserver"
                  auth="Container"
                  type="javax.sql.DataSource"
                  driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                  url="jdbc:sqlserver://127.0.0.1:1433;databaseName=test"
                  username="test"
                  password="000000"
                  maxActive="30"
                  maxIdle="5"
                  maxWait="10000" />
    以上两步就足够tomcat的配置。

遇到的问题

  • HTTP 405 (这是因为HTTP请求中的GET/POST方法弄混了,405的意思是指找到了服务器,也找到了对应的方法入口,但是调用的方法没有匹配成功)
  • 把项目搬过来的时候,报错【Name jdbc is not bound in this Context或者是Cannot create JDBC driver of class '' for connect URL 'null'】。(这就是上下文没有匹配到JDBC.在myeclipse中不要用自带的tomcat,把自己新建的TOMCAT配进去就行)
  • 在mybatis里整型变量比较需要加.toString(),如<if test ="num == '0'.toString()">
  • 还是不明白java:/comp/env/这个变量到底在哪里声明的?
  • sqlserver在tomcat中连接url的配置格式是:Oracle、Mysql和SQL Server数据库连接的URL写法
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
String url="jdbc:sqlserver://localhost:1433;databaseName=mydb"; 
//mydb为数据库 
String user="sa"; 
String password=""; 
Connection conn= DriverManager.getConnection(url,user,password);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章