1. 程式人生 > >Spring4整合MyBatis3 (1)

Spring4整合MyBatis3 (1)

開發環境配置:

1.安裝jdk8 2.安裝eclipse 3.安裝Tomcat8 4.安裝MySql5

準備資料庫資源:

#建立使用者表
create table tb_user(
id int primary key auto_increment,#id
loginname varchar(50)  unique,    #登入名 郵箱
       PASSWORD VARCHAR(18),          #密碼
      username     VARCHAR(18),          #使用者名稱
      phone          varchar(18),              #電話
     address        varchar(255)             #地址
);
INSERT INTO tb_user(loginname,PASSWORD,username,phone,address) values('jack','123456','傑克','13920001234',廣州市天河區)


CREATE TABLE tb_book(

id INT (11)  PRIMARY KEY AUTO_INCREMENT,
name  VARCHAR(54),
        author  VARCHAR(54),
        publicationdate  DATE,
        publication VARCHAR(150),
price    double;
  image varchar(54),
remark  varchar(600)  
)
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('1','瘋狂java講義(附光碟)','李剛 編著','2008-10-01','電子工業出版社','74.2','java.jpg','瘋狂源自夢想');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('2','瘋狂ajax講義(附光碟)','李剛 編著','2009-10-01','電子工業出版社','84.2','java.jpg','瘋狂源自夢想,是啊');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('3','瘋狂xml講義(附光碟)','李剛 編著','2010-10-01','電子工業出版社','94.2','java.jpg','瘋狂源自夢想,必須的');
INSERT INTO tb_book(id,name,author,publicationdate,publication,price,image,remark)  values('4','瘋狂ios講義(附光碟)','李剛 編著','2011-10-01','電子工業出版社','104.2','java.jpg','瘋狂源自夢想,肯定的')

完成配置檔案:

新建一個專案fkbookapp,加入jar包

fkbookapp/src/db.properties

dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/mybatis
dataSource.user=root
dataSource.password=root
dataSource.maxPoolSize=20
dataSource.maxIdleTime=1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
  fkbookapp/WebContent/WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
			            http://www.springframework.org/schema/context
			            http://www.springframework.org/schema/context/spring-context-4.2.xsd
			            http://www.springframework.org/schema/mvc
			            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
			            http://www.springframework.org/schema/tx
			            http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
			            http://mybatis.org/schema/mybatis-spring 
			            http://mybatis.org/schema/mybatis-spring.xsd ">
		
		
		<!-- mybatis:scan會將org.fkit.mapper包裡的所有介面當做mapper配置,之後可以自動引入mapper類 -->
		<mybatis:scan base-package="org.fkit.mapper"/>
		<!-- 掃描org.fkit包下面的java檔案,有Spring的相關注解的類,則把這些類註冊為Spring 的bean -->
		<context:component-scan base-package="org.fkit" />
		<!-- 使用PropertyOverrideConfigurer後處理器載入資料來源引數 -->
		<context:property-override location="classpath:db.properties"/>
		<!-- 配置C3p0資料來源 -->
		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"/>
		<!-- 配置SqlSessionFacotroy,org.mybatis.spring.SqlSessionFactoryBean是MyBatis社群開發用於整合Spring的Bean -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" />
		<!-- JDBC事務管理器 -->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
		<!-- 啟用支援annotation註解方式事務管理 -->
		<tx:annotation-driven transaction-manager="transactionManager" /> 
 </beans>

fkbookapp/WebContent/WEB-INF/springmvc-config.xml


<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd     
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        
   	 <!-- 自動掃描該包,springMVC會將包下用了@controller註解的類註冊為Spring的controller -->
    	<context:component-scan base-package="org.fkit.controller"/>
     <!-- 設定預設配置方案 -->
    	<mvc:annotation-driven/>
     <!-- 檢視解析器 -->
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<!-- 字首 -->
    		<property name="prefix">
    			<value>/WEB-INF/content/</value>
    		</property>
    		<!-- 字尾 -->
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
</beans>