前言:在此以前,我給你們講到了三大框架中spring和springmvc的搭建和整合,由於內容過多,因而我分紅了兩部分;今天給你們講的是mybatis的整合以及spring和mybatis的整合!!!java
其樂蛋糕店後臺管理系統 | 地址 |
---|---|
Github--其樂後臺管理系統源碼 | https://github.com/XINGGou/qile |
其樂後臺管理系統(一)--項目介紹 | http://www.javashuo.com/article/p-gbpajmha-dd.html |
其樂後臺管理系統(二)--整合三大框架(spring+springmvc+mybatis) | http://www.javashuo.com/article/p-nzkhhnxu-dp.html |
其樂後臺管理系統(三)--整合mybatis框架(三大框架搭建成功) | http://www.javashuo.com/article/p-ozfmlifj-ee.html |
其樂後臺管理系統(四)--門店管理模塊 | http://www.javashuo.com/article/p-ragyplqr-cu.html |
其樂後臺管理系統(五)--訂單管理系統 | http://www.javashuo.com/article/p-oqpdbazv-en.html |
<!-- 整合mybatis框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <!-- druid鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency>
<?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"> <!-- MyBatis的全局配置文件 --> <configuration > <!-- 1.配置開發環境 --> <environments default="develop"> <!-- 這裏能夠配置多個環境,好比develop,test等 --> <environment id="develop"> <!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) --> <transactionManager type="JDBC"></transactionManager> <!-- 1.2.配置數據源,即鏈接池方式:JNDI/POOLED/UNPOOLED --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/qiledb?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml --> </configuration>
若須要數據庫可在github上下載:https://github.com/XINGGou/qilemysql
package com.it.pojo; /** * 門店信息 */ public class Door { private Integer id; //門店編號 private String name; //門店名稱 private String tel; //門店電話 private String addr; //門店地址 //getter和setter方法 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } //重寫toString方法 @Override public String toString() { return "Door [id=" + id + ", name=" + name + ", tel=" + tel + ", addr=" + addr + "]"; } }
配置內容以下:git
<?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"> <!-- 門店表的映射文件 namespace值爲對應接口的全路徑 --> <mapper namespace="com.it.mapper.DoorMapper"> <!-- 1.查詢全部門店信息,id值爲對應接口中方法的名字 resultType指定將查詢的結果封裝到哪一個pojo對象中 --> <select id="findAll" resultType="com.it.pojo.Door"> select * from tb_door </select> </mapper>
<!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml --> <!-- 配置映射文件 --> <mappers> <mapper resource="mybatis/mapper/DoorMapper.xml"/> </mappers>
package com.it.mapper; import java.util.List; import com.it.pojo.Door; /** * DoorMapper接口 * 聲明增刪改查方法,對門店信息進行操做 */ public interface DoorMapper { /** * 1.查詢全部門店信息 */ public List<Door> findAll(); }
package com.it.controller; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.it.mapper.DoorMapper; import com.it.pojo.Door; public class TestMybatis { public static void main(String[] args) throws Exception { //1.讀取mybatis-config.xml核心文件 InputStream in = Resources.getResourceAsStream( "mybatis/mybatis-config.xml"); //2.獲取SqlSessionFactory工廠 SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(in); //3.獲取SqlSession對象 SqlSession session = factory.openSession(); //4.獲取DoorMapper接口的實例 DoorMapper mapper = session.getMapper(DoorMapper.class); //5.調用findAll方法查詢全部門店信息 List<Door> list = mapper.findAll(); //6.遍歷全部門店信息 for(Door door : list){ System.out.println(door); } } }
執行結果爲:github
當顯示出結果時,說明mybatis整合成功,接下來將整合spring和mybatisweb
<?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"> <!-- MyBatis的全局配置文件 --> <configuration > <!-- 1.配置開發環境 --> <!-- 1.1.配置事務管理方式:JDBC:將事務交給JDBC管理(推薦) --> <!-- 1.2.配置數據源,即鏈接池方式:JNDI/POOLED/UNPOOLED --> <!-- 2.加載Mapper配置文件,路徑以斜槓間隔: xx/xx/../xx.xml --> </configuration>
<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"> <!-- 1.加載jdbc.properties文件的位置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置druid鏈接池 ,id是固定值,class是druid鏈接池類的全路徑 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 配置鏈接數據庫的基本信息 --> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> <!-- 3.整合spring和mybatis框架 將SqlSession等對象的建立交給Spring容器 id值(sqlSessionFactory)是固定值 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 3.1.指定mybatis核心配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 3.2.配置鏈接池(數據源) ref指向鏈接池bean對象的id值 --> <property name="dataSource" ref="dataSource"></property> <!-- 3.三、掃描全部的 XxxMapper.xml映射文件,讀取其中配置的SQL語句 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- 四、定義mapper接口掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描全部XxxMapper接口,將接口實例的建立交給spring容器 --> <property name="basePackage" value="com.it.mapper"/> </bean> <!-- 5.配置須要掃描的包(service層):spring自動去掃描 base-package下的類, 若是掃描到的類上有 @Controller、@Service、@Component等註解, 將會自動將類註冊爲bean(即由spring建立實例) --> <context:component-scan base-package="com.it.service"> </context:component-scan> </beans>
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql:///qiledb?characterEncoding=utf-8 db.username=root db.password=root
package com.it.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.it.mapper.DoorMapper; import com.it.pojo.Door; /** 測試類:測試SSM開發環境 */ @Controller /* 這個註解表示當前類屬於Controller層代碼 */ public class TestSSM { /** 自動裝配:由spring自動爲屬性賦值(對象) */ @Autowired DoorMapper doorMapper; @RequestMapping("/testssm") public String testSSM(){ //1.調用findAll方法查詢全部門店信息 List<Door> list = doorMapper.findAll(); //2.遍歷全部門店信息 for(Door door : list){ System.out.println(door); } return "test"; } }
在加載完成後,輸入/testssmspring
結果圖:sql