spring mvc整合 mybatis (非maven)
spring mvc在
https://mp.csdn.net/postedit/80430167
這篇已經搭建好了,缺少資料庫總歸是個殘疾專案
這裡實現如何把mybatis整合到 spring mvc專案中
這裡的環境
jdk 1.8
tomcat 8
mysql
一 先說jar包,
需要的jar包有三個,
第一個 mysql的驅動jar
mysql-connector-java
連線
http://xcy.xiaoshikd.com/mysql-connector-java-5.1.44.zip
第二個
mybatis jar
連線:
https://oss.sonatype.org/content/repositories/snapshots/org/mybatis/mybatis/(選擇自己需要的版本下載)
第三個spring 與 mybatis整合的jar
mybatis-spring.jar
連結:
https://github-production-release-asset-2e65be.s3.amazonaws.com/8247368/2f47fcca-276a-11e8-839b-226c8ebf4223?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180525%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180525T054517Z&X-Amz-Expires=300&X-Amz-Signature=2a7de77ceb3e49b6927c7c7a06f307e951c64eebc8e4dfe477269280016463f1&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dmybatis-spring-1.3.2.zip&response-content-type=application%2Foctet-stream
二 編寫配置檔案
新增了三個配置檔案,和一個連線db的引數檔案
分別是
sqlMapConfig.xml(mybatis的總配置檔案由於有spring的存在,所以這裡配置很簡單)
<?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>
</configuration>
spring-dao.xml(這裡面建立的jdbc的連線池,並且確定了該掃描哪裡的mapper介面和xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 載入配置檔案 -->
<context:property-placeholder location="/WEB-INF/db.properties" />
<!-- 資料庫連線池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 資料庫連線池 -->
<property name="dataSource" ref="dataSource" />
<!-- 載入mybatis的全域性配置檔案,雖然這個全域性配置檔案是空的,但是這個全域性配置檔案是必不可少的 -->
<property name="configLocation" value="/WEB-INF/sqlMapConfig.xml" />
</bean>
<!-- 配置Mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.mapper"/>
</bean>
</beans>
spring-trans.xml(主要配置了事務管理)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事務管理器,用的是Spring JDBC的事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 資料來源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.itheima.springmvc.service.*.*(..))" />
</aop:config>
</beans>
web.xml增加呼叫配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-dao.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
還有一個db.properties (連線db的引數)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://
jdbc.username=
jdbc.password=
配置完之後,直接啟動,發現控制檯沒有錯誤,說明沒少配置什麼
三 就可以編寫mybatis 相關邏輯了
IMybatisMapper.java
IMybatisMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springMvc.mapper.IMybatisMapper">
<select id="selectUserId" parameterType="hashmap" resultType="hashmap">
select
1 userId,
1 passWord
from
TMP_IMP01
where
im_plate = #{userId}
and customer_no = #{passWord}
</select>
</mapper>
這裡面namespace 要對應介面的名字
service呼叫
四 直接啟動,看是否有錯誤
Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
這裡少了兩個jar
1 commons dbcp.jar https://mirrors.tuna.tsinghua.edu.cn/apache//commons/dbcp/binaries/commons-dbcp-1.4-bin.zip
2 commons pool.jar https://mirrors.tuna.tsinghua.edu.cn/apache//commons/pool/binaries/commons-pool-1.6
引入後 繼續啟動
發現就剩一點程式碼的小問題,解決完,使用log4就看到日誌查詢到了資料庫中的內容
至此 ssm成功搭建