Latex符號表
阿新 • • 發佈:2022-04-17
回顧Spring-mybatis 與 mybatis的結構比較
對比
Spring-mybatis 三大改變(準確是2個改變 1.覆蓋配置 2.改寫方法)
1.使用Spring方式實現了原來寫在mybatis裡面獲取SqlSession的工具類(自定義的)
2.可以使用Spring方式實現mybatis-config.xml裡面的配置(也可以兩者互通)
3.可以使用Spring方式實現介面的方法(也可使用註解) [然後方法裡面去獲得對應的mapper]
相關依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- test單元測試模組匯入的包--> <dependencyView Code> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>Test_885</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
靜態資源過濾
</dependencies> <!-- 靜態資源過濾--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build>View Code
改寫方法1
xml
<!-- 使用Spring的資料來源替換Mybatis的配置--> <bean id="dataSource88" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="my7shig69l>A"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource88" /> <!-- 繫結(替代)Mabatis配置檔案 然後可以設定相關的屬性--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/ljm/dao/UserMapper.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能使用構造器注入,因為沒有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="userMapper_01" class="com.ljm.dao.UserMappermpl"> <property name="sqlSession" ref="sqlSession"/> </bean>View Code
實現類
public class UserMappermpl implements UserMapper{ //原來所有操作都使用sqlSession執行,現在使用SqlSessionTemplate; private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public List<User> select_user(){ UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.select_user(); } }View Code
測試
public void test02() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper_01",UserMapper.class); for (User user : userMapper.select_user()) { System.out.println(user); } }View Code
改寫方法2(本質與方法一相同,不過是簡化了步驟)
xml
<bean id="dataSource88" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="my7shig69l>A"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource88" /> <!-- 繫結(替代)Mabatis配置檔案 然後可以設定相關的屬性--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/ljm/dao/UserMapper.xml"/> </bean> <bean id="userMapper_02" class="com.ljm.dao.UserMappermpl02"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>View Code
實現類
public class UserMappermpl02 extends SqlSessionDaoSupport implements UserMapper{ // SqlSessionDaoSupport這個類幫忙寫了之前的程式碼 public int insert_user(User user){ return getSqlSession().getMapper(UserMapper.class).insert_user(user); } }View Code
測試
public void test03() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper_02",UserMapper.class); for (User user : userMapper.select_user()) { System.out.println(user); } }View Code
Spring 相關配置(見i上面id=sqlSessionFactory)
指定mybatis-config.xml檔案
指定UserMapper.xml檔案