Spring整合MyBatis 你get了嗎?
阿新 • • 發佈:2018-12-07
Spring整合MyBatis
1.整體架構
dao,entity,service,servlet,xml
2..引入依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <!--cglib--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <!--mysql驅動 jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!--spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--資料來源--> <!--dbcp 資料來源--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--c3p0 資料來源--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--druid 資料來源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.29</version> </dependency> <!--mybatis jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.1</version> </dependency> <!-- Spring整合JavaWeb的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--javaee jar--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> </dependencies>
3.MyBatis配置檔案
配置個typeAliases
4.xml
1.編寫資料來源
2.建立 SqlSessionFactoryBean 節點
2.1連線資料來源
2.2寫入MyBatis的配置檔案(加一個classPath:)
3.建立MapperScannerConfguln 節點
3.1配置basePackage ref是dao資料夾全路徑
4.配置service節點
4.1傳入dao層介面(注意命名規範,介面要用I開頭,方便識別)
5.編寫web.xml
5.1編寫上下文
5.2編寫監聽器
5.3編寫servlet web.xml的方式
6.編寫servlet
6.1編寫新增圖書的邏輯程式碼。
xml:
web.xml:
mybatis配置檔案:
servlet(編寫servlet記得引javaee的依賴):
頁面: